0
votes

I'm trying to boost a search by the "created" field (an integer / timestamp) but always run into

"{"error":{"root_cause":[{"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [script].","line":1,"col":181}],"type":"parsing_exception","reason":"Unknown key for a START_OBJECT in [script].","line":1,"col":181},"status":400}"

Without the 'script' the query works fine. But I'm running out of ideas how to write this script correctly. Any ideas?

 return [
            'index' => 'articles_' . $this->system,
            'body'  => [
                'size' => $this->size,
                'from' => $this->start,

                'sort'  => [
                    $this->order => 'desc',
                ],
                'query' => [
                    'query_string' => [
                        'query'            => $this->term,
                        'fields'           => ['title^5', 'caption^3', 'teaser^2', 'content'],
                        'analyze_wildcard' => true,
                    ],
                    'script' => [
                        'script' => [
                            'lang' => 'painless',
                            'source' => "doc['@created'].value / 100000",
                        ],
                    ],
                ],
            ],

        ];

EDIT: Updated query, but still running into "{"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171},"status":400}"

2
Script should be part of query, sibling of query stingjaspreet chahal
Then I get {"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] unknown token [START_OBJECT] after [script]","line":1,"col":179}],"type":"parsing_exception","reason":"[query_string] unknown token [START_OBJECT] after [script]","line":1,"col":179},"status":400}bibamann
Or {"error":{"root_cause":[{"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171}],"type":"parsing_exception","reason":"[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171},"status":400} @Robbibamann
format of script should be like below "bool": { "must": [ { "script": { "script": "_score * doc['f'].value" } }, { "query_string": { "default_field": "FIELD", "query": "this AND that OR thus" } } ] } , bool>must:[{script},{querystring}]jaspreet chahal

2 Answers

0
votes

Script is not a standalone attribute. It should be part of bool. When you have multiple filters these should be in must/should/filter under bool

'body'  => [
                'size' => $this->size,
                'from' => $this->start,

                'sort'  => [
                    $this->order => 'desc'
                ],
                'query' => [
                      'bool' => [
                          'must' =>[
                              'query_string' => [
                                'query'    => $this->term,
                                'fields'   => ['title^5', 'caption^3',                        'teaser^2', 'content'],
                                'analyze_wildcard' => true
                              ],
                             'script' => [
                                'script' => [
                                  'lang' => 'painless',
                                  'source' => "doc['@created'].value / 100000"
                                ]
                              ]
                          ]
                      ]
                ]
            ]

Above can have syntax issue of brackets(I couldn't test it) , query structure is correct

0
votes
...

'query' => [
                    'function_score' => [
                        'query' => [
                            'query_string' => [
                                'query'            => $this->term,
                                'fields'           => ['title^10', 'caption^8', 'teaser^5', 'content'],
                                'analyze_wildcard' => true,
                            ],
                        ],

                        'script_score' => [
                            'script' => [
                                'lang'   => 'expression',
                                'source' => "_score + (doc['created'] / 10000000000000)",
                            ],

                        ],
                    ],
                ],

Was my solution at the end. Sadly found at the documentation of elasticsearch later. But you really have to divide the timestamp strongly that it doesn't totally overpower the best matches.