【问题标题】:Elasticsearch Function Score Query with Should Clause in Bool QueryBool 查询中带有 should 子句的 Elasticsearch 函数评分查询
【发布时间】:2018-08-16 00:39:50
【问题描述】:

这是一个包含 2 个查询的故事。一个返回结果,而另一个不返回。为什么?

返回结果的查询:

{
   "index":"my_index",
   "type":"places",
   "body":{
      "from":0,
      "size":"12",
      "sort":{
         "_score":{
            "order":"desc"
         }
      },
      "query":{
         "function_score":{
            "query":{
               "bool":{
                  "must":[

                  ],
                  "should":[

                  ],
                  "must_not":[

                  ],
                  "filter":[

                  ]
               }
            },
            "functions":[
               {
                  "gauss":{
                     "location":{
                        "origin":{
                           "lat":"41.243368",
                           "lon":"-116.79711"
                        },
                        "offset":"0mi",
                        "scale":"100mi"
                     }
                  }
               }
            ],
            "score_mode":"sum",
            "boost_mode":"sum"
         }
      }
   }
}

不返回结果的查询:

{
   "index":"my_index",
   "type":"places",
   "body":{
      "from":0,
      "size":"12",
      "sort":{
         "_score":{
            "order":"desc"
         }
      },
      "query":{
         "function_score":{
            "query":{
               "bool":{
                  "must":[

                  ],
                  "should":[
                     {
                        "terms":{
                           "region_id":[
                              32273
                           ],
                           "boost":2
                        }
                     }
                  ],
                  "must_not":[

                  ],
                  "filter":[

                  ]
               }
            },
            "functions":[
               {
                  "gauss":{
                     "location":{
                        "origin":{
                           "lat":"41.243368",
                           "lon":"-116.79711"
                        },
                        "offset":"0mi",
                        "scale":"100mi"
                     }
                  }
               }
            ],
            "score_mode":"sum",
            "boost_mode":"sum"
         }
      }
   }
}

布尔查询只有一个应该从句。

另外,我将分数相加,而不是相乘。

确实,我在索引中没有任何具有指定“region_id”的项目,但是我看不到第一个查询的结果是如何从第二个查询中排除的。 Bool Query 上的 0 分似乎就像一个过滤器。

【问题讨论】:

  • 您有 region_id = 32273 的文档吗?

标签: elasticsearch


【解决方案1】:

第一个查询充当match_all 查询,因此它返回所有内容并应用函数分数。 在第二个查询中,您有一个 should 子句,但由于没有 mustfilter 子句,它必须匹配。

来自 ES 文档:“在没有 must 或 filter 子句的布尔查询中,一个或多个 should 子句必须与文档匹配”

【讨论】:

  • 值得注意的是,可以通过指定minimum_should_match来调整行为
  • @Olivier。在这个例子中,我没有任何运气设置 minimum_should_match=0。
猜你喜欢
  • 2016-06-06
  • 2015-04-30
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
相关资源
最近更新 更多