ElasticSearch function_score 带过滤器的查询

ElasticSearch function_score query with filters

正在尝试创建一个搜索,以返回距特定地理位置点约 500 米的位置结果。

我需要根据位置在源中是否为空来过滤此搜索的结果。

我试过这样的事情:

"filtered" : {
    "filter": {
        "missing" : { "field" : "location" }
    }
}

这是搜索JSON我得到:

 {"query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {"fieldA": "value"}
                        },
                        {
                            "match": { "fieldB": "value"}
                        }
                    ]
                }
            },
            "functions": [{
                    "gauss": {
                        "location": {
                            "origin": "'.$lat.','.$lon.'",
                            "scale": "500m",
                            "offset": "0km",
                            "decay": 0.33
                        }
                    }
                }]
        }
    }
}

我尝试将过滤器放在查询中的不同位置,但它对我不起作用,所以我知道我在查询的结构方面做了一些根本性的错误。将来我想添加更多评分逻辑和其他过滤器,但找不到此类查询的好例子。

我应该怎么做才能让它发挥作用?

您可以使用带有 geo_distance 的筛选查询来先筛选出结果。您还可以在 function_score 中使用 "weight" 来显式增加 "match" 查询的距离:

{
   "query": {
      "function_score": {
         "query": {
            "filtered": {
               "query": {
                  "bool": {
                     "must": [
                        {
                           "match": {
                              "fieldA": "value"
                           }
                        },
                        {
                           "match": {
                              "fieldB": "value"
                           }
                        }
                     ]
                  }
               },
               "filter": {
                  "geo_distance" : {
                        "distance" : "500m",
                        "location" : "'.$lat.','.$lon.'"
                    }
               }
            }
         },
         "functions": [
            {
               "gauss": {
                  "location": {
                     "origin": "'.$lat.','.$lon.'",
                     "scale": "500m",
                     "offset": "0km",
                     "decay": 0.33
                  }
               },
               "weight": "3"
            }
         ]
      }
   }
}