0

我需要做的是按位置提升文档(越近越好)。locations是嵌套类型。

工作正常,如果文档中缺少 Elasticsearch 不返回locations文档。如果缺少字段,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

4

1 回答 1

1

在功能部分中再添加一个function,对于 missing 具有高提升(如 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

于 2017-02-17T10:54:42.397 回答