ElasticSearch并在PHP中搜索多个字段 [英] ElasticSearch and searching on multiple fields in PHP
问题描述
我正在使用 elasticsearch-php 的最新版本以及MongoDB的最新版本和ElasticSearch.
I am using the latest version of elasticsearch-php as well as the latest version of MongoDB and ElasticSearch.
我需要对可以包含一个或多个值的多个字段进行搜索.示例:
I need to do a search on multiple fields that can contain one or multiple values. Example:
国家/地区代码应为NL,BE或DE 和 类别应包含AA01,BB01,CC02或ZZ11
country_code should be either NL, BE or DE AND category should contain be AA01, BB01, CC02 or ZZ11
我认为我可以按以下方法解决此问题(PHP):
I thought I would solve it as followed (PHP):
$countries = array("NL", "BE", "DE");
$category = array("AA01", "BB01", "CC02", "ZZ11");
$searchParams['body']['query']['bool']['must']['terms']['country'] = $countries;
$searchParams['body']['query']['bool']['must']['terms']['categories'] = $category;
$searchParams['body']['query']['bool']['must']['terms']['minimum_should_match'] = 1;
但是结果甚至还没有接近我希望返回的数据.
But the result does not even come close the the data that I expect to get back.
有时候 $ countries 和/或 $ category 只能有一个元素.
Sometimes $countries and/or $category can only have one element.
推荐答案
这是因为PHP数组如何工作,您每次都覆盖terms
查询,而是尝试以下方式:
It is becaue of how PHP arrays work, you are overwriting the terms
query each time, instead try something along the lines of:
array(
'body' => array('query' =>
'bool' => array(
'must' => array(
array('terms' => array('country' => implode(' ', $countries))),
array('terms' => array('category' => implode(' ', $category))),
)
)
))
minimum_should_match
与查询的must
子句没用.
这篇关于ElasticSearch并在PHP中搜索多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!