Elasticsearch-[function_score]查询格式错误,应为[END_OBJECT],但找到[FIEL



我在PHP中有一个查询已经在运行,现在我想用function_score扩展它。目标是我可以根据时间戳来提升最近的内容。

我找到了这篇文章https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100也在读这篇论文https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html.

我想这是把新零件放错地方了,但我不知道该放在哪里。我对Elasticsearch很陌生。

错误

"type":"parsing_exception",
"reason":"[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"

新部件

'function_score' => [
'functions' => [
[
'filter'=> [
'range' => [
'tstamp' => [
'gte' => 'now-1y',
'lte'  => 'now'
]
]
],
'weight' => 5
],
[
'filter' => [
'range' => [
'tstamp' => [
'gte' => 'now-3yr',
'lte' => 'now-1yr'
]
]
],
'weight' => 2
]
],
'boost_mode' => 'multiply'
],

完整查询

'query' => [
'function_score' => [
'functions' => [
[
'filter'=> [
'range' => [
'tstamp' => [
'gte' => 'now-1y',
'lte'  => 'now'
]
]
],
'weight' => 5
],
[
'filter' => [
'range' => [
'tstamp' => [
'gte' => 'now-3yr',
'lte' => 'now-1yr'
]
]
],
'weight' => 2
]
],
'boost_mode' => 'multiply'
],
'bool' => [
'filter' => [
['range' => [
'starttime' => ['lte' => $now],
]]
],
'must' => [
["multi_match" => [
'fuzziness' =>  'auto',
'query' => $_REQUEST['kw'],
'fields' => [
'content^2',
'teaser^2',
'bodytext^2',
'title^5',
'header^3'
],
]],
['bool' => [
'should' => [
['match' => ['sys_language_uid' =>  $sysLanguageUid]],
['match' => ['sys_language_uid' => -1]],
],
'minimum_should_match' => 1,
]],
],
'must_not' => [
['match' => ['hidden' => 1]],
['match' => ['deleted' => 1]],
['match' => ['no_search' => 1]]
],
]
],

非常感谢任何帮助

我找到了解决方案。以下是有效的完整查询。我希望它能帮助其他人:

";查询";部分属于";function_score":

'query' => [
'function_score' => [
'query' => [
'bool' => [
'filter' => [
['range' => [
'starttime' => ['lte' => $now],
]],
],
'should' => [
['multi_match' => [
'query' => $keyword,
'fields' => [
'content^2',
'teaser^2',
'bodytext^2',
'title^5',
'header^3'
],
'type'=>'best_fields',
'boost' => 3,
'operator' => 'and'
]],
['multi_match' => [
'query' => $keyword,
'fields' => [
'content^2',
'teaser^2',
'bodytext^2',
'title^5',
'header^3'
],
'type'=>'best_fields',
'boost' => 2
]],
['multi_match' => [
'fuzziness' =>  'auto',
'query' => $keyword,
'fields' => [
'content^2',
'teaser^2',
'bodytext^2',
'title^5',
'header^3'
],
]],
],
'must' => [
['bool' => [
'should' => [
['match' => ['sys_language_uid' =>  $sysLanguageUid]],
['match' => ['sys_language_uid' => -1]],
],
'minimum_should_match' => 1,
]],
],
'must_not' => [
['match' => ['hidden' => 1]],
['match' => ['deleted' => 1]],
['match' => ['no_search' => 1]]
],
]
],
'functions' => [
[
'filter'=> [
'range' => [
'tstamp' => [
'gte' => $now - 31556952,
'lte'  => $now
]
]
],
'weight' => 3
],
[
'filter' => [
'range' => [
'tstamp' => [
'gte' => $now - 94670856,
'lte' => $now - 31556952
]
]
],
'weight' => 2
]
],
'boost_mode' => 'multiply'
],
],