赞
踩
目录
1.2 terms查询terms和term的查询机制是一样的,都不会将指定的查询关键字进行分词,直接去分词库中匹配,找到相应文档内容。
2 、match查询match查询属于高级查询,他会根据你查询的字段类型不一样,采用不同的查询方式,更加灵活多变↓
2.3 match查询,追加操作,或者,并且基于一个Field匹配的内容,采用and或者or的方式连接
2.4 multi_match查询,多字段属性查询match针对一个field做检索,multi_match针对多个field进行检索,多个field对应一个text。
3.2 ids查询根据多个id查询,类似MySQL中的where id in(id1,id2,id2...)
3.3 prefix查询,前缀查询前缀查询,可以通过一个关键字去指定一个Field的前缀,从而查询到指定的文档。
3.4 fuzzy查询,模糊,比如不完全输对,也能搜索出来模糊查询,我们输入字符的大概,ES就可以去根据输入的内容大概去匹配一下结果。
3.5 wildcard查询,占位符,通配符查询通配查询,和MySQL中的like是一个套路类似,可以在查询时,在字符串中指定通配符*和占位符?
3.6 range查询,范围查询范围查询,只针对数值类型,对某一个Field进行大于或者小于的范围指定
3.7 regexp查询,正则查询正则查询,通过你编写的正则表达式去匹配内容。
4、 深分页之Scroll(滚动分页)之前学过分页了,为啥还要学习深分页?因为ES对from + size是有限制的,from和size二者之和不能超过1W
5 _delete_by_query,删除,根据查询出来的数据来删除(查询删除)根据term,match等查询方式去删除大量的文档
6.1 bool查询,布尔查询,组装多个条件复合过滤器,将你的多个查询条件,以一定的逻辑组合在一起。
6.2 boosting查询之前我们查询数据,可以查到分数,分数越高,越靠前,但是有些时候,我又不想某些数据太靠前,所以要操作分数↓
- // Java代码实现方式
- @Test
- public void termQuery() throws IOException {
- //1. 创建Request对象
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.from(0);
- builder.size(5);
- builder.query(QueryBuilders.termQuery("province","北京"));
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 获取到_source中的数据,并展示
- for (SearchHit hit : resp.getHits().getHits()) {
- Map<String, Object> result = hit.getSourceAsMap();
- System.out.println(result);
- }
- terms是在针对一个字段包含多个值的时候使用。
-
- term:where city = 北京;
-
- terms:where city = 北京 or id = ?or name = ?
-
- # terms查询
-
- // Java实现
- @Test
- public void termsQuery() throws IOException {
- //1. 创建request
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 封装查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.query(QueryBuilders.termsQuery("province","北京","山西"));
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出_source
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
查询的是日期或者是数值的话,他会将你基于的字符串查询内容转换为日期或者数值对待。
如果查询的内容是一个不能被分词的内容(keyword),match查询不会对你指定的查询关键字进行分词。
如果查询的内容时一个可以被分词的内容(text),match会将你指定的查询内容根据一定的方式去分词,去分词库中匹配指定的内容。
match查询,实际底层就是多个term查询,将多个term查询的结果给你封装到了一起而已。
- 查询全部内容,不指定任何查询条件。
- # match_all查询
- java代码实现方式
- @Test
- public void matchAllQuery() throws IOException {
- //1. 创建Request
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.query(QueryBuilders.matchAllQuery());
- builder.size(20); // ES默认只查询10条数据,如果想查询更多,添加size
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
-
- System.out.println(resp.getHits().getHits().length);
- }
- # match查询
- POST /sms-logs-index/sms-logs-type/_search
- java代码实现方式
- @Test
- public void matchQuery() throws IOException {
- //1. 创建Request
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //-----------------------------------------------
- builder.query(QueryBuilders.matchQuery("smsContent","收货安装"));
- //-----------------------------------------------
- request.source(builder);
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
# 布尔match查询,内容既包含中国也包含健康
# 布尔match查询,内容包括健康或者包括中国
java代码实现方式
- // Java代码实现
- @Test
- public void booleanMatchQuery() throws IOException {
- //1. 创建Request
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------- 选择AND或者OR↓
- builder.query(QueryBuilders.matchQuery("smsContent","中国 健康").operator(Operator.OR));
- //-----------------------------------------------
- request.source(builder);
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- # multi_match 查询
- // java代码实现
- @Test
- public void multiMatchQuery() throws IOException {
- //1. 创建Request
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //-----------------------------------------------
- builder.query(QueryBuilders.multiMatchQuery("北京","province","smsContent"));
- //-----------------------------------------------
- request.source(builder);
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- # 查询id为21的数据
- GET /sms-logs-index/sms-logs-type/21
- java代码实现方式(GetRequest)
-
- // Java代码实现
- @Test
- public void findById() throws IOException {
- //1. 创建GetRequest
- GetRequest request = new GetRequest(index,type,"21");//查id为21,可以打开看id再写即可
-
- //2. 执行查询
- GetResponse resp = client.get(request, RequestOptions.DEFAULT);
-
- //3. 输出结果
- System.out.println(resp.getSourceAsMap());
- }
# ids查询
java代码实现方式(idsQuery)
- // Java代码实现
- @Test
- public void findByIds() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.idsQuery().addIds("21","22","23"));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- # prefix查询
-
- java代码实现方式
-
- // Java实现前缀查询
- @Test
- public void findByPrefix() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.prefixQuery("corpName","关键词"));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- # fuzzy查询,可以指定前面几个字符是不允许出错
- POST /sms-logs-index/sms-logs-type/_search
-
- java代码实现方式(fuzzyQuery)
-
- // Java代码实现Fuzzy查询
- @Test
- public void findByFuzzy() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.fuzzyQuery("corpName","大概内容").prefixLength(2));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
# wildcard查询,可以使用*和?指定通配符和占位符(指定长度)
POST /sms-logs-index/sms-logs-type/_search
- 代码实现方式(wildcardQuery)
-
- // Java代码实现Wildcard查询
- @Test
- public void findByWildCard() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.wildcardQuery("corpName","中国*"));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
# range查询,可以使用 gt:> gte:>= lt:< lte:<=
POST /sms-logs-index/sms-logs-type/_search
java代码实现(rangeQuery)
- // Java实现range范围查询
- @Test
- public void findByRange() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.rangeQuery("fee").lte(10).gte(5));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
Ps:prefix,fuzzy,wildcard和regexp查询效率相对比较低,要求效率比较高时,避免去使用
# regexp查询,编写正则
POST /sms-logs-index/sms-logs-type/_search
java代码实现方式(regexpQuery)
- // Java代码实现正则查询
- @Test
- public void findByRegexp() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //----------------------------------------------------------
- builder.query(QueryBuilders.regexpQuery("mobile","180[0-9]{8}"));
- //----------------------------------------------------------
- request.source(builder);
-
- //3. 执行
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- # 根据scroll,查询下一页数据
- POST /_search/scroll
-
- # 删除scroll,在ES上下文中的数据
- DELETE /_search/scroll/scroll_id
- java代码实现方式
-
- // Java实现scroll分页
- @Test
- public void scrollQuery() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定scroll信息!
- request.scroll(TimeValue.timeValueMinutes(1L));
-
- //3. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
- builder.size(4);
- builder.sort("fee", SortOrder.DESC);
- builder.query(QueryBuilders.matchAllQuery());
-
- request.source(builder);
-
- //4. 获取返回结果scrollId,source
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- String scrollId = resp.getScrollId();
- System.out.println("----------首页---------");
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
-
- while(true) {
- //5. 循环 - 创建SearchScrollRequest
- SearchScrollRequest scrollRequest = new SearchScrollRequest(scrollId);
-
- //6. 指定scrollId的生存时间!
- scrollRequest.scroll(TimeValue.timeValueMinutes(1L));
-
- //7. 执行查询获取返回结果
- SearchResponse scrollResp = client.scroll(scrollRequest, RequestOptions.DEFAULT);
-
- //8. 判断是否查询到了数据,输出
- SearchHit[] hits = scrollResp.getHits().getHits();
-
- if(hits != null && hits.length > 0) {
- System.out.println("----------下一页---------");
- for (SearchHit hit : hits) {
- System.out.println(hit.getSourceAsMap());
- }
- }else{
- //9. 判断没有查询到数据-退出循环
- System.out.println("----------结束---------");
- break;
- }
- }
-
- //10. 创建CLearScrollRequest
- ClearScrollRequest clearScrollRequest = new ClearScrollRequest();
-
- //11. 指定ScrollId
- clearScrollRequest.addScrollId(scrollId);
-
- //12. 删除ScrollId
- ClearScrollResponse clearScrollResponse = client.clearScroll(clearScrollRequest, RequestOptions.DEFAULT);
-
- //13. 输出结果
- System.out.println("删除scroll:" + clearScrollResponse.isSucceeded());
- }
Ps:如果你需要删除的内容,是index下的大部分数据,推荐创建一个全新的index,将保留的文档内容,添加到全新的索引
# delete-by-query
java代码实现方式
- // Java代码实现
- @Test
- public void deleteByQuery() throws IOException {
- //1. 创建DeleteByQueryRequest
- DeleteByQueryRequest request = new DeleteByQueryRequest(index);
- request.types(type);
-
- //2. 指定检索的条件 和SearchRequest指定Query的方式不一样
- request.setQuery(QueryBuilders.rangeQuery("fee").lt(4));
-
- //3. 执行删除
- BulkByScrollResponse resp = client.deleteByQuery(request, RequestOptions.DEFAULT);
-
- //4. 输出返回结果
- System.out.println(resp.toString());
- }
- // Java代码实现Bool查询
- @Test
- public void BoolQuery() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
-
- BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
-
- //# 查询省份为武汉或者北京
- boolQuery.should(QueryBuilders.termQuery("province","武汉"));
- boolQuery.should(QueryBuilders.termQuery("province","北京"));
- //# 运营商不是联通
- boolQuery.mustNot(QueryBuilders.termQuery("operatorId",2));
-
- //# smsContent中包含中国和平安
- boolQuery.must(QueryBuilders.matchQuery("smsContent","中国"));
- boolQuery.must(QueryBuilders.matchQuery("smsContent","平安"));
-
- builder.query(boolQuery);
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
boosting查询可以帮助我们去影响查询后的score↓
positive:只有匹配上positive的查询的内容,才会被放到返回的结果集中(巧记积极返回,我要的)
negative:如果匹配上和positive并且也匹配上了negative,就可以降低这样的文档score(巧记消极减分,我要扣分的)
negative_boost:指定系数,必须小于等于1.0
关于查询时,分数是如何计算的↓
搜索的关键字在文档中出现的频次越高,分数就越高
搜索的内容所在的文档越短,分数就越高
我们在搜索时,指定的关键字也会被分词,这个被分词的内容,被分词库匹配的个数越多,分数越高
# boosting查询,收货安装
POST /sms-logs-index/sms-logs-type/_search
java代码实现方式:
- // Java实现Boosting查询
- @Test
- public void BoostingQuery() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
-
- BoostingQueryBuilder boostingQuery = QueryBuilders.boostingQuery(
- QueryBuilders.matchQuery("smsContent", "收货安装"),
- QueryBuilders.matchQuery("smsContent", "王五")
- ).negativeBoost(0.5f);
-
- builder.query(boostingQuery);
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
查询结果比如没减分之前王五的分数是1.75...,减分之后,系数写的0.5,就相当于1.75乘以0.5等于0.8
java实现方式
- // Java实现filter操作
- @Test
- public void filter() throws IOException {
- //1. SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 查询条件
- SearchSourceBuilder builder = new SearchSourceBuilder();
-
- BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
-
- boolQuery.filter(QueryBuilders.termQuery("corpName","关键词"));
- boolQuery.filter(QueryBuilders.rangeQuery("fee").lte(5));
-
- builder.query(boolQuery);
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 输出结果
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getSourceAsMap());
- }
- }
- RESTful实现
-
- # highlight查询
- POST /sms-logs-index/sms-logs-type/_search
-
- 说白了就是把要高亮的数据增加一个html标签并加上属性,比如字体的红色属性,这样以后把查询出来的数据在浏览器打开时就是红色的了
-
- java代码实现方式:
-
- // Java实现高亮查询
- @Test
- public void highLightQuery() throws IOException {
- //1. SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定查询条件(高亮)
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //2.1 指定查询条件
- builder.query(QueryBuilders.matchQuery("smsContent","关键词"));
-
- //2.2 指定高亮
- HighlightBuilder highlightBuilder = new HighlightBuilder();
- highlightBuilder.field("smsContent",10)
- .preTags("<font color='red'>")
- .postTags("</font>");
-
- builder.highlighter(highlightBuilder);
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 获取高亮数据,输出
- for (SearchHit hit : resp.getHits().getHits()) {
- System.out.println(hit.getHighlightFields().get("smsContent"));
- }
- }
ES的聚合查询和MySQL的聚合查询类似,但ES的聚合查询相比MySQL要强大的多,有ES提供的统计数据的方式多种多样,但是格式基本如下↓
# ES聚合查询的RESTful语法
去重计数,即Cardinality,第一步先将返回的文档中的一个指定的field进行去重,统计一共有多少条
# 去重计数查询 北京 上海 武汉 山西
java代码实现方式
- //Java代码实现去重计数查询
- @Test
- public void cardinality() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定使用的聚合查询方式
- SearchSourceBuilder builder = new SearchSourceBuilder();
-
- builder.aggregation(AggregationBuilders.cardinality("agg").field("province"));
-
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 获取返回结果
- Cardinality agg = resp.getAggregations().get("agg");
- long value = agg.getValue();
- System.out.println(value);
- }
比如在一定的区间内的数据统计查询出来封装在桶里面
统计一定范围内出现的文档个数,比如,针对某一个Field的值在 0~100,100~200,200~300之间文档出现的个数分别是多少。
范围统计可以针对普通的数值,针对时间类型,针对ip类型都可以做相应的统计。
range数值范围,date_range时间范围,ip_range即ip访问统计↓
# 数值方式范围统计,from有包含当前值的意思
POST /sms-logs-index/sms-logs-type/_search
结果,from5有>=5的意思,而to没有
时间范围统计↓
# 时间方式范围统计
POST /sms-logs-index/sms-logs-type/_search
结果,2000年以前的有多少个,2000以后的有多少个数据
ip范围统计↓
# ip方式 范围统计
POST /sms-logs-index/sms-logs-type/_search
- // Java实现数值 范围统计
- @Test
- public void range() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定使用的聚合查询方式
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //---------------------------------------------
- builder.aggregation(AggregationBuilders.range("agg").field("fee")
- .addUnboundedTo(5)
- .addRange(5,10)
- .addUnboundedFrom(10));
- //---------------------------------------------
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 获取返回结果
- Range agg = resp.getAggregations().get("agg");//注意这里用Range才有getBuckets方法↓
- for (Range.Bucket bucket : agg.getBuckets()) {
- String key = bucket.getKeyAsString();
- Object from = bucket.getFrom();
- Object to = bucket.getTo();
- long docCount = bucket.getDocCount();
-
- System.out.println(String.format("key:%s,from:%s,to:%s,docCount:%s",key,from,to,docCount));//%s理解为占位符的意思
- }
- }
代码怎么写其实和查询出来的结果标签其实是一一对应的,要注意这里用Range才有getBuckets方法
他可以帮你查询指定Field的最大值,最小值,平均值,平方和等
使用extended_stats查出来的结果里面就有各种最大值,最小值,平均值,平方和等(扩展状态,扩展内容
# 统计聚合查询,扩展状态
java代码实现方式
- // Java实现统计聚合查询
- @Test
- public void extendedStats() throws IOException {
- //1. 创建SearchRequest
- SearchRequest request = new SearchRequest(index);
- request.types(type);
-
- //2. 指定使用的聚合查询方式
- SearchSourceBuilder builder = new SearchSourceBuilder();
- //---------------------------------------------
- builder.aggregation(AggregationBuilders.extendedStats("agg").field("fee"));
- //---------------------------------------------
- request.source(builder);
-
- //3. 执行查询
- SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
-
- //4. 获取返回结果
- ExtendedStats agg = resp.getAggregations().get("agg");
- double max = agg.getMax();
- double min = agg.getMin();
- System.out.println("fee的最大值为:" + max + ",最小值为:" + min);
- }
其他聚合查询方式看官方文档:Elasticsearch Guide [6.5] | Elastic
ES中提供了一个数据类型 geo_point,这个类型就是用来存储经纬度的,
创建一个带geo_point类型的索引,并添加测试数据,来方便接下来的查询↓
10.1 ES的地图检索方式
语法 说明
geo_distance 直线距离检索方式
geo_bounding_box 以两个点确定一个矩形,获取在矩形内的全部数据
geo_polygon 以多个点,确定一个多边形,获取多边形内的全部数据
10.2 基于RESTful实现地图检索
geo_distance↓
# geo_distance,确定一个点,表示检索经纬度是北京站北京distance为3000米,方圆的范围的数据,arc圆形
geo_bounding_box↓
# geo_bounding_box,左上角中央人民大学的经纬度坐标点,右下角北京建筑大学的经纬度坐标点
# geo_polygon,指定多个点确定一个多边形,第一个点西苑操场,第二个点巴沟山水园,第三个点中关村
10.3 Java实现es基于地理位置经纬度范围查询↓
public class Test03 {
RestHighLevelClient client = ESClient.getClient();
String index = "map";//索引库名字
String type = "map";//类型表名字
@Test
public void geoPolygon() throws IOException {
//1.SearchRequest
SearchRequest request = new SearchRequest(index);
request.types(type);
//2.指定检索方式
SearchSourceBuilder builder = new SearchSourceBuilder();
List<GeoPoint> points = new ArrayList<GeoPoint>();
//geo_polygon,多点多边形,以第一个点西苑操场,第二个点巴沟山水园,第三个点中关村构成的多边形,包括海淀公园↓
points.add(new GeoPoint(39.99878,116.298916));
points.add(new GeoPoint(39.972576,116.29561));
points.add(new GeoPoint(39.984739,116.327661));
builder.query(QueryBuilders.geoPolygonQuery("location",points));
//geo_bounding_box,两点矩形,以左上角中央人民大学,右下角北京建筑大学构成的矩形包括北京动物园↓
//GeoBoundingBoxQueryBuilder location1 = QueryBuilders.geoBoundingBoxQuery("location");
//location1.topLeft().reset(39.95499,116.326943);
//location1.bottomRight().reset(39.939281,116.347783);
//builder.query(location1);
//distance,单点方圆,北京站这个点,方圆3000米的范围,包括天安门↓
//GeoDistanceQueryBuilder location = QueryBuilders.geoDistanceQuery("location");
//location.point(39.908404,116.433733).distance("3000");
//builder.query(location);
request.source(builder);
//3.执行查询
SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//4.输出结果
for (SearchHit hit : resp.getHits().getHits()) {
System.out.println(hit.getSourceAsMap());
}
}
- restClient = ElasticSearchPoolUtil.getClient();
- SearchSourceBuilder search = new SearchSourceBuilder();
- BoolQueryBuilder bool = QueryBuilders.boolQuery();
- Response rsp = null;
- Map<String, String> params = Collections.singletonMap("pretty", "true");
- 分页查询
- int start = limit * (page - 1);
- String substring = kssj.substring(0, 6);
- String index = "";
- 索引库名字
- index = "stgj_" + substring;
- //必须满足and,,用should组合在一起,表示Or的意思
- //范围查询
- bool.must(new RangeQueryBuilder("CreateTime").gte(kssj).lte(jssj));
- //模糊查询
- bool.must(QueryBuilders.wildcardQuery("DeviceID", "320902" + "*"));
- //匹配查询
- bool.must(QueryBuilders.termQuery("FaceIDList", FaceI));
-
- search.from(start);
- search.size(limit);
- //查询条件
- search.query(bool);
- HttpEntity entity = new NStringEntity(search.toString(), ContentType.APPLICATION_JSON);
- rsp = restClient.performRequest("GET", "/" + index + "/_search", params, entity);
- 此处查询所有以ycst_开头的索引库
- // rsp = restClient.performRequest("GET", "/" + "ycst_" + "*" + //"/_search", params, entity);
- String jsonStr = EntityUtils.toString(rsp.getEntity());
- JSONObject doc = (JSONObject) JSONObject.parseObject(jsonStr);
- JSONObject jsonObject = (JSONObject) doc.getJSONObject("hits");
- JSONArray hits = (JSONArray) jsonObject.get("hits");
- //
- HashMap<String, Object> map = new HashMap<>(1);
- CommonExceptionEnum.NOT_NULL.assertNotEmpty(request);
- CommonExceptionEnum.NOT_NULL.assertNotEmpty(request.getPlatform());
- CommonExceptionEnum.NOT_NULL.assertNotEmpty(request.getType());
- if ("2".equals(request.getPlatform())) {
- Long deptId = SecurityUtils.getDeptId();
- if (ObjectUtil.isEmpty(deptId)) {
- return RequestResult.success(map);
- }
- request.setDeptId(deptId.intValue());
- }
- Long start = System.currentTimeMillis();
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtils.YYYY_MM_DD);
- //日
- if (request.getType().equals(1)) {
- simpleDateFormat = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
- start = Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant()).getTime();
- }
- //周
- if (request.getType().equals(2)) {
- start = getTimesWeekmorning().getTime();
- }
- //月
- if (request.getType().equals(3)) {
- start = getTimesMonthmorning().getTime();
- }
-
- String substring = start.toString().substring(0,start.toString().length() - 5);
- start = (Long.parseLong(substring) * 100000L);
-
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
- long end = System.currentTimeMillis();
-
- boolQueryBuilder.must(QueryBuilders.rangeQuery("cjTimestamp").gte(start).lte(end));
- if (ObjectUtil.isNotEmpty(request.getDeptId())) {
- boolQueryBuilder.must(QueryBuilders.termQuery("devicesInfo.deptId", request.getDeptId()));
- }
- //dateHistogram 间隔是天的话 差距8小时,需要减去8小时
- AggregationBuilder aggregationBuilder = null;
- if (!request.getType().equals(1)) {
- aggregationBuilder = AggregationBuilders
- .dateHistogram("dateHistogram")//自定义名称
- .dateHistogramInterval(getDateHistogramInterval(request.getType()))//设置间隔
- .minDocCount(0)//返回空桶
- .field("cjTimestamp")//指定时间字段
- .format("")
- .extendedBounds(new ExtendedBounds(start, end))//设定范围
- .offset("-8h");
- } else {
- aggregationBuilder = AggregationBuilders
- .dateHistogram("dateHistogram")//自定义名称
- .dateHistogramInterval(getDateHistogramInterval(request.getType()))//设置间隔
- .minDocCount(0)//返回空桶
- .field("cjTimestamp")//指定时间字段
- .format("")
- .extendedBounds(new ExtendedBounds(start, end));//设定范围
- }
-
- searchSourceBuilder.query(boolQueryBuilder).aggregation(aggregationBuilder).size(0);
- SearchRequest searchRequest = new SearchRequest();
- searchRequest.source(searchSourceBuilder);
- searchRequest.indices("standard_device_data");
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- Aggregation agg = searchResponse.getAggregations().get("dateHistogram");
- List<? extends Histogram.Bucket> buckets = ((Histogram) agg).getBuckets();
- List<CollectDataResponse> list = new ArrayList<>();
- for (Histogram.Bucket bucket : buckets) {
- CollectDataResponse collectDataResponse = new CollectDataResponse();
- long time = Long.parseLong(bucket.getKeyAsString().replace(",", ""));
- if (time >= start) {
- String dateString = simpleDateFormat.format(time);
- collectDataResponse.setDate(dateString);
- collectDataResponse.setNum(bucket.getDocCount());
- list.add(collectDataResponse);
- }
- }
- map.put("allNum", list);
-
-
- private DateHistogramInterval getDateHistogramInterval(Integer dateType) {
- if (dateType.equals(1)) {
- return DateHistogramInterval.minutes(15);//统计一个小时内数据,每隔10分钟一个显示
- }
- //周
- if (dateType.equals(2)) {
- return DateHistogramInterval.days(1);
- }
- //月
- if (dateType.equals(3)) {
- return DateHistogramInterval.days(1);
- }
- return DateHistogramInterval.days(1);
- }
-
- // 获得本周一0点时间
- public static Date getTimesWeekmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
- return cal.getTime();
- }
-
- // 获得本月第一天0点时间
- public static Date getTimesMonthmorning() {
- Calendar cal = Calendar.getInstance();
- cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
- cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
- return cal.getTime();
- }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。