0

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}"

4
  • Script should be part of query, sibling of query sting Commented Sep 26, 2019 at 10:11
  • 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} Commented Sep 26, 2019 at 10:25
  • 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} @Rob Commented Sep 26, 2019 at 10:30
  • 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}] Commented Sep 26, 2019 at 10:37

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0
...

'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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.