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

        });


    }
相关推荐
御前一品带刀侍卫24 分钟前
elasticsearch基础
大数据·elasticsearch·搜索引擎
武子康1 小时前
大数据-191 Elasticsearch - ES 集群模式 配置启动 规划调优
java·大数据·elk·elasticsearch·搜索引擎·全文检索
落落落sss1 小时前
es实现自动补全
大数据·服务器·elasticsearch·搜索引擎·全文检索
小汤猿人类1 小时前
什么是Elasticsearch?
大数据·elasticsearch·搜索引擎
武子康1 小时前
大数据-187 Elasticsearch - ELK 家族 Logstash Filter 插件 使用详解
大数据·数据结构·elk·elasticsearch·搜索引擎·全文检索·1024程序员节
执键行天涯4 小时前
【工具使用】VSCode如何将本地项目关联到远程的仓库 (vscode本地新项目与远程仓库建立链接)
ide·vscode·elasticsearch
再拼一次吧7 小时前
Elasticsearch
大数据·elasticsearch·搜索引擎
、十一、11 小时前
Linux中ES的安装
linux·运维·elasticsearch
苏少朋16 小时前
Es概念理解 ▎Es索引库操作 ▎Es文档操作
大数据·elasticsearch·搜索引擎
小小工匠16 小时前
ElasticSearch - Bucket Selector使用指南
elasticsearch·bucket sort