Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ElasticSearch – Elastic\Elasticsearch\Exception\ClientResponseException

Ive implemented ElasticSearch in our organization application and all went well so far, but when I tried to add conditions to the query it started throw 400s on almost everything I try following their documentation.

My struggle is to query for only products with status ‘active’.

Here is a code snippet.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

       $products = $this->elasticsearch->search([
            'index' => $product->getSearchIndex(),
            'type' => $product->getSearchType(),
            'body' => [
                '_source' => ['category_id', 'title', 'status'],
                'size' => 30,
                'query' => [
                    'bool' => [
                        'must' => [
                            'multi_match' => [ // ^1 means that title field has a higher priority in search tree
                                'fields' => ['title^1'],
                                'type' => 'phrase_prefix',
                                'query' => $query,
                            ],
                            'bool' => [
                                'must' => [
                                    [
                                        'term' => ['status' => ProductStatus::ACTIVE]
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);

Here is the full exception text I get from elastic using this piece of code above.

400 Bad Request: {"error":{"root_cause":[{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":170}],"type":"x_content_parse_exception","reason":"[1:170] [bool] failed to parse field [must]","caused_by":{"type":"parsing_exception","reason":"[multi_match] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":170}},"status":400}

I tried using filters and so forth, but none of the instructions in docs kind of lead me to success.

What am I missing?

>Solution :

Your multi_match simply needs to be in its own array element

            'query' => [
                'bool' => [
                    'must' => [
                      [
                        'multi_match' => [ // ^1 means that title field has a higher priority in search tree
                            'fields' => ['title^1'],
                            'type' => 'phrase_prefix',
                            'query' => $query,
                        ]
                      ],
                      [
                         'term' => ['status' => ProductStatus::ACTIVE]
                      ],
                    ],
                ],
            ],

Also it would be more efficient to do it this way by moving the constraint on status in bool/filter:

            'query' => [
                'bool' => [
                    'must' => [
                      [
                        'multi_match' => [ // ^1 means that title field has a higher priority in search tree
                            'fields' => ['title^1'],
                            'type' => 'phrase_prefix',
                            'query' => $query,
                        ]
                      ]
                    ],
                    'filter' => [
                      [
                         'term' => ['status' => ProductStatus::ACTIVE]
                      ],
                    ],
                ],
            ],
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading