0

Does anyone know a good resource with elasticsearch-php examples ideally covering queries taking MySQL Examples. I am struggling both with the code syntax and what to use when.

For example, I want to do a search where $name must be part of field 'business' and where 'country' matches $country

$params = [
    'index' => 'xxxxx',
    'type' => 'zzzzz',
    'body' => [
        'from' => 0, 
        'size' => $maxResults, 
        'query' => [
            'bool' => [
                'must' => [
                    'match' => ['name' => $searchString],
                ],
                'must' => [
                    'match' => ['country' => $country],
                ],

            ],
        ],
    ],
];

The first 'must' seems to be completely ignored. Removing this will return exactly the same results.

I searched around for hours. There are plenty of quick beginner tutorials with simple search examples but I already get stuck one step further like with the above example

Thanks

2
  • The elasticsearch guides do have really good examples: elastic.co/guide/index.html. What we normally do is create a test in kibana, and use that json to create the php object. You can cast this object to json again and use that for your query. Commented Mar 4, 2020 at 8:59
  • 1
    Thank you, I have been reading more about this today. There a quite some examples but finding exactly what you are looking for is difficult. Testing json first will probably help and make things a little bit easier. Commented Mar 4, 2020 at 18:02

1 Answer 1

2

You can only have a single must in a bool query, then all must constraints must be elements of the must array. Try like this instead:

$params = [
    'index' => 'xxxxx',
    'type' => 'zzzzz',
    'body' => [
        'from' => 0, 
        'size' => $maxResults, 
        'query' => [
            'bool' => [
                'must' => [
                  [
                    'match' => ['name' => $searchString],
                  ],
                  [
                    'match' => ['country' => $country],
                  ],
                ]
            ],
        ],
    ],
];
Sign up to request clarification or add additional context in comments.

Comments

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.