ES练习---各种查询方式

一、创建表结构:

 

 

二、ES的各种查询

2.1term&terms查询

2.1.1term查询(我试了下province没有办法查询,需到后面加个.keyword)

 

 

 

package com.qf.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.utils.ESClient;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.Map;

public class Demo4 {
    ObjectMapper mapper=new ObjectMapper();
    RestHighLevelClient client = ESClient.getClient();

    String index="sms-logs-index";
    String type="sms-logs-type";
    @Test
    public void termQuery() throws IOException {
        //1.创建request对象
        SearchRequest searchRequest=new SearchRequest(index);
        searchRequest.types(type);
        //2.指定查询条件
        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.from(0);
        builder.size(5);
        builder.query(QueryBuilders.termQuery("province.keyword","北京"));
        searchRequest.source(builder);
        //执行查询
        SearchResponse rsp=client.search(searchRequest, RequestOptions.DEFAULT);
        //输出结果
        for (SearchHit hit:rsp.getHits().getHits()) {
            Map<String,Object> result=hit.getSourceAsMap();
            System.out.println(result);
            
        }
            
        }
   }

  

2.1.2terms查询

 

 

POST /sms-logs-index/sms-logs-type/_search
{
  "from": 0,
  "size": 5,
  "query":{
  "terms":{
    "province.keyword":[
      "江苏",
      "北京",
      "南通"
    ]
    
   }
  } 
}

  

    @Test
    public void termsQuerry() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);
        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.termsQuery("province.keyword","南通","江苏"));
        request.source(builder);;
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        //输出结果
        for (SearchHit hit:rsp.getHits().getHits()) {
            Map<String,Object> result=hit.getSourceAsMap();
            System.out.println(result);
    }
   }}

  

 

2.2match_all&match查询

2.2.1match_all

 

 

 

POST /sms-logs-index/sms-logs-type/_search
{
  "query": {
    "match_all": {}
  }
  }

  注:ES默认查询10条数据

 @Test
    public void  matchAllQuery() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);
        
        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.matchAllQuery());
        builder.size(20);//ES默认查询10条数据
            request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
         for (SearchHit hit: rsp.getHits().getHits()) {
             System.out.println(hit.getSourceAsMap());


        }
    }

  

2.2.2match查询

 

POST /sms-logs-index/sms-logs-type/_search
{
  "query": {
    "match": {
      "smsContent": "恭喜"
    }
  }
}
  @Test
    public void  matchQuery() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("smsContent","恭喜"));
        builder.size(20);//ES默认查询10条数据
        request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());


        }
    }

  2.2.3布尔match查询

 

 

 

 

    public void  booleanMatchQuery() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("smsContent","恭喜 高级").operator(Operator.AND));
        builder.size(20);//ES默认查询10条数据
        request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());


        }
    }

  2.2.4mulitMatch查询

 

 

 @Test
    public void  mulitMatchQuery() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.multiMatchQuery("北京","province","smsContent"));
        builder.size(20);//ES默认查询10条数据
        request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());


        }
    }

  

2.3id&ids查询

2.3.1id查询

 

 

 

 @Test
    public  void findByid() throws IOException {
        GetRequest request=new GetRequest(index,type,"1");
        GetResponse rsp=client.get(request, RequestOptions.DEFAULT);
        System.out.println(rsp.getSourceAsMap());

    }

 

 

2.3.2ids查询

 

    @Test
    public void findByids() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.idsQuery().addIds("1","2","3"));
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

 

2.4prefix查询

 

 

 @Test
    public void findByPrefix() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.prefixQuery("corpName","恭喜"));
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

2.5fuzzy查询

 

 

 

  @Test
    public void findByfuzzy() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.fuzzyQuery("corpName","恭喜您").prefixLength(2));
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }

2.6wildcard查询

 

 @Test
    public void findByWildcard() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.wildcardQuery("corpName","恭喜*"));
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
        }
    }
View Code

2.7range查询

 

 

 

 @Test
    public void findByrange() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.rangeQuery("fee").gte(5).lte(10));
        builder.size(20);//ES默认查询10条数据
        request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());


        }
    }
View Code

2.7regexp查询

 

 

 

 @Test
    public void findByRegexp() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);

        SearchSourceBuilder builder=new SearchSourceBuilder();
        builder.query(QueryBuilders.regexpQuery("mobile","123[0-9]{8}"));
        builder.size(20);//ES默认查询10条数据
        request.source(builder);

        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        for (SearchHit hit: rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());


        }
    }
View Code

2.8深分页Scroll

 

 

 

 

 

 

package com.qf.test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.utils.ESClient;
import org.elasticsearch.action.search.*;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.Scroll;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.io.IOException;

public class Demo7 {
    ObjectMapper mapper=new ObjectMapper();
    RestHighLevelClient client = ESClient.getClient();

    String index="sms-logs-index";
    String type="sms-logs-type";
    @Test
    public  void scrollQuery() throws IOException {
        //1.创建SearchRrequest对象
        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 rsp= client.search(request, RequestOptions.DEFAULT);
       String scrollId=rsp.getScrollId();
       System.out.println("---------首页--------");
        for (SearchHit hit:rsp.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
            
        }

        //5.循环/创建SearchScrollRequest
        while(true) {
            SearchScrollRequest searchScrollRequest=new SearchScrollRequest(scrollId);

            //6.指定scrollId生存时间
            searchScrollRequest.scroll(TimeValue.timeValueMinutes(1L));

            //7.执行查询返回的结果
            SearchResponse scrollRsp=client.scroll(searchScrollRequest,RequestOptions.DEFAULT);

            //8.判断是否查询到了数据,输出
          SearchHit[] hits =scrollRsp.getHits().getHits();
            if (hits!=null&& hits.length
            >0){
                System.out.println("----------下一页------------");
                for (SearchHit hit:rsp.getHits().getHits()) {
                    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("删除scrollId:" +clearScrollResponse.isSucceeded());
    }
}
View Code

2.9delete-by-query

 

 

 @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(6));
        //3.执行删除
       BulkByScrollResponse resp =client.deleteByQuery(request, RequestOptions.DEFAULT);
        //4.返回结果
        System.out.println(resp.toString());
    }
View Code

 3.0复合查询

3.0.1bool查询

POST /sms-logs-index/sms-logs-type/_search
{
  "query":{
    "bool":{
      "should": [
        {
          "term": {
            "province": {
              "value": "北京"
            }
          }
        },
        {
          "term": {
            "province": {
              "value": "南通"
            }
          }
        }
      ],
       "must_not": [
        {
          "term": {
            "operatorId": {
              "value": "2"
            }
          }
        }
      ],
      "must": [
        {
          "match": {
            "smsContent": "恭喜"
          }
        },
        {
          "match": {
            "smsContent": "黑卡"
          }
        }
      ]
      
    }
  }
}
@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"));
        boolQuery.must(QueryBuilders.matchQuery("smsContent","高级"));
        boolQuery.must(QueryBuilders.matchQuery("smsContent","黑卡"));

        builder.query(boolQuery);
        request.source(builder);
        //3.查询
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        //4.输出结果
        for (SearchHit hit:rsp.getHits().getHits()
             ) {
            System.out.println(hit.getSourceAsMap());
            
        }
    }
View Code

3.0.2boosting查询

@Test
    public  void BoostingQuery() throws IOException {
        //1.创建SearchRequest
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        //2.查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoostingQueryBuilder boostingQueryBuilder = QueryBuilders.boostingQuery(
                QueryBuilders.matchQuery("smsContent", "高级"),
                QueryBuilders.matchQuery("smsContent", "黑卡")
        ).negativeBoost(0.5f);
        builder.query(boostingQueryBuilder);
        request.source(builder);

        SearchResponse rsp = client.search(request, RequestOptions.DEFAULT);
        //4.输出结果
        for (SearchHit hit : rsp.getHits().getHits()
        ) {
            System.out.println(hit.getSourceAsMap());

        }
    }
View Code

 3.1filter查询

 

 @Test
    public void filter() throws IOException {
        //1.创建SearchRequest
        SearchRequest request = new SearchRequest(index);
        request.types(type);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery= QueryBuilders.boolQuery();
        
        boolQuery.filter(QueryBuilders.termQuery("corpName","银行"));
        boolQuery.filter(QueryBuilders.rangeQuery("fee").lte(8));
        
        builder.query(boolQuery);
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        //4.输出结果
        for (SearchHit hit:rsp.getHits().getHits()
        ) {
            System.out.println(hit.getSourceAsMap());

        }

    }
View Code

3.2高亮查询

 

 

 

 

 @Test
    public  void highLightQuery() throws IOException {
        SearchRequest request=new SearchRequest(index);
        request.types(type);
        SearchSourceBuilder builder = new SearchSourceBuilder();
        builder.query(QueryBuilders.matchQuery("smsContent","黑卡"));
        //指定高亮
        HighlightBuilder highlightBuilder=new HighlightBuilder();
        highlightBuilder.field("smsContent",10)
                .preTags("<font color='red>")
                .postTags("</font>");

        builder.highlighter(highlightBuilder);
        request.source(builder);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
     for (SearchHit hit:rsp.getHits().getHits()
                                 ) {
         System.out.println(hit.getHighlightFields().get("smsContent"));

        }
    }
View Code

 

3.3聚合查询

 

 

3.3.1去重计数查询(cardinality)

 

 

  @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);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        Cardinality agg=rsp.getAggregations().get("agg");
       long value= agg.getValue();
        System.out.println(value);
    }
View Code

3.3.2范围统计

 

 

 

 

 

 

 

    @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)
                .addUnboundedTo(10));
        request.source(builder);
        SearchResponse rsp = client.search(request, RequestOptions.DEFAULT);
        //返回结果
        Range agg = rsp.getAggregations().get("agg");

        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 ));
        }
    }
View Code

 

3.3.3统计聚合查询

 

 //统计聚合查询
    @Test
    public void extendsStates() 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);
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
        ExtendedStats agg=rsp.getAggregations().get("agg");
        double max =agg.getMax();
        double min = agg.getMin();
        System.out.println("fee最大值为:"+max+",最小值为:"+min);

    }
View Code

 3.4地图经纬度查询

 

 

 

 

 

 3.4.1地图检索方式

 

 

 3.4.2基于RESTFUL实现地图检索

 

 

 

 

 3.4.3java实现geo_polygon检索

@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<>();
        points.add(new GeoPoint(39.99878,116.298915));
        points.add(new GeoPoint(39.972576,116.29561));
        points.add(new GeoPoint(39.984739,116.327661));

        builder.query(QueryBuilders.geoPolygonQuery("location",points));
        request.source(builder);
        //3.执行查询
        SearchResponse rsp=client.search(request, RequestOptions.DEFAULT);
       //4.输出结果
        for (SearchHit hit:rsp.getHits().getHits()
        ) {
            System.out.println(hit.getSourceAsMap());

        }
    }
View Code

 

posted @ 2020-09-18 10:44  想赚点零花钱  阅读(2209)  评论(1编辑  收藏  举报