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

问题描述 投票:0回答:2

我需要做的是按位置增强文档(越近越好)。

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

elasticsearch elasticsearch-5
2个回答
1
投票

在功能部分添加一个

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


0
投票

在函数内部使用过滤器

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"
    }
  }
}```
© www.soinside.com 2019 - 2024. All rights reserved.