建立映射
PUT /my_index
{
"mappings": {
"my_type": {
"properties": {
"location" : {
"type": "geo_point" //地理位置的分词器是geo_point
}
}
}
}
}
填充数据
PUT my_index/my_type/1
{
"text": "Geo-point as an object",
"location": {
"lat": 41.12, //latitude :纬度的缩写
"lon": -71.34 //longitude : 经度的缩写
}
}
根据地理位置进行查询
GET /my_index/my_type/_search
{
"query": {
"geo_bounding_box": {
"location": {
"top_left" : {
"lat" : 42,
"lon" : -72
},
"bottom_right" : {
"lat" : 40,
"lon" : -74
}
}
}
}
}
//有一点很奇怪,经纬度和坐标系有点不一样
酒店O2O
//建立索引
PUT /hotel_app
{
"mappings": {
"hotels" : {
"properties": {
"bin" : {
"properties": {
"location" : {
"type" : "geo_point"
}
}
}
}
}
}
}
//填充数据
PUT /hotel_app/hotels/1
{
"name": "喜来登大酒店",
"pin" : {
"location" : {
"lat" : 40.12,
"lon" : -71.34
}
}
}
//搜索(两点矩形)
GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_bounding_box": {
"pin.location": {
"top_left" : {
"lat" : 40.73,
"lon" : -74.1
},
"bottom_right" : {
"lat" : 40.01,
"lon" : -71.12
}
}
}
}
}
}
}
//搜索(多点,多边形)
GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_polygon": {
"pin.location": {
"points": [
{"lat" : 40.73, "lon" : -74.1},
{"lat" : 40.01, "lon" : -71.12},
{"lat" : 50.56, "lon" : -90.58}
]
}
}
}
}
}
}
//搜索(根据距当前位置的距离)
GET /hotel_app/hotels/_search
{
"query": {
"bool": {
"must": [
{"match_all": {}}
],
"filter": {
"geo_distance": {
"distance": "1000km",
"pin.location": {
"lat": 40,
"lon": -74
}
}
}
}
}
}
//聚合分析,(距离当前位置一定范围内有多少个酒店)
GET /hotel_app/hotels/_search
{
"size": 0,
"aggs": {
"count_by_distinct": {
"geo_distance": {
"field": "pin.location", //要分析的点
"origin": { //当前位置经纬度
"lat": 40,
"lon": 70
},
"ranges": [ //范围控制
{"to" : 100},
{
"from": 100,
"to": 300
},
{"from": 300}
],
"unit": "mi", //单位
"distance_type": "arc" //排序,这个比较消耗性能,请谨慎使用
}
}
}
}