ElasticSearch - Elastic\Elasticsearch\Exception\ClientResponseException

问题描述 投票:0回答:1

我已经在我们的组织应用程序中实现了 ElasticSearch,到目前为止一切都很顺利,但是当我尝试向查询添加条件时,它开始在我按照他们的文档尝试的几乎所有内容上抛出 400 秒。

我的困难是只查询状态为“活动”的产品。

这是一个代码片段。

       $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]
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
        ]);

这是我使用上面这段代码从 elastic 获得的完整异常文本。

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}

我尝试使用过滤器等,但文档中的任何说明都没有让我成功。

我错过了什么?

php laravel elasticsearch backend
1个回答
0
投票

您的

multi_match
只需要位于其自己的数组元素中

            '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]
                      ],
                    ],
                ],
            ],

此外,通过将

status
上的约束移动到
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]
                      ],
                    ],
                ],
            ],
© www.soinside.com 2019 - 2024. All rights reserved.