我遇到了困难,将一个multi_match查询与2个过滤器组合在一起。
我正在使用Elastic7.17并使用PHP指定查询。
错误如下:
{
"error": {
"root_cause": [{
"type": "parsing_exception",
"reason": "[term] 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 [filter]",
"caused_by": {
"type": "parsing_exception",
"reason": "[term] malformed query, expected [END_OBJECT] but found [FIELD_NAME]",
"line": 1,
"col": 170
}
},
"status": 400
}解析器可以单独处理每个过滤器,如果我注释掉一个过滤器,另一个过滤器工作正常,但是一旦两个过滤器都没有注释,我就会得到一个错误。
这大概意味着我定义多个过滤器的方式才是问题所在?
然而,我正在构建的查询与一个官方的弹性示例几乎完全相同。
https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-filter-context.html
我的疑问是:
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => $keywords,
'fields' => [
'title^4',
'subtitle^4',
]
],
],
'filter' => [
'term' => [
'status' => 'live'
],
'range' => [
'published_date' => [
'lte' => '2022-11-01'
]
]
]
]
]发布于 2022-08-11 23:26:11
之所以失败,是因为我将过滤器表示为PHP数组。为了将其转换为JSON数组,PHP需要数组中的数组。
添加额外的方括号将其转换为过滤器数组。
'query' => [
'bool' => [
'must' => [
'multi_match' => [
'query' => $keywords,
'fields' => [
'title^4',
'subtitle^4',
]
],
],
'filter' => [
[
'term' => [
'status' => 'live',
],
],
[
'range' => [
'published_date' => [
'lte' => '2022-11-01'
]
]
]
]
]
]
]
];在json编码过程中使用JSON_PRETTY_PRINT常量有助于调试。
$json = json_encode($query, JSON_PRETTY_PRINT);https://stackoverflow.com/questions/73321127
复制相似问题