开发者问题收集

elasticsearch 7,按整数值提升

2019-09-26
400

我尝试通过“created”字段(整数/时间戳)来加速搜索,但总是遇到

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

没有“脚本”,查询可以正常工作。但我不知道如何正确编写此脚本。有什么想法吗?

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

        ];

编辑:更新查询,但仍然遇到“{"error":{"root_cause":[{"type":"parsing_exception","re​​ason":[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171}],"type":"parsing_exception","re​​ason":[query_string] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":171},"status":400}"

2个回答

脚本不是独立属性。它应该是布尔值的一部分。当您有多个过滤器时,这些过滤器应该位于布尔值下的 must/should/filter 中

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

以上可能存在括号语法问题(我无法测试),查询结构正确

jaspreet chahal
2019-09-27
...

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

                        ],
                    ],
                ],

这是我最后的解决方案。遗憾的是后来在 elasticsearch 文档中发现。但你真的必须强烈划分时间戳,这样它才不会完全压倒最佳匹配。

bibamann
2019-10-02