【问题标题】:Elasticsearch - [function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]Elasticsearch - [function_score] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]
【发布时间】:2022-10-25 12:51:52
【问题描述】:

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

任何帮助深表感谢

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    我找到了解决方案。这是有效的完整查询。我希望它可以帮助别人。

                        'query' => [
                            'function_score' => [
                                'query' => [
                                    'bool' => [
                                        'filter' => [
                                            ['range' => [
                                                'starttime' => ['lte' => $now],
                                            ]],
                                        ],
                                        'should' => [
                                            ['multi_match' => [
                                                'query' => $_REQUEST['kw'],
                                                'fields' => [
                                                    'content^2',
                                                    'teaser^2',
                                                    'bodytext^2',
                                                    'title^5',
                                                    'header^3'
                                                ],
                                                'type'=>'best_fields',
                                                'boost' => 3,
                                                'operator' => 'and'
                                            ]],
                                            ['multi_match' => [
                                                'query' => $_REQUEST['kw'],
                                                'fields' => [
                                                    'content^2',
                                                    'teaser^2',
                                                    'bodytext^2',
                                                    'title^5',
                                                    'header^3'
                                                ],
                                                'type'=>'best_fields',
                                                'boost' => 2
                                            ]],
                                            ['multi_match' => [
                                                'fuzziness' =>  'auto',
                                                'query' => $_REQUEST['kw'],
                                                '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'
                            ],
                        ],
    

    【讨论】:

      猜你喜欢
      • 2017-12-01
      • 1970-01-01
      • 2018-01-30
      • 2018-02-10
      • 2018-04-25
      • 2021-04-22
      • 2020-02-26
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多