搜索两个过滤器均与Elasticsearch匹配的位置

时间:2018-10-10 13:17:03

标签: php sql elasticsearch

我正在使用elasticSearch进行搜索,但是我想对其进行过滤,并且搜索必须匹配两个过滤器。

这是代码:

"query" => [
                            "bool" => [
                                "must" => [
                                    'multi_match'=> [
                                        'query'=> $this->input->get('search'),
                                        'fields'=> ["title^2", "description"]
                                    ]
                                ],
                                "filter" => [
                                    "terms" => [ 
                                        "Categories" => ['Featured Works']
                                    ],
                                    "terms" => [
                                        "Tags" => ['Merida']
                                    ]
                                ]
                            ]
                        ]

携带搜索结果,但将过滤器作为“或”,我想要一个“与”

1 个答案:

答案 0 :(得分:0)

问题在于如何在PHP关联数组中定义terms过滤器。由于两者具有相同的键,因此只有一个键会出现在您的查询中。改为这样:

                            "filter" => [
                               [
                                 "terms" => [ 
                                    "Categories" => ['Featured Works']
                                 ]
                               ],
                               [
                                 "terms" => [
                                    "Tags" => ['Merida']
                                 ]
                               ]
                            ]