Home > Net >  Elasticsearch - [function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]
Elasticsearch - [function_score] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

Time:10-25

I have a query in PHP that is already working and now i want to expand it with function_score. The goal is that i can boost more recent content based on a timestamp.

I found this article https://discuss.elastic.co/t/how-to-prioritize-more-recent-content/134100 and was also reading this doc https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html.

I guess its some kind of misplacement the new part but i can't figure out where to put it. Im pretty new to Elasticsearch.

The error

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

The new part

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

The full query

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

Any help is much appreciated

CodePudding user response:

I found the solution. Here is the full query that works. I hope it helps someone else.

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