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:
{
"name"
:
"北京"
,
"location"
:
"39.9047253699,116.4072154982"
}
';
curl -XPUT http:
{
"name"
:
"顺义"
,
"location"
:
"40.1299127031,116.6569478577"
}
';
curl -XPUT http:
{
"name"
:
"天津"
,
"location"
:
"39.0850853357,117.1993482089"
}
';
curl -XPUT http:
{
"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"
}
}
]
}'
再举一个更加实际的例子
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:
{
"name"
:
"京津冀"
,
"location"
: [[
116.4072154982
,
39.9047253699
],[
116.6569478577
,
40.1299127031
]]}
';
佩服ES的强大