2.实体类org.elasticsearch.client elasticsearch-rest-high-level-client7.8.0
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))); //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 List4.测试类getMessage(Double longitude,Double latitude,int banjing) { List 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 getSomeMessage(Double longitude,Double latitude,int banjing,String word,String value) { List 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; } }
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" } } } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)