030 elasticsearch查询、聚合

文章目录

SpringData对ES客户端的封装:ElasticsearchRestTemplate
SpringData对CRUD的封装:ElasticsearchRepository
原生ES客户端:RestHighLevelClient

查询

java 复制代码
package com.xd.cubemall.sdes;


import com.xd.cubemall.search.CubemallSearchApplication;
import com.xd.cubemall.search.model.Blog;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = CubemallSearchApplication.class)
public class QueryTest {
    @Autowired
    private ElasticsearchRestTemplate template;

    @Test
    public void testQuery1() {
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.matchAllQuery())
                .build();
        SearchHits<Blog> searchHits = template.search(query, Blog.class);
        //取总记录数
        long totalHits = searchHits.getTotalHits();
        System.out.println("总记录数:" + totalHits);
        //取文档列表
        List<SearchHit<Blog>> hits = searchHits.getSearchHits();
        hits.stream().forEach(h-> System.out.println(h));

    }


    @Test
    public void testQuery2() throws Exception {
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                //查询设置
                .withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))
                //过滤条件,可以设置多个,支持查询条件
                .withFilter(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("title", "电影"))
                        .should(QueryBuilders.termQuery("content","祝福"))
                )
//                .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))
                //分页设置
                .withPageable(PageRequest.of(0, 10))
                //高亮设置
                .withHighlightBuilder(new HighlightBuilder()
                        //高亮显示的字段
                        .field("title")
                        .field("content")
                        //高亮显示的前缀
                        .preTags("<em>")
                        //高亮显示的后缀
                        .postTags("</em>")
                ).build();
        //使用template对象执行查询
        SearchHits<Blog> searchHits = template.search(query, Blog.class);
        //取返回结果
        //总记录数
        long totalHits = searchHits.getTotalHits();
        System.out.println("总记录数:" + totalHits);
        //取文档列表
        List<SearchHit<Blog>> hits = searchHits.getSearchHits();
        hits.stream().forEach(hit->{
            //取文档对象
            Blog blog = hit.getContent();
            //System.out.println(blog);
            //取高亮结果
            Map<String,List<String>> highlightFields = hit.getHighlightFields();
            //System.out.println(highlightFields);
            String title = highlightFields.get("title").get(0);
            String content = highlightFields.get("content").get(0);
            blog.setTitle(title);
            blog.setContent(content);
            System.out.println(blog);

        });
    }
}

聚合查询

api使用方法

搜索时,请求体中包含聚合条件

在aggs属性中设置聚合条件:

value_count

max

min

avg

json 复制代码
GET blog_1/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "mobile_count": {
      "value_count": {
        "field": "mobile"
      }
    },
    "mobile_max":{
      "max": {
        "field": "mobile"
      }
    }
  }
}

分桶(分组)聚合

json 复制代码
GET blog_1/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "mobile_count": {
      "value_count": {
        "field": "mobile"
      }
    },
    "mobile_group":{
      "terms": {
        "field": "mobile",
        "size": 10
      }
    }
  }
}

RestHighLevelClient

java 复制代码
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    @Test
    public void testAggs() throws IOException {
        SearchRequest request = new SearchRequest()
                .indices("blog_1")
                .source(new SearchSourceBuilder()
                        .query(QueryBuilders.matchAllQuery())
                        .aggregation(new ValueCountAggregationBuilder("doc_count").field("mobile"))
                        .aggregation(new TermsAggregationBuilder("group_count").field("mobile").size(10))
                );
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        System.out.println(response);
    }

ElasticsearchRestTemplat

java 复制代码
    @Test
    public void testQuery2() throws Exception {
        NativeSearchQuery query = new NativeSearchQueryBuilder()
                //查询设置
                .withQuery(QueryBuilders.multiMatchQuery("看电影","title","content"))
                //过滤条件,可以设置多个,支持查询条件
                .withFilter(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("title", "电影"))
                        .should(QueryBuilders.termQuery("content","祝福"))
                )
//                .withFilter(QueryBuilders.termQuery("mobile", "13000000000"))
                //分页设置
                .withPageable(PageRequest.of(0, 10))
                //高亮设置
                .withHighlightBuilder(new HighlightBuilder()
                        //高亮显示的字段
                        .field("title")
                        .field("content")
                        //高亮显示的前缀
                        .preTags("<em>")
                        //高亮显示的后缀
                        .postTags("</em>")
                )
                .addAggregation(new TermsAggregationBuilder("mobile_group").field("mobile"))
                .build();
        //使用template对象执行查询
        SearchHits<Blog> searchHits = template.search(query, Blog.class);
        //取返回结果
        //总记录数
        long totalHits = searchHits.getTotalHits();
        System.out.println("总记录数:" + totalHits);
        //取文档列表
        List<SearchHit<Blog>> hits = searchHits.getSearchHits();
        hits.stream().forEach(hit->{
            //取文档对象
            Blog blog = hit.getContent();
            //System.out.println(blog);
            //取高亮结果
            Map<String,List<String>> highlightFields = hit.getHighlightFields();
            //System.out.println(highlightFields);
            String title = highlightFields.get("title").get(0);
            String content = highlightFields.get("content").get(0);
            blog.setTitle(title);
            blog.setContent(content);
            System.out.println(blog);

        });
        Aggregations aggregations = searchHits.getAggregations();
        ParsedStringTerms aggregation = aggregations.get("mobile_group");
        List<? extends Terms.Bucket> buckets = aggregation.getBuckets();
        buckets.forEach(e-> {
            System.out.println(e.getKeyAsString());
            System.out.println(e.getDocCount());

        });


    }
相关推荐
java1234_小锋8 小时前
Elasticsearch是如何实现Master选举的?
大数据·elasticsearch·搜索引擎
梦幻通灵14 小时前
ES分词环境实战
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客14 小时前
Elasticsearch 中的热点以及如何使用 AutoOps 解决它们
大数据·运维·elasticsearch·搜索引擎·全文检索
小黑屋说YYDS20 小时前
ElasticSearch7.x入门教程之索引概念和基础操作(三)
elasticsearch
Java 第一深情1 天前
Linux上安装单机版ElasticSearch6.8.1
linux·elasticsearch·全文检索
KevinAha2 天前
Elasticsearch 6.8 分析器
elasticsearch
wuxingge2 天前
elasticsearch7.10.2集群部署带认证
运维·elasticsearch
Elastic 中国社区官方博客2 天前
Elasticsearch:如何部署文本嵌入模型并将其用于语义搜索
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
Dreams°1232 天前
【大数据测试 Elasticsearch 的标准--超详细篇】
大数据·elasticsearch·jenkins
鸠摩智首席音效师2 天前
如何在 Elasticsearch 中配置 SSL / TLS ?
elasticsearch·ssl