栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 数据挖掘与分析

Elasticsearch LBS 的简单应用

Elasticsearch LBS 的简单应用

1. 背景介绍

有一些餐馆或酒店散步在你的周围,你想查询你附近的酒店。

2. ES model
{
  "shop" : {
    "mappings" : {
      "properties" : {
        "category_id" : {
          "type" : "integer"
        },
        "category_name" : {
          "type" : "keyword"
        },
        "id" : {
          "type" : "integer"
        },
        "location" : {
          "type" : "geo_point"
        },
        "name" : {
          "type" : "text",
          "analyzer" : "ik_max_word",
          "search_analyzer" : "ik_smart"
        }
      }
    }
  }
}

重点在于 shop model mapping 中有一列是:location。

数据格式:

3. 距离计算

POST /shop/_search

{
  "query": {
    "match": {
      "name": "和府"
    }
  },
  "_source": "*", 
  "script_fields": {
    "distance": {
      "script": {
        "source": "haversin(lat, lon, doc['location'].lat, doc['location'].lon)",
        "lang": "expression",
        "params": {"lat": 42.17, "lon": 117.12}
      }
    }
  }
}
4. 根据距离升序排序

POST /shop/_search

{
  "query": {
    "match": {
      "name": "和府"
    }
  },
  "_source": "*", 
  "script_fields": {
    "distance": {
      "script": {
        "source": "haversin(lat, lon, doc['location'].lat, doc['location'].lon)",
        "lang": "expression",
        "params": {"lat": 42.17, "lon": 117.12}
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "location": {
          "lat": 42.17,
          "lon": 117.12
        },
        "order": "asc",
        "unit": "km",
        "distance_type": "arc"
      }
    }  
  ]
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/279120.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号