java操作ElasticSearch聚合查询的示例代码

 更新时间:2024年08月02日 10:23:12   作者:诸葛博仌  
这篇文章主要介绍了java操作ElasticSearch聚合查询的示例代码,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧

1、引入依赖

<!-- es工具-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>

2、配置es连接信息

sring:
  elasticsearch:
    rest:
      uris: "http://localhost:9200"

3、构建索引

@Document(indexName = "test_index_person")
public class TestPerson{
    public TestPerson( String personName, Integer personId, String gmtCreate, String gmtModified) {
        this.personName = personName;
        this.personId = personId;
        this.gmtCreate = gmtCreate;
        this.gmtModified = gmtModified;
    }
    public TestPerson() {
    }
    @Override
    public String toString() {
        return "TestPerson{" +
                "id='" + id + '\'' +
                ", personName='" + personName + '\'' +
                ", personId=" + personId +
                ", gmtCreate=" + gmtCreate +
                ", gmtModified=" + gmtModified +
                '}';
    }
    @Id
    private String id;
    @Field(type = FieldType.Text, analyzer = "ik_analyzer",name = "person_name")
    private String personName;
    @Field(type = FieldType.Integer,name = "person_id")
    private Integer personId;
    @Field(type = FieldType.Date,name = "gmt_create")
    private String gmtCreate;
    @Field(type = FieldType.Date,name = "gmt_modified")
    private String gmtModified;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPersonName() {
        return personName;
    }
    public void setPersonName(String personName) {
        this.personName = personName;
    }
    public Integer getPersonId() {
        return personId;
    }
    public void setPersonId(Integer personId) {
        this.personId = personId;
    }
    public String getGmtCreate() {
        return gmtCreate;
    }
    public void setGmtCreate(String gmtCreate) {
        this.gmtCreate = gmtCreate;
    }
    public String getGmtModified() {
        return gmtModified;
    }
    public void setGmtModified(String gmtModified) {
        this.gmtModified = gmtModified;
    }
}

4、查询(包含自定义聚合查询)

    @Autowired
    ElasticsearchOperations elasticsearchOperations;
    /***
     * 自定义 match 查询
     */
    @Test
    public void test6(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("person_name", "小黑子");
        queryBuilder.withQuery(matchQueryBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 自定义 term 查询
     */
    @Test
    public void test7(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        TermQueryBuilder personId = QueryBuilders.termQuery("person_id", "1");
        queryBuilder.withQuery(personId);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 自定义 match_all 查询
     */
    @Test
    public void test8(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        queryBuilder.withQuery(matchAllQueryBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 自定义 match_phrase 查询
     */
    @Test
    public void test9(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        MatchPhraseQueryBuilder matchPhraseQueryBuilder = QueryBuilders.matchPhraseQuery("person_name", "小黑子");
        queryBuilder.withQuery(matchPhraseQueryBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 范围查询
     */
    @Test
    public void test10(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("gmt_create");
        rangeQuery.gte("2024-01-31T11:00:00");
        rangeQuery.lt("2024-07-31T11:00:00");
        queryBuilder.withQuery(rangeQuery);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 模糊匹配
     */
    @Test
    public void test11(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 添加基本分词查询
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("person_name", "张三");
        queryBuilder.withQuery(fuzzyQueryBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 布尔查询 必须匹配(must) 过滤(filter) 至少匹配一个(should)
     */
    @Test
    public void test12(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 构建布尔查询
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //布尔查询下添加must
        List<QueryBuilder> must = boolQueryBuilder.must();
        TermQueryBuilder personId = QueryBuilders.termQuery("person_id", "1");
        must.add(personId);
        //布尔查询下添加filter
        List<QueryBuilder> filter = boolQueryBuilder.filter();
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("gmt_create");
        rangeQuery.gte("2024-07-31T10:00:00");
        filter.add(rangeQuery);
       //布尔查询下添加should
        List<QueryBuilder> should = boolQueryBuilder.should();
        TermQueryBuilder shouldTerm = QueryBuilders.termQuery("person_id", "111");
        should.add(shouldTerm);
        //布尔查询下添加must_not
        List<QueryBuilder> queryBuilders = boolQueryBuilder.mustNot();
        TermQueryBuilder mustNotTerm = QueryBuilders.termQuery("person_name", "哈哈哈");
        queryBuilders.add(mustNotTerm);
        //构建布尔查询语法,准备执行
        queryBuilder.withQuery(boolQueryBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 分页
     */
    @Test
    public void test13(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        // 查询全部
        MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
        queryBuilder.withQuery(matchAllQueryBuilder);
        Pageable pageable = PageRequest.of(0, 10, Sort.by("personId").descending());
        queryBuilder.withPageable(pageable);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
    }
    /***
     * 对查询出来的值进行聚合查询 统计每个唯一值的个数
     */
    @Test
    public void test14(){
        // 构建查询条件
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //查询数据
        MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("person_name", "张三");
        queryBuilder.withQuery(matchQuery);
        //聚合数据
        TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("test_aggs").field("gmt_create");
        queryBuilder.addAggregation(termsAggregationBuilder);
        SearchHits<TestPerson> searchHits = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        searchHits.forEach(item ->{
            System.out.println(item);
        });
        Aggregations aggregations = searchHits.getAggregations();
        // 检查 aggregations 是否为 null
        if (aggregations != null) {
            // 第二步:通过聚合名称获取特定的聚合结果
            Terms testAggs = aggregations.get("test_aggs");
            // 检查特定聚合是否存在
            if (testAggs != null) {
                // 第三步:遍历聚合中的桶(buckets)
                for (Terms.Bucket bucket : testAggs.getBuckets()) {
                    // 获取桶的键(在这个例子中是 gmt_create 的值)
                    String keyAsString = bucket.getKeyAsString();
                    // 获取桶中文档的数量
                    long docCount = bucket.getDocCount();
                    // 打印或处理每个桶的结果
                    System.out.println("Key: " + keyAsString + ", DocCount: " + docCount);
                }
            }
        }
    }
    /***
     * 对查询出来的值进行聚合查询 统计每个唯一值的个数  仅仅返回聚合结果
     */
    @Test
    public void test15(){
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //仅仅返回聚合结果
        queryBuilder.withPageable(PageRequest.of(0,1));
        //查询数据
        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("person_name", "张三");
        queryBuilder.withQuery(matchQueryBuilder);
        //聚合
        TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("test_aggs").field("gmt_create");
        queryBuilder.addAggregation(aggregationBuilder);
        //获取聚合信息
        SearchHits<TestPerson> search = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        Aggregations aggregations = search.getAggregations();
        if (aggregations != null){
            Terms terms = aggregations.get("test_aggs");
            List<? extends Terms.Bucket> buckets = terms.getBuckets();
            buckets.forEach(item ->{
                System.out.println("key:"+item.getKeyAsString()  +"===="+"doc_count:"+item.getDocCount());
            });
        }
    }
    /***
     * 多个聚合
     */
    @Test
    public void test16(){
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        //仅仅返回聚合结果
        queryBuilder.withPageable(PageRequest.of(0,1));
        TermsAggregationBuilder test_aggs = AggregationBuilders.terms("test_aggs").field("gmt_create");
        queryBuilder.addAggregation(test_aggs);
        AvgAggregationBuilder test_aggs2 = AggregationBuilders.avg("test_aggs2").field("person_id");
        queryBuilder.addAggregation(test_aggs2);
        SearchHits<TestPerson> search = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        Aggregations aggregations = search.getAggregations();
        if (aggregations != null){
            Terms test_aggs1 = aggregations.get("test_aggs");
            if (test_aggs != null){
                test_aggs1.getBuckets().forEach(item ->{
                    System.out.println("key:"+ item.getKeyAsString()+"===="+"doc_count:"+item.getDocCount());
                });
            }
            Avg test2_aggs = aggregations.get("test_aggs2");
            System.out.println( test2_aggs.getValue());
        }
    }
    /***
     * 子聚合 先分组,再计算
     */
    @Test
    public void test17(){
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.withPageable(PageRequest.of(0,1));
        TermsAggregationBuilder test_aggs = AggregationBuilders.terms("test_aggs").field("gmt_create");
        test_aggs.subAggregation(AggregationBuilders.avg("son_aggs_test").field("person_id"));
        queryBuilder.addAggregation(test_aggs);
        SearchHits<TestPerson> search = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        Aggregations aggregations = search.getAggregations();
        Terms testAgges =  aggregations.get("test_aggs");
        testAgges.getBuckets().forEach(item ->{
            Avg aggregations1 = item.getAggregations().get("son_aggs_test");
            System.out.println("key:"+item.getKeyAsString()+"--doc_count:"+item.getKeyAsString()+"--son_aggs_test"+aggregations1.getValue());
        });
    }
    /**
     * 取聚合最大值
     */
    @Test
    public void test18(){
        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        queryBuilder.addAggregation(AggregationBuilders.max("test_agg").field("person_id"));
        SearchHits<TestPerson> search = elasticsearchOperations.search(queryBuilder.build(), TestPerson.class);
        Aggregations aggregations = search.getAggregations();
        Max test_agg = aggregations.get("test_agg");
        System.out.println(test_agg.getValue());
    }

到此这篇关于java操作ElasticSearch(包含聚合查询)的文章就介绍到这了,更多相关java操作ElasticSearch内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解SpringMVC注解@initbinder解决类型转换问题

    详解SpringMVC注解@initbinder解决类型转换问题

    本篇文章主要介绍了详解SpringMVC注解@initbinder解决类型转换问题,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • java使用CKEditor实现图片上传功能

    java使用CKEditor实现图片上传功能

    这篇文章主要为大家详细介绍了java使用CKEditor实现图片上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Java SE实现多人聊天室功能

    Java SE实现多人聊天室功能

    这篇文章主要为大家详细介绍了Java SE实现多人聊天室功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Mybatis映射文件实例详解

    Mybatis映射文件实例详解

    这篇文章主要介绍了 Mybatis映射文件实例详解,需要的朋友可以参考下
    2017-02-02
  • Java中的动态代理原理及实现

    Java中的动态代理原理及实现

    这篇文章主要介绍了Java中的动态代理原理及实现,动态是相对于静态而言,何为静态,即编码时手动编写代理类、委托类,而动态呢,是不编写具体实现类,等到使用时,动态创建一个来实现代理的目的,需要的朋友可以参考下
    2023-12-12
  • Java基础之枚举Enum类案例详解

    Java基础之枚举Enum类案例详解

    这篇文章主要介绍了Java基础之枚举Enum类案例详解,文中有非常详细的代码示例,对正在学习java基础的小伙伴们有很好的帮助,需要的朋友可以参考下
    2021-05-05
  • Java后台返回blob格式的文件流的解决方案

    Java后台返回blob格式的文件流的解决方案

    在Java后台开发中,经常会遇到需要返回Blob格式的文件流给前端的情况,Blob是一种二进制大对象类型,可以用于存储大量的二进制数据,例如图片、音频、视频等,本文将为你详细介绍如何在Java后台中返回Blob格式的文件流,需要的朋友可以参考下
    2024-08-08
  • java.lang.ExceptionInInitializerError初始化程序中的异常错误的解决

    java.lang.ExceptionInInitializerError初始化程序中的异常错误的解决

    java.lang.ExceptionInInitializerError 异常在 Java 中表示一个错误,该错误发生在尝试初始化一个类的静态变量、静态代码块或枚举常量时,本文就来介绍并解决一下,感兴趣的可以了解一下
    2024-05-05
  • Java Apollo环境搭建以及集成SpringBoot案例详解

    Java Apollo环境搭建以及集成SpringBoot案例详解

    这篇文章主要介绍了Java Apollo环境搭建以及集成SpringBoot案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java中的ThreadLocal详解

    Java中的ThreadLocal详解

    这篇文章主要介绍了Java中的ThreadLocal详解,ThreadLocal 是一个线程局部变量,其实的功用非常简单,就是为每一个使用该变量的线程都提供一个变量值的副本,是Java中一种较为特殊的线程绑定机制,需要的朋友可以参考下
    2023-09-09

最新评论