es6.3.1 搜索中must和should混合的用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zch3210/article/details/89471618

再使用must和should混合查询的时候,发现should并不起作用。

如a==1时搜索b=1或者b=2的数据,按照编程语言的逻辑则是在a=1的条件下必须满足b=1或者b=2,

所以must和should平级的写法是错误的。

注意错误写法

根据搜索结果可以发现should并未起作用

正确写法

$params = [
            'index' => 'news',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            ['match' => ['age' => 50]],
                            ['bool' => [
                                'should' => [
                                    ['match' => ['content' => '西红柿']],
                                    ['match' => ['content' => '中国和美国']]
                                ]
                            ]]
                        ]
                    ]
                ]
            ]
        ];
        $result = $this->es->search($params);
        var_dump($result);

搜索结果

猜你喜欢

转载自blog.csdn.net/zch3210/article/details/89471618