当前位置: 首页 > news >正文

ES中经纬度查询geo_point

0. ES版本

6.x版本

1. 创建索引

PUT /location
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "location": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "name": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_max_word"
        },
        "coordinate": {
          "type": "geo_point"
        }
      }
    }
  }
}

2. 批量插入数据

注意:这里的coordinate用的是字符串插入,所以要用纬经度。试了下用数组的形式(“coordinate”:[116.64002551005981,40.17836693398477])会报错,应该是6.x的版本不支持吧

PUT /location/location/_bulk
{"index":{"_id":"1"}}
{"name":"资料名称1","coordinate":"40.17836693398477,116.64002551005981"}
{"index":{"_id":"2"}}
{"name":"资料名称2","coordinate":"40.19103839805197,116.5624013764374"}
{"index":{"_id":"3"}}
{"name":"资料名称3","coordinate":"40.13933715136454,116.63441990026217"}
{"index":{"_id":"4"}}
{"name":"资料名称4","coordinate":"40.14901664712196,116.53067995860928"}
{"index":{"_id":"5"}}
{"name":"资料名称5","coordinate":"40.125057718315716,116.62963567059545"}
{"index":{"_id":"6"}}
{"name":"资料名称6","coordinate":"40.19216257806647,116.64025980109571"}
{"index":{"_id":"7"}}
{"name":"资料名称7","coordinate":"40.16371689899584,116.63095084701624"}
{"index":{"_id":"8"}}
{"name":"资料名称8","coordinate":"40.146045218040605,116.5696251832195"}
{"index":{"_id":"9"}}
{"name":"资料名称9","coordinate":"40.144735806234166,116.60712460957835"}

3. geo_bounding_box查询

3.1 解释

geo_bounding_box是查询一个矩形内的所有数据,提供左上角和右上角的经纬度坐标
在这里插入图片描述

3.2 经纬度属性查询

GET /location/location/_search
{
  "query": {
    "geo_bounding_box": {
      "coordinate": {
        "top_left": {
          "lat": 40.187328,
          "lon": 116.498353
        },
        "bottom_right": {
          "lat": 40.084509,
          "lon": 116.610461
        }
      }
    }
  }
}

3.3 经纬度数组查询

GET /location/location/_search
{
  "query": {
    "geo_bounding_box": {
      "coordinate": {
        "top_left": [116.498353, 40.187328],
        "bottom_right": [116.610461, 40.084509]
      }
    }
  }
}

3.4 经纬度字符串查询

GET /location/location/_search
{
  "query": {
    "geo_bounding_box": {
      "coordinate": {
        "top_left": "40.187328, 116.498353",
        "bottom_right": "40.084509, 116.610461"
      }
    }
  }
}

3.5 经纬度边界框WKT查询

GET /location/location/_search
{
  "query": {
    "geo_bounding_box": {
      "coordinate": {
        "wkt": "BBOX(116.498353,116.610461,40.187328,40.084509)"
      }
    }
  }
}

3.6 经纬度顶点属性查询

GET /location/location/_search
{
  "query": {
    "geo_bounding_box": {
      "coordinate": {
        "top": 40.187328,
        "left": 116.498353,
        "bottom": 40.084509,
        "right": 116.610461
      }
    }
  }
}

4. geo_polygon查询

4.1 解释

geo_polygon查询一个多边形区域内的所有数据,需要按顺序形成一个闭合的区域,不管从哪个坐标开始,只要能形成一个闭合区域就行
在这里插入图片描述

4.2 查询

GET /location/location/_search
{
  "query": {
    "geo_polygon": {
      "coordinate": {
        "points": [
          "40.178012,116.577188",
          "40.169329,116.586315",
          "40.178288,116.591813"
        ]
      }
    }
  }
}
GET /location/location/_search
{
  "query": {
    "geo_polygon": {
      "coordinate": {
        "points": [
          "40.178012,116.577188",
          "40.169329,116.586315",
          "40.178288,116.591813",
          "40.171975,116.587105"
        ]
      }
    }
  }
}

5. Springboot中使用geo_bounding_box查询

5.1 实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "location", type = "location")
public class AddressEsEntity {
    @Id
    private String id;

    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String name;

    @Field(type = FieldType.Keyword)
    private String coordinate;
}

5.2 geoBoundingBoxQuery

这里的setCorners里面直接传topLeft和bottomRight会报错,具体原因还没研究

String topLeft = "40.187328,116.498353";
String bottomRight = "40.084509,116.610461";
try {
    SearchRequest searchRequest = new SearchRequest("location");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.geoBoundingBoxQuery("coordinate")
            .setCorners(new GeoPoint("40.187328,116.498353"), new GeoPoint("40.084509,116.610461")));
    searchRequest.types("location").source(searchSourceBuilder);
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    SearchHit[] hits = searchResponse.getHits().getHits();
    System.out.println(searchResponse);
} catch (IOException e) {
    e.printStackTrace();
}

5.3 geoPolygonQuery

List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint("40.178012,116.577188"));
points.add(new GeoPoint("40.169329,116.586315"));
points.add(new GeoPoint("40.178288,116.591813"));
try {
    SearchRequest searchRequest = new SearchRequest("address");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.geoPolygonQuery("coordinate", points));
    searchRequest.types("address").source(searchSourceBuilder);
    SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
    SearchHit[] hits = searchResponse.getHits().getHits();
    System.out.println(searchResponse);
} catch (IOException e) {
    e.printStackTrace();
}
http://www.dtcms.com/a/109092.html

相关文章:

  • 图像处理之Homography matrix(单应性矩阵)
  • 2025年4月3日(模数转换器)
  • 【Centos】centos7内核升级-亲测有效
  • 【动态规划】P8638 [蓝桥杯 2016 省 A] 密码脱落
  • 树莓派 5 换清华源
  • 【C语言】C语言文件操作指南
  • 质检LIMS系统在垃圾处理厂的应用 垃圾处理质检的三重挑战与LIMS破局之道
  • 管理系统如何帮助你节省时间和成本?
  • 移动端六大语言速记:第7部分 - 文件与输入输出(I/O)
  • 【网络流 图论建模 最大权闭合子图】 [六省联考 2017] 寿司餐厅
  • 二十八、城市建成区提取——领域分析法
  • vulnhub-DC-2通关攻略
  • 从零构建大语言模型全栈开发指南:第五部分:行业应用与前沿探索-5.2.1模型偏见与安全对齐(Red Teaming实践)
  • 新能源汽车测试中的信号调理模块:从原理到实战应用
  • python--文件夹的压缩和解压缩(zipfile/pyzipper)
  • Day15——路由
  • 飞浆PaddlePaddle 猫狗数据大战
  • Pyinstaller 打包flask_socketio为exe程序后出现:ValueError: Invalid async_mode specified
  • 学习threejs,使用Texture纹理贴图,测试repeat重复纹理贴图
  • ngx_regex_init
  • C语言基础要素(019):输出ASCII码表
  • 李沐 X 动手学深度学习--第九章 现代循环神经网络
  • webstorm初始化配置项目
  • MySQL学习集--DDL
  • Python 数据科学实战指南:从零开始构建高效分析流程
  • 单片机学习之SPI
  • JVM深入原理(七)(一):运行时数据区
  • 图形界面设计理念
  • 使用 PyTorch 的 `optim.lr_scheduler.CosineAnnealingLR` 学习率调度器
  • 蓝桥云客-修建灌木