elasticsearch function_score:将缺失字段视为完美命中

ep6jt1vc  于 5个月前  发布在  ElasticSearch
关注(0)|答案(2)|浏览(43)

我需要做的是按位置提升文档(越近越好)。locations是嵌套类型。
工作正常除了如果文档中缺少locations,Elasticsearch不会返回文档。如果缺少字段,Elasticsearch应该将文档视为完美命中。如何实现这一点?
我的查询:

{
   "sort": [
      {
         "_score": "desc"
      }
   ],
   "query": {
      "function_score": {
         "query": {
            "bool": {
               "must_not": [],
               "should": [
                  {
                     "nested": {
                        "path": "locations",
                        "query": {
                           "function_score": {
                              "score_mode": "sum",
                              "functions": [
                                 {
                                    "gauss": {
                                       "locations.coordinates": {
                                          "origin": {
                                             "lat": "50.1078852",
                                             "lon": "15.0385376"
                                          },
                                          "scale": "100km",
                                          "offset": "20km",
                                          "decay": "0.5"
                                       }
                                    }
                                 }
                              ]
                           }
                        }
                     }
                  }
               ]
            }
         }
      }
   }
}

字符串
顺便说一句:我正在使用Elasticsearch 5.0

krugob8w

krugob8w1#

在functions部分中添加一个function,并使用高提升(如1000)来弥补缺失的locations,类似于以下内容:

{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "locations"
                }
            }
        },
    },
    "weight": 1000
}

字符串
因此,缺少locations的记录将首先出现,因为权重很高。
这可能略有不同。有关查询的更多信息,请单击此处:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

icnyk63a

icnyk63a2#

使用过滤器内部函数

POST loc/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": {
            "query": "McDonald's"
          }
        }
      },
      "functions": [
        {
          "filter": {
            "exists": {
              "field": "location"
            }
          },
          "gauss": {
            "location": {
              "origin": {
               "lat" : -23.553720  ,"lon" : -46.652940
              },
              "offset": "200m",
              "scale": "20000km",
              "decay": "0.5"
            }
          },
          "weight": 100
        }
      ],
      "boost_mode": "replace"
    }
  }
}```

字符串

相关问题