一.环境准备
1.elasticsearch压缩包、kibana插件、head-master插件,具体怎么安装配置,小伙伴们可以自行百度网上很多案例
可参考:https://blog.csdn.net/sinat_42338962/article/details/85227902
这里说一下,国内下载一般都很慢,可以参考这篇文章,博主都放在百度云,下载很方便
文章链接:https://blog.csdn.net/weixin_37281289/article/details/101483434
2.head插件使用,在elasticsearch-head-master根目录下执行npm run start,访问http://localhost:9100/即可查询并操作ES中数据,
3.kibana使用。在kibana/bin文件夹下,执行kibana.bat,访问http://localhost:5601/进行管理界面,在dev toos可进行操作数据,如下
4.查询数据之前首先要创建索引和定义数据类型,比如你想存放哪些字段(时间,经纬度,部门等)
PUT hbp
{
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"dept": {
"type": "text"
},
"location":{
"type": "geo_point"
},
"pub_date":{
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd"
}
}
}
}
5.再用脚本新增数据,或者用java代码连接es新增数据
PUT hbp/_doc/2
{
"name":"李四",
"pub_date":"2020-09-02 11:30:00",
"dept":"研发二部",
"location":{
"lat":39.669607,
"lon":118.257638
}
}
二:这里重点讲一下java根据距离范围查询并通过距离排序,当然下面代码中也有其他的查询和排序,仅供参考,这里需要先在pom.xml引入依赖,注意版本号一致
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.2</version>
</dependency>
接下来直接上代码
package com.zzht.esDemo;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.geo.GeoDistance;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
import org.elasticsearch.index.query.GeoValidationMethod;
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 org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* es测试
*
*/
public class EsDemo
{
private static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// 默认缓存限制为100MB,此处修改为30MB。
builder.setHttpAsyncResponseConsumerFactory(
new HttpAsyncResponseConsumerFactory
.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
protected RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));;
private String indexName = "hbp";
private String type = "_doc";
//从ES中查询数据
@Test
public void test1() throws IOException {
Integer pageSize = 10;
Integer PageNum = 0;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder bq = QueryBuilders.boolQuery();
//按时间范围查询
QueryBuilder dateBuilder = QueryBuilders.rangeQuery("pub_date")
.gte("2020-09-01 10:30:00").lte("2020-09-02 13:50:00").format("yyyy-MM-dd HH:mm:ss");//时间范围查询
//根据id查询
List<String> idList = new ArrayList<String>();
idList.add("1");
idList.add("2");
idList.add("3");
// idList.add("4");
// idList.add("5");
QueryBuilder idBuilder = QueryBuilders.termsQuery("_id", idList);
// 以某点为中心,搜索指定范围
GeoDistanceQueryBuilder distanceQueryBuilder = new GeoDistanceQueryBuilder("location");
distanceQueryBuilder.point(39.662263, 118.197815);
//查询单位:km
distanceQueryBuilder.distance("6", DistanceUnit.KILOMETERS);
//构建检索
/*这里说明一下,distanceQueryBuilder就是距离查询条件,idBuilder就是id过滤条件,
dateBuilder就是时间范围查询条件,可以定义一个或多个条件(QueryBuilder),下面的
排序也是同理,可以构造多个sortBuilder*/
QueryBuilder query = bq
// .filter(distanceQueryBuilder)
// .must(idBuilder)
.filter(dateBuilder)
;
searchSourceBuilder.query(query);
//按时间排序
// searchSourceBuilder.sort("pub_date", SortOrder.DESC);
//按距离排序
GeoDistanceSortBuilder distanceSort = SortBuilders.geoDistanceSort("location", 39.662263, 118.197815);
distanceSort.order(SortOrder.ASC);
distanceSort.geoDistance(GeoDistance.ARC);
distanceSort.unit(DistanceUnit.KILOMETERS);
distanceSort.validation(GeoValidationMethod.STRICT);
searchSourceBuilder.sort(distanceSort);
//查询es数据
searchSourceBuilder.from(PageNum);
searchSourceBuilder.size(pageSize);
searchSourceBuilder.timeout(new TimeValue(100,TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest("hbp");
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for(SearchHit hit : hits){
System.out.println(hit.getSourceAsString());
System.out.println(hit.getSortValues()[0]);//获取排序的距离,index为排序的位置
}
//get方式数据查询 ,参数为Index,type和id
// GetRequest request = new GetRequest(indexName, "7");
// GetResponse getResponse1 = client.get(request, COMMON_OPTIONS);
// System.out.println(getResponse1.getSourceAsString());
client.close();
}
//插入数据
public void test2() throws IOException {
String jsonStr = "{\"name\":\"开发004\",\"pub_date\":\"2020-09-05 18:00:00\",\"dept\":\"研发一部\",\"location\":{\"lon\":118.250192,\"lat\":39.691231}}";
IndexRequest request = new IndexRequest(indexName);
request.source(jsonStr, XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.getResult());
}
}
执行上面代码的test1方法,输出结果如下:
这个主要是根据经纬度来查询某个点方圆半径内的数据,比如附近5km内的餐馆
另外贴下根据父文档查询子文档代码写法
//父级查询结果返回字段设置
// FetchSourceContext fetchSourceContext = new FetchSourceContext(true,new String[]{"formObject"},null);
// HasParentQueryBuilder parentCondition = JoinQueryBuilders.hasParentQuery("procinst", //父文档type
// bqParent,false);//.innerHit(new InnerHitBuilder().setFetchSourceContext(fetchSourceContext));//设置父级(流程实例)返回字段
//
// queryChild.filter(parentCondition);