Elasticsearch进阶笔记第三十九篇

257 阅读2分钟

Elasticsearch高手进阶篇(80)

elasticsearch高手进阶_实战搜索距离当前位置一定范围内的酒店

酒店o2o app

比如说,现在用户,所在的位置,是个地理位置的坐标,我是知道我的坐标的,app是知道的,android,地理位置api,都可以拿到当前手机app的经纬度

我说,我现在就要搜索出,举例我200m,或者1公里内的酒店

重要!!!!

我们之前出去玩儿,都会用一些酒店o2o app,典型的代表,很多旅游app,一般来说,我们怎么搜索,到了一个地方,就会搜索说,我这里举例几百米,2公里内的酒店,搜索一下

上节课讲解的,其实也很重要,一般来说,发生在我们在去旅游之前,会现在旅游app上搜索一个区域内的酒店,比如说,指定了西安火车站、西安博物馆,拿指定的几个地方的地理位置,组成一个多边形区域范围,去搜索这个区域内的酒店

承认,一些案例,当然不可能说达到讲解真实的复杂的大型的项目的效果来的那么好,光是学知识,学技术而言,有一些案例就非常不错了

后面,会讲解真正的企业级的大型的搜索引擎,真实复杂业务的数据分析系统的项目

 GET /waws_hotel_app/hotels/_search
 {
   "query": {
     "bool": {
       "must": [
         {
           "match_all": {}
         }
       ],
       "filter": {
         "geo_distance": {
           "distance": "200km",   
           "pin.location": {
             "lat": 40,
             "lon": -70
           }
         }
       }
     }
   }
 }
 
 {
   "took": 14,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 1,
     "hits": [
       {
         "_index": "waws_hotel_app",
         "_type": "hotels",
         "_id": "1",
         "_score": 1,
         "_source": {
           "name": "喜来登大酒店",
           "pin": {
             "location": {
               "lat": 40.12,
               "lon": -71.34
             }
           }
         }
       }
     ]
   }
 }

Elasticsearch高手进阶篇(81)

elasticsearch高手进阶_统计当前位置每个距离范围内有多少家酒店

基于地理位置进行聚合分析

我的需求就是,统计一下,举例我当前坐标的几个范围内的酒店的数量,比如说举例我0-100m有几个酒店,100m-300m有几个酒店,300m以上有几个酒店

  • 一般来说,做酒店app,一般来说,我们是不是会有一个地图,用户可以在地图上直接查看和搜索酒店,此时就可以显示出来举例你当前的位置,几个举例范围内,有多少家酒店,让用户知道,心里清楚,用户体验就比较好
 GET /waws_hotel_app/hotels/_search
 {
   "size": 0,
   "aggs": {
     "agg_by_distance_range": {
       "geo_distance": {
         "field": "pin.location",
         "origin": {
           "lat": 40,
           "lon": -70
         },
         "unit": "mi", 
         "ranges": [
           {
             "to": 100
           },
           {
             "from": 100,
             "to": 300
           },
           {
             "from": 300
           }
         ]
       }
     }
   }
 }
 ​
 {
   "took": 60,
   "timed_out": false,
   "_shards": {
     "total": 5,
     "successful": 5,
     "failed": 0
   },
   "hits": {
     "total": 1,
     "max_score": 0,
     "hits": []
   },
   "aggregations": {
     "agg_by_distance_range": {
       "buckets": [
         {
           "key": "*-100.0",
           "from": 0,
           "to": 100,
           "doc_count": 1
         },
         {
           "key": "100.0-300.0",
           "from": 100,
           "to": 300,
           "doc_count": 0
         },
         {
           "key": "300.0-*",
           "from": 300,
           "doc_count": 0
         }
       ]
     }
   }
 }
  • m (metres) but it can also accept: m (miles), km (kilometers)
  • sloppy_arc (the default), arc (most accurate)and plane (fastest)