ElasticSearch2.1 基于空间位置geo_query距离计算

本文介绍了如何在ElasticSearch 2.1中创建索引和映射,特别是针对地理位置(geo_point)字段的设置。然后展示了如何添加地理位置数据,并通过geo_distance_range查询来获取指定位置附近的城市。示例代码包括创建索引、添加数据、模糊查询和获取附近城市的函数。
摘要由CSDN通过智能技术生成

首先声明 我使用的ES版本是2.1,不同版本api可能不尽相同

1、首先创建索引mapping

(1)方法1

[java]  view plain   copy
  在CODE上查看代码片 派生到我的代码片
  1. // 创建索引方法1  
  2.     public static void createIndex2(String indexName, String indexType) throws IOException {  
  3.         Settings settings = Settings.settingsBuilder()  
  4.                 .put("cluster.name""cluster_wubing")  
  5.                 .build();  
  6.         InetAddress inetAddress = InetAddress.getByName("192.168.1.107");;  
  7.   
  8.         Client esClient = TransportClient.builder().settings(settings).build()  
  9.                 .addTransportAddress(new InetSocketTransportAddress(inetAddress, 9300));  
  10.         //创建mapping,我这里使用的分词器analyzer/search_analyzer为ik,注意位置location类型为geo_point  
  11.         String mapping = "{\"properties\": {\n" +  
  12.                 "      \"id\": {\n" +  
  13.                 "        \"type\": \"integer\",\n" +  
  14.                 "        \"index\": \"not_analyzed\",\n" +  
  15.                 "        \"include_in_all\": false\n" +  
  16.                 "      },\n" +  
  17.                 "      \"title\": {\n" +  
  18.                 "        \"type\": \"string\",\n" +  
  19.                 "        \"store\": \"no\",\n" +  
  20.                 "        \"term_vector\": \"with_positions_offsets\",\n" +  
  21.                 "        \"analyzer\": \"ik_max_word\",\n" +  
  22.                 "        \"search_analyzer\": \"ik_max_word\",\n" +  
  23.                 "        \"include_in_all\": true\n" +  
  24.                 "      },\n" +  
  25.                 "      \"city\": {\n" +  
  26.                 "        \"type\": \"string\",\n" +  
  27.                 "        \"store\": \"no\",\n" +  
  28.                 "        \"term_vector\": \"with_positions_offsets\",\n" +  
  29.                 "        \"analyzer\": \"ik_max_word\",\n" +  
  30.                 "        \"search_analyzer\": \"ik_max_word\",\n" +  
  31.                 "        \"include_in_all\": true\n" +  
  32.                 "      },\n" +  
  33.                 "      \"location\":{\n" +  
  34.                 "        \"type\":\"geo_point\",\n" +  
  35.                 "        \"index\": \"not_analyzed\",\n" +  
  36.                 "        \"include_in_all\": false\n" +  
  37.                 "      }\n" +  
  38.                 "    }}";  
  39.         esClient.admin().indices().prepareCreate(indexName).execute().actionGet();  
  40.         PutMappingResponse response  = esClient.admin().indices().preparePutMapping(indexName)  
  41.                 .setType(indexType)  
  42.                 .setSource(mapping)  
  43.                 .get();  
  44.   
  45.         if (!response.isAcknowledged()) {  
  46.             System.out.println("Could not define mapping for type [" + indexName + "]/[" + indexType + "].");  
  47.         } else {  
  48.             System.out.println("Mapping definition for [" + indexName + "]/["
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值