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());

        });


    }
相关推荐
woshiabc1113 小时前
windows安装Elasticsearch及增删改查操作
大数据·elasticsearch·搜索引擎
arnold666 小时前
探索 ElasticSearch:性能优化之道
大数据·elasticsearch·性能优化
成长的小牛2338 小时前
es使用knn向量检索中numCandidates和k应该如何配比更合适
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客9 小时前
Elasticsearch:什么是查询语言?
大数据·数据库·elasticsearch·搜索引擎·oracle
启明真纳10 小时前
elasticache备份
运维·elasticsearch·云原生·kubernetes
幽弥千月19 小时前
【ELK】ES单节点升级为集群并开启https【亲测可用】
elk·elasticsearch·https
运维&陈同学19 小时前
【Elasticsearch05】企业级日志分析系统ELK之集群工作原理
运维·开发语言·后端·python·elasticsearch·自动化·jenkins·哈希算法
Y编程小白1 天前
Git版本控制工具--基础命令和分支管理
大数据·git·elasticsearch
酱学编程2 天前
ES搜索原理
大数据·elasticsearch·搜索引擎
龙少95432 天前
【SpringBoot中怎么使用ElasticSearch】
spring boot·elasticsearch·jenkins