elasticsearch中的should+distance函数

vshtjzan  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(233)

我试图在elasticsearch中返回的值中对地理邻近性进行加权。我希望近距离比某些字段(如法律名称)更重要,但比其他字段更重要。
从文档来看,当前的方法似乎是使用distance\u特性。但是,我创建的should条件决不会以任何相关方式改变结果。事实上,如果我去掉must值,看起来分数是基于更远的距离。也就是说,分数越高,距离越远。当然,我想要的是用近距离来奖励比分。任何关于我做错了什么的建议。。。
(需要注意的是,“坐标”字段的类型为geoƏu point)
简化文档

{
_index: "organizations",
_type: "_doc",
_id: "3",
_version: 9,
_seq_no: 16944,
_primary_term: 5,
found: true,
_source: {
   id: 3,
   legal_name: "Air Canada",
   operating_name: "Air Canada",
   ...
   coordinate: "43.85133,-79.36572",
}
}

查询

{
    "from": 0,
    "size": 100,
    "query": {
        "bool": {
            "must": [{
                "multi_match": {
                    "query": "Air Canada",
                    "fields": ["legal_name^7","operating_name^7","interest_areas^4","city^3", "description","state","country"
                    ]
                }},
                {"term" : { "organization_type.keyword": "Sponsor" }},
                {"term" : { "approved" : true }}
            ],
            "should": {
              "distance_feature": {
                "field": "coordinate",
                "pivot": "25km",
                "origin": [43.63, -79.3716],
                "boost": 5.0
              }
            }
        }
    }
}```
5lhxktic

5lhxktic1#

最后,我放弃了距离的特征,而是使用了高斯函数。
{“from”:0,“size”:20,“query”:{

"function_score": {
  "query": {
    "bool": {
      "must": [{
        "multi_match": {
            "query": "national bank",
            "fields": ["legal_name^0.7", "operating_name^0.7", "interest_areas^0.4", "city^0.4", "description^0.4", "state^0.1", "country^0.1"]
        }},
        {"term" : { "organization_type.keyword": "Sponsor" }},
        {"term" : { "approved" : true }}
      ]
    }
  },
  "boost": "1",
  "boost_mode": "sum",
  "functions": [{
    "gauss": {
      "coordinate": {
        "origin": { "lat": 43.63, "lon": -79.3716 },
        "offset": "500km",
        "decay": 0.5,
        "scale": "100km"
      }
    },
    "weight": 1
  }]
}

} }`

相关问题