Elasticsearch查询距离内的文档(geo_distance)

在下面附录一中,创建了 es_shop 索引,并添加了一些文档数据。其中 location 字段为 gps_point 类型,表示一个定位点。

下面查询示例,用于匹配距离(120.28,36.27)定位点 5km 内的文档,查询结果按距离由近到远排序:

GET /es_shop/_search
{
  "query": {
    "bool": {
      "must": {
          "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "5km",
          "location": {
            "lat": 36.27,
            "lon": 120.28
          }
        }
      }
    }
  },
  "sort": [
   {
     "_geo_distance": {
       "location": {
         "lat": 36.27,
         "lon": 120.28
       },
       "order": "asc",
       "unit": "km"
     }
   }
 ]
}

输出结果:其中 sort 为该文档与中心点的距离

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "es_shop",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : null,
        "_source" : {
          "id" : 1,
          "name" : "海尔国际广场",
          "location" : {
            "lat" : 36.26,
            "lon" : 120.27
          }
        },
        "sort" : [
          1.4283782078384288
        ]
      },
      {
        "_index" : "es_shop",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : null,
        "_source" : {
          "id" : 2,
          "name" : "东唐影视创意园",
          "location" : {
            "lat" : 36.26,
            "lon" : 120.26
          }
        },
        "sort" : [
          2.109902342826878
        ]
      },
      {
        "_index" : "es_shop",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : null,
        "_source" : {
          "id" : 6,
          "name" : "青岛伊甸园",
          "location" : {
            "lat" : 36.26,
            "lon" : 120.31
          }
        },
        "sort" : [
          2.910449792316815
        ]
      },
      {
        "_index" : "es_shop",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : null,
        "_source" : {
          "id" : 5,
          "name" : "海尔生物医疗新兴产业园",
          "location" : {
            "lat" : 36.29,
            "lon" : 120.32
          }
        },
        "sort" : [
          4.219210926197896
        ]
      }
    ]
  }
}

附录

附录一:es_shop 索引

添加 es_shop 索引映射

PUT es_shop
{
  "mappings": {
    "properties": {
      "id": {
        "type": "long"
      },
      "name": {
        "type": "text"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

添加 es_shop 索引文档

POST /es_shop/_doc/1
{
  "id": 1,
  "name": "海尔国际广场",
  "location": {
    "lat": 36.26,
    "lon": 120.27
  }
}


POST /es_shop/_doc/2
{
  "id": 2,
  "name": "东唐影视创意园",
  "location": {
    "lat": 36.26,
    "lon": 120.26
  }
}

POST /es_shop/_doc/3
{
  "id": 3,
  "name": "青岛方特梦想王国",
  "location": {
    "lat": 36.21,
    "lon": 120.28
  }
}

POST /es_shop/_doc/4
{
  "id": 4,
  "name": "青岛胶东国际机场",
  "location": {
    "lat": 36.36,
    "lon": 120.09
  }
}

POST /es_shop/_doc/5
{
  "id": 5,
  "name": "海尔生物医疗新兴产业园",
  "location": {
    "lat": 36.29,
    "lon": 120.32
  }
}

POST /es_shop/_doc/6
{
  "id": 6,
  "name": "青岛伊甸园",
  "location": {
    "lat": 36.26,
    "lon": 120.31
  }
}

你可能感兴趣的:(elasticsearch,搜索引擎,java)