【问题标题】:elasticsearch 7, boost by integer valueelasticsearch 7,按整数值提升
【发布时间】:2019-09-26 10:07:44
【问题描述】:

我正在尝试通过“创建”字段(整数/时间戳)来增加搜索,但总是遇到

"{"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”,“reason”:“[query_string] 格式错误的查询,应为 [END_OBJECT] 但发现[FIELD_NAME]","line":1,"col":171}],"type":"parsing_exception","re​​ason":"[query_string] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]"," line":1,"col":171},"status":400}"

【问题讨论】:

  • 脚本应该是查询的一部分,是查询字符串的兄弟
  • 然后我得到 {"error":{"root_cause":[{"type":"parsing_exception","re​​ason":"[query_string] 未知令牌 [START_OBJECT] after [script]", "line":1,"col":179}],"type":"parsing_exception","re​​ason":"[query_string] 未知标记 [START_OBJECT] after [script]","line":1,"col" :179},"状态":400}
  • 或 {"error":{"root_cause":[{"type":"parsing_exception","re​​ason":"[query_string] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]" ,"line":1,"col":171}],"type":"parsing_exception","re​​ason":"[query_string] 查询格式错误,应为 [END_OBJECT] 但找到 [FIELD_NAME]","line":1 ,"col":171},"status":400} @Rob
  • 脚本的格式应该如下 "bool": { "must": [ { "script": { "script": "_score * doc['f'].value" } }, { "query_string": { "default_field": "FIELD", "query": "this AND that OR such" } } ] } , bool>must:[{script},{querystring}]

标签: elasticsearch


【解决方案1】:

脚本不是一个独立的属性。它应该是 bool 的一部分。当您有多个过滤器时,它们应该在 bool 下的 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"
                                ]
                              ]
                          ]
                      ]
                ]
            ]

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

【讨论】:

    【解决方案2】:
    ...
    
    '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 的文档中发现。但是你真的必须强烈划分时间戳,它不会完全压倒最佳匹配。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      相关资源
      最近更新 更多