elasticsearch笔记(4) java操作es的查询_02----- match查询

一. match查询比较智能.体现在能够根据查询的字段类型不用, 采用不同的查询方式

  • 查询的是日期或者是数组, 会把你基于字符串查询内容转为日期或数值对待
  • 查询内容是keyword类型, 则match查询不会对你指定的查询进行分词.
  • 查询的内容是text类型, 则mathc查询会根据ik分词方式分词, 去分词库中匹配指定内容.

mathc查询的底层是多个term查询

1.1 match_all查询

elasticsearch笔记(4) java操作es的查询_02----- match查询

 

 

在java中实现mathc_all查询

 1     @Test
 2     public void match_allTest() throws IOException {
 3         //        1. 创建查询对象
 4         String index = "sms-logs-index";
 5         String type = "sms-logs-type";
 6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
 7         searchRequest.types(type);//指定类型
 8 //    2. 封装查询条件
 9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
10         searchSourceBuilder.query(QueryBuilders.matchAllQuery()); searchSourceBuilder.size(20)//不指定size则只查询10条
11         searchRequest.source(searchSourceBuilder);
12 
13         //        3.执行查询
14         // client执行
15         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
16         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
17         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
18         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
19 //        4.获取数据(source中的数据)
20         SearchHit[] hits = search.getHits().getHits();
21         for (SearchHit searchHit : hits) {
22             Map<String, Object> map = searchHit.getSourceAsMap();
23             System.out.println(map);
24         }
25     }

 

1.2 mathc查询

kibana中用math查询

elasticsearch笔记(4) java操作es的查询_02----- match查询

 

 java中用es查询

    public void matchTest() throws IOException {
        //        1. 创建查询对象
        String index = "sms-logs-index";
        String type = "sms-logs-type";
        SearchRequest searchRequest = new SearchRequest(index);//指定索引
        searchRequest.types(type);//指定类型
//    2. 封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(20);
        searchSourceBuilder.query(QueryBuilders.matchQuery("smsContent","电话玩家活物"));
        searchRequest.source(searchSourceBuilder);


        //        3.执行查询
        // client执行
        HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        4.获取数据(source中的数据)
        SearchHit[] hits = search.getHits().getHits();
        for (SearchHit searchHit : hits) {
            Map<String, Object> map = searchHit.getSourceAsMap();
            System.out.println(map);
        }
    }

 

 

1.3布尔match查询 : and 或 or 作为查询条件. 

elasticsearch笔记(4) java操作es的查询_02----- match查询

 

 

java操作es布尔match

  @Test
    public void matchBooleanTest() throws IOException {
        //        1. 创建查询对象
        String index = "sms-logs-index";
        String type = "sms-logs-type";
        SearchRequest searchRequest = new SearchRequest(index);//指定索引
        searchRequest.types(type);//指定类型
//    2. 封装查询条件
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.size(20);
        searchSourceBuilder.query(QueryBuilders.matchQuery("smsContent", "电话玩家活物")
                                                      .operator(Operator.OR));//或者and
        searchRequest.source(searchSourceBuilder);


        //        3.执行查询
        // client执行
        HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
        RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
//        4.获取数据(source中的数据)
        SearchHit[] hits = search.getHits().getHits();
        for (SearchHit searchHit : hits) {
            Map<String, Object> map = searchHit.getSourceAsMap();
            System.out.println(map);
        }
    }

 

 

1.4 多条件match查询: multi_match . 

       针对多个field条件查询: 如条件是"北京",field里可以用province和smsContent中两个field里, 只要有一个field中有ik分词器分词的条件后. 就会返回

elasticsearch笔记(4) java操作es的查询_02----- match查询

 

 multiquery在java中的应用

 1     @Test
 2     public void matchMultiTest() throws IOException {
 3         //        1. 创建查询对象
 4         String index = "sms-logs-index";
 5         String type = "sms-logs-type";
 6         SearchRequest searchRequest = new SearchRequest(index);//指定索引
 7         searchRequest.types(type);//指定类型
 8 //    2. 封装查询条件
 9         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
10         searchSourceBuilder.size(20);
11         searchSourceBuilder.query(QueryBuilders.multiMatchQuery("电话玩家活物", "province","smsContent"));
12         searchRequest.source(searchSourceBuilder);
13 
14 
15         //        3.执行查询
16         // client执行
17         HttpHost httpHost = new HttpHost("192.168.43.30", 9200);
18         RestClientBuilder restClientBuilder = RestClient.builder(httpHost);
19         RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
20         SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
21 //        4.获取数据(source中的数据)
22         SearchHit[] hits = search.getHits().getHits();
23         for (SearchHit searchHit : hits) {
24             Map<String, Object> map = searchHit.getSourceAsMap();
25             System.out.println(map);
26         }
27     }

 

上一篇:elasticsearch笔记(4) java操作es的查询_04----- prefix查询 fuzzy查询 wildcard查询 range查询 regexp查询


下一篇:ESClient