1
votes

The query returns this error.

Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":{"root_cause":[{"type":"parsing_exception","reason":"unknown query [query]","line":1,"col":62}],"type":"x_content_parse_exception","reason":"[1:62] [bool] failed to parse field [should]","caused_by":{"type":"parsing_exception","reason":"unknown query [query]","line":1,"col":62,"caused_by":{"type":"named_object_not_found_exception","reason":"[1:62] unknown field [query]"}}},"status":400} in

How can i write this query ? . In array first one is (manufacturer_code,ean) parent, other objects are nested.

$query = [
            'bool' => [
                'should' => [
                    ['query' => [   //parent root
                        'query_string' => [
                            'fields' => ['manufacturer_code', 'ean'],
                            'query'  =>  $search,
                        ],
                    ],
                    ],

                    ['nested' => [   //nested parent.lang
                        'path'  => 'lang',
                        'query' => [
                            'query_string' => [
                                'fields' => 'lang.name',
                                'query'         =>  $search ,
                            ],
                        ],
                    ]],
                    ['nested' => [  //nested parent.product_base.lang
                        'path'  => 'product_base.lang',
                        'query' => [
                            'query_string' => [
                                'fields' => 'product_base.lang.meta_keywords',
                                'query'         =>  $search ,
                            ],
                        ],
                    ]],
                ],
                'minimum_should_match' => 1,
            ],

        ];
1

1 Answers

1
votes

You're almost there, in the first should clause you need to remove the first query, i.e. query_string should be at top level

$query = [
            'bool' => [
                'should' => [
                    ['query_string' => [ //parent root     <-- modify this clause
                            'fields' => ['manufacturer_code', 'ean'],
                            'query'  =>  $search,
                        ],
                    ],

                    ['nested' => [   //nested parent.lang
                        'path'  => 'lang',
                        'query' => [
                            'query_string' => [
                                'fields' => 'lang.name',
                                'query'         =>  $search ,
                            ],
                        ],
                    ]],
                    ['nested' => [  //nested parent.product_base.lang
                        'path'  => 'product_base.lang',
                        'query' => [
                            'query_string' => [
                                'fields' => 'product_base.lang.meta_keywords',
                                'query'         =>  $search ,
                            ],
                        ],
                    ]],
                ],
                'minimum_should_match' => 1,
            ],

        ];