首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Elasticsearch - [function_score]格式错误的查询,期望[END_OBJECT]但找到[FIELD_NAME]

Elasticsearch - [function_score]格式错误的查询,期望[END_OBJECT]但找到[FIELD_NAME]
EN

Stack Overflow用户
提问于 2022-10-20 13:27:10
回答 1查看 59关注 0票数 0

我在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非常陌生。

错误

代码语言:javascript
运行
复制
"type":"parsing_exception",
"reason":"[function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]"

新部件

代码语言:javascript
运行
复制
 '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'
],

全文查询

代码语言:javascript
运行
复制
'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]]
        ],
    ]
],

任何帮助都非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-25 04:51:52

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

“查询”部分属于"function_score":

代码语言:javascript
运行
复制
'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'
    ],
],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74140689

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档