java使用es查询地理位置信息
1.引入依赖
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.8.0</version></dependency>
2.实体类
import lombok.Data;@Data
public class GeoLocations {private String name;private Object location;//切记这里不要用GeoPoint,否则json转化那里会保存,索引的创建可以使用kibana进行创建
}
使用kibana创建索引
PUT locations
{"mappings": {"properties": {"name": {"type": "text"},"location": {"type": "geo_point"}}}
}
3.工具类
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.GeoDistanceSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;public class EsGeoUntils {private static String host="192.168.127.103";private static int port=9200;private static String scheme="http";private Logger logger;private static final String INDEX_NAME ="locations";private static RestHighLevelClient client=new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));/*** 初始化 先创建index*///public void init(){// 1、创建索引请求// CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME);// 2、客户端执行请求 IndicesClient,请求后获得响应// try {// client.indices().create(request, RequestOptions.DEFAULT);// } catch (IOException e) {// logger.info(e.getMessage());//}// }/*** 根据传入的点位和半径查询周围的数据*/public List<GeoLocations> getMessage(Double longitude,Double latitude,int banjing) {List<GeoLocations> list = new ArrayList<>();//1.创建查询条件SearchSourceBuilder srb = new SearchSourceBuilder();QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")//location 是属性字段.point(latitude,longitude)//先纬度后经度.distance(banjing, DistanceUnit.METERS);//半径大小,单位:米srb.query(qb);//排序GeoDistanceSortBuilder sort = SortBuilders.geoDistanceSort("location",latitude,longitude)//location 是属性字段.order(SortOrder.ASC).unit(DistanceUnit.METERS);srb.sort(sort);// 2.创建并设置SearchRequest对象SearchRequest searchRequest = new SearchRequest(INDEX_NAME);searchRequest.source(srb);// 发起查询请求try {SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);//查询SearchHits hits = search.getHits();for (SearchHit hit: hits) {String location = hit.getSourceAsString();GeoLocations jsonObject = JSON.parseObject(location,GeoLocations.class);list.add(jsonObject);}} catch (IOException e) {logger.error(e.getMessage(), e);}return list;}/*** 根据传入的点位和半径查询周围指定的数据*/public List<GeoLocations> getSomeMessage(Double longitude,Double latitude,int banjing,String word,String value) {List<GeoLocations> list = new ArrayList<>();//1.创建查询条件SearchSourceBuilder srb = new SearchSourceBuilder();QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")//location 是属性字段.point(latitude,longitude)//先纬度后经度.distance(banjing, DistanceUnit.METERS);//半径大小,单位:米srb.query(qb);GeoDistanceSortBuilder sort = SortBuilders.geoDistanceSort("location",latitude,longitude)//location 是属性字段.order(SortOrder.ASC).unit(DistanceUnit.METERS);srb.sort(sort);//布尔查询条件BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(word, value));//根据要查询的字段和值进行查询srb.query(boolQueryBuilder);// 查询条件--->生成DSL查询语句// 2.创建并设置SearchRequest对象SearchRequest searchRequest = new SearchRequest(INDEX_NAME);searchRequest.source(srb);// 发起查询请求try {SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);//查询SearchHits hits = search.getHits();for (SearchHit hit: hits) {String location = hit.getSourceAsString();GeoLocations jsonObject = JSON.parseObject(location,GeoLocations.class);list.add(jsonObject);}} catch (IOException e) {logger.error(e.getMessage(), e);}return list;}
}
4.测试类
import java.io.IOException;
import java.util.List;
public class GeoTest {public static void main(String[] args) throws IOException {EsGeoUntils esGeoUntils = new EsGeoUntils();Double d1=-25.442987;Double d2=-49.239504;
// List list=esGeoUntils.getMessage(d2,d1,600);
// System.out.println(list);List list2=esGeoUntils.getSomeMessage(d2,d1,600,"name","贝克汉");System.out.println(list2);}}
5.数据(使用kibana插入)
POST locations/_bulk
{ "index" : { "_id" : "1" } }
{ "name" : "William", "location": "-25.443053, -49.238396" }
{ "index" : { "_id" : "2" } }
{ "name" : "Robert", "location": "-25.440173, -49.243169" }
{ "index" : { "_id" : "3" } }
{ "name" : "Bernard", "location": "-25.440262, -49.247720" }
{ "index" : { "_id" : "4" } }
{ "name" : "Dolores", "location": "-25.442987, -49.239504" }
{ "index" : { "_id" : "5" } }
{ "name" : "贝克汉姆", "location": "-25.443059, -49.238399" }
{ "index" : { "_id" : "6" } }
{ "name" : "阿拉丁", "location": "-25.440179, -49.243179" }
{ "index" : { "_id" : "7" } }
{ "name" : "丘吉尔", "location": "-25.440269, -49.247729" }
{ "index" : { "_id" : "8" } }
{ "name" : "奥林匹克公园", "location": "-25.442187, -49.239104" }
6.查询(使用kibana查询)
GET locations/_search
{"query": {"bool": {"filter": {"geo_distance": {"distance": "600","location": "-25.442987, -49.239504"}}}}
}
java使用es查询地理位置信息相关推荐
- java使用es查询
众所周知,elasticsearch简称es,它是基于基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开 ...
- java操作es查询数据总量
通过java查询es的数据总量返回数据接口 public long rangeCount(String indexName, String timeFileds, String vehicleidFi ...
- java通过手机号查询地区信息
通过手机号查询省份城市. 有两种方式: 一,通过发送url请求获取信息.此方法在遍历手机号查询时效率很低. private String getProvinceName(String phoneNum ...
- java调用Linux查询各类信息脚本汇总
神坑预警 如果采用shell或者命令行里执行命令的话,没有任何问题,但是如果采用字符串类型在java中调用的话会出现部分执行无返回结果的问题,其原因猜测是部分命令解析不了java中的空格以及| 等符号 ...
- 根据IP查询地理位置信息
引入库 在build.gradle中引用库 compile 'com.maxmind.geoip2:geoip2:2.8.0' 下载数据库文件. 在GEOIP中有2个版本, 一个是lite(免费), ...
- Java操作es 查询时 [search_phase_execution_exception] all shards failed
co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [search_phase_ex ...
- Android通过百度地图API用Service和Alarm在后台定时获取地理位置信息
本文主要介绍了Android项目集成百度地图API,使用AlarmManager定时调用Service,在Service中请求坐标更新,并通过坐标得到省.市和县三级地理位置信息的方法. 程序结构很简单 ...
- ES地理范围查询第一讲:Java操作地理位置信息(geo_point)
- 简要介绍 我们知道es支持的数据类型是多种多样的,除了我们常见的几种基本数据类型,它也支持记录位置信息的的数据类型.在es中,记录地理位置信息的数据类型有两种,分别为geo_shap和geo_po ...
- java 查询 代码_java使用es查询的示例代码
众所周知,elasticsearch简称es,它是基于基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开 ...
最新文章
- vue 2.0 :key的作用
- [转] 中文字体网页开发指南
- python json解析 超过对象_json序列化数据超出最大值(maxJsonLength)
- 苹果如何分屏_刚刚,苹果证实,iPhone12 刘海没了!
- 硬核知识,C/C++移植法则分享
- 用html编写你好,02 - HTML5第一个项目:HelloWorld!(收藏)
- windows 10卸载(注销)WSL,注销(卸载)当前安装的Linux的Windows子系统
- Serializable serialVersionUID作用
- 获取句子迷的经典语录[Python版本]
- Mac如何查看系统根目录
- 第八届北京国际电影节开幕 首次发布中国电影大数据
- 【codeforces 417D】Cunning Gena
- 用flash做连线题(线的一端跟随鼠标)
- 虚化视频边框,添加滚动字幕的简单教程
- java实现的FTP上传下载客户端
- 觉得还不错的国内外编程技术网站、论坛列表
- Win11 2022更新向所有符合兼容要求的用户开放,你会升级吗?
- python图像处理:凸透镜效果/鱼眼图像
- Android4.2.2 SurfaceFlinger本地的FramebufferSurface实现真正的显示
- 蓝桥杯-c语言 高僧斗法