3

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:

country_code should be either NL, BE or DE AND category should contain be AA01, BB01, CC02 or ZZ11

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.

Sometimes $countries and/or $category can only have one element.

1
  • That is because of how arrays in PHP work, you are overwriting the terms key each time Commented Dec 28, 2013 at 19:45

1 Answer 1

5

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 is useless with must clause of the query.

Sign up to request clarification or add additional context in comments.

4 Comments

"you are overwriting the terms query each time" - so obvious. Thanks @Sammaye.
As a side note This: array('country' => implode(' ', $countries)) does not seem to work, but array('country' => $countries) goes fine.
Is it possible to specify an array there without imploding it ?
@HappyCoder I don' believe so, it was one of the things that confused me about ES: that you culd not query by tjhe structure of it's own schema, among other things

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.