Elasticsearch 7.x 小白到高手

elasticsearch 7.x Distance Feat

2019-04-25  本文已影响41人  郭彦超

通过距离函数可以在空间和时间上动态提升距离目标数据较近的文档score,主要应用场景有附件好友查询、附近商圈、节日营销推送等。

1、介绍

es 提供的这个distance_feature query主要是针对date、date_nanos、geo_point数据类型进行操作的。和function_score相比,这种查询在没有把track_total_hits设置为true的情况下,可以有效的跳过无效的计算数据,提升检索性能。

2、操作

参数 说明
field 指定查询字段
origin 目标点,必须和field中的数据类型一致,也可以是es支持的日期函数
pivot score 分割点,distance小于pivot时score值>0.5;反之,<0.5
boost 权重
score = boost * pivot / (pivot + distance)
curl -X PUT "localhost:9200/items" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "production_date": {
        "type": "date"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}
'
curl -X PUT "localhost:9200/items/_doc/1" -H 'Content-Type: application/json' -d'
{
  "name" : "chocolate",
  "production_date": "2018-02-01",
  "location": [-71.34, 41.12]
}
'
curl -X PUT "localhost:9200/items/_doc/2" -H 'Content-Type: application/json' -d'
{
  "name" : "chocolate",
  "production_date": "2018-01-01",
  "location": [-71.3, 41.15]
}
'
curl -X PUT "localhost:9200/items/_doc/3" -H 'Content-Type: application/json' -d'
{
  "name" : "chocolate",
  "production_date": "2017-12-01",
  "location": [-71.3, 41.12]
}
'
curl -X POST "localhost:9200/items/_refresh"

基于时间距离查询,有效提升距离当前时间点不足7天的数据:

curl -X GET "localhost:9200/items/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "chocolate"
        }
      },
      "should": {
        "distance_feature": {
          "field": "production_date",
          "pivot": "7d",
          "origin": "now"
        }
      }
    }
  }
}
'

基于空间位置查询,有效提升距离[-71.3, 41.15]点不足1000m的数据:

curl -X GET "localhost:9200/items/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "chocolate"
        }
      },
      "should": {
        "distance_feature": {
          "field": "location",
          "pivot": "1000m",
          "origin": [-71.3, 41.15]
        }
      }
    }
  }
}
'

上一篇下一篇

猜你喜欢

热点阅读