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

gfttwv5a  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(190)

我在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]]
        ],
    ]
],

任何帮助都是非常感谢的

juzqafwq

juzqafwq1#

我找到了解决方案。下面是有效的完整查询。我希望它能帮助其他人:
“查询”部分属于“函数_分数”:

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

相关问题