elasticsearch 经纬度查询

本文介绍如何使用Elasticsearch进行地理定位搜索,包括设置经纬度字段、索引配置及查询示例。通过不同类型的查询,如过滤指定距离内的地点,并结合评分函数实现更精细的搜索结果排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
curl -XPUT "http://localhost:9200/shop/" -d '{
  "mappings": {
    "shop": {
      "properties": {
        "name": {
          "type""string"
        },
        "location": {
          "type""geo_point",//经纬度类型
"lat_lon"true,
"fielddata": {
      "format":    "compressed"//压缩模式,节省内存
      "precision""3m"
    }
        }
      }
    }
  }
}';
 
 
curl -XPUT http://localhost:9200/shop/shop/1 -d'
{"name""北京""location" "39.9047253699,116.4072154982"}
';
 
curl -XPUT http://localhost:9200/shop/shop/2 -d'
{"name""顺义""location" "40.1299127031,116.6569478577"}
';
 
curl -XPUT http://localhost:9200/shop/shop/3 -d'
{"name""天津""location" "39.0850853357,117.1993482089"}
';
 
curl -XPUT http://localhost:9200/shop/shop/4 -d'
{"name""上海""location" "31.2304324029,121.4737919321"}
';
 
 
curl -XGET "http://localhost:9200/shop/shop/_search?pretty" -d '{
  "query": {
    "filtered": {
      "filter": {
        "geo_distance": {
          "distance""28km",
          "type":    "indexed",
          "distance_type""sloppy_arc",
          "location": { 
            "lat":  39.9682060617,
            "lon":  116.4107280170
          }
        }
      }
    }
  },
  "sort": [     //按距离排序
    {
      "_geo_distance": {
        "location": { 
          "lat":  39.9682060617,
          "lon":  116.4107280170
        },
        "order":         "asc",
        "unit":          "km"
        "distance_type""sloppy_arc" //推荐适应此模式 plane 不准,精度太差
      }
    }
  ]
}'
 
 
再举一个更加实际的例子
 
curl -XGET "http://localhost:9200/shop/shop/_search?pretty" -d '{
  "query" : {
    "function_score" : {
      "query" : {
        "bool" : {
          "filter" : {
            "geo_distance": {
            "distance""28km",
            "type":    "indexed",
            "distance_type""sloppy_arc",
            "location": { 
            "lat":  39.9682060617,
            "lon":  116.4107280170
}
            }
          }
        }
      },
      "functions" : [ {
        "script_score" : {
          "script" : {
            "inline" "return 0"
          }
        }
      },{
         "gauss": {   //按举例远近打分
                  "location": {
                     "origin""39.9682060617,116.4107280170",
                     "scale""5km",
                     "offset""0",
                     "decay"0.5
                  }
               },
               "weight""1"
      } ],
      "score_mode" "sum",
      "boost_mode" "replace"
    }
  }
}'
 
此时你会看到北京的得分是0.25分,因为差5公里,衰减0.5, 北京距此坐标7公里,所以取值0.25.
 
 
2.3 版本的mapping 可以写成
 
curl -XPUT "http://localhost:9200/shop/" -d '{
  "mappings": {
    "shop": {
      "properties": {
        "name": {
          "type""string"
        },
        "location": {
          "type""geo_point",
"lat_lon"true//倒排索引
"geohash"true,
"geohash_prefix":     true
"geohash_precision":  "50m" 
        }
      }
    }
  }
}';
 
其实它还支持多个地址,例如
curl -XPUT http://localhost:9200/ext-shop/ext-shop/5 -d'
{"name""京津冀""location" : [[116.4072154982,39.9047253699],[116.6569478577,40.1299127031]]}
';
 
佩服ES的强大



本文转自whk66668888 51CTO博客,原文链接:http://blog.51cto.com/12597095/2048249


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值