• es如何聚合查询同一字段文档数大于1的文档


    1.mapping

    {
        "settings": {
            "number_of_shards": 3,
            "number_of_replicas": 2
        },
        "mappings": {
             "dynamic": "true",
            "properties": {
                "id": {
                    "type": "keyword"
                },
                "name": {
                    "type": "keyword"
                },
                "sex": {
                    "type": "keyword"
                },
                "age": {
                    "type": "integer"
                },
                "school": {
                    "type": "keyword"
                }
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    查询结果
    在这里插入图片描述

    导入数据:

    POST peron_test/_bulk
    { "index": {} }
    { "id": 1, "name": "张一", "sex": "男", "age": 18, "school": "深圳大学" }
    { "index": {} }
    { "id": 2, "name": "张二", "sex": "男", "age": 19, "school": "深圳大学" }
    { "index": {} }
    { "id": 3, "name": "张一", "sex": "男", "age": 20, "school": "深圳大学" }
    { "index": {} }
    { "id": 4, "name": "张三", "sex": "男", "age": 21, "school": "深圳大学" }
    { "index": {} }
    { "id": 5, "name": "张五", "sex": "男", "age": 22, "school": "深圳大学" }
    { "index": {} }
    { "id": 6, "name": "张一", "sex": "男", "age": 23, "school": "深圳大学" }
    { "index": {} }
    { "id": 7, "name": "张一", "sex": "男", "age": 24, "school": "深圳大学" }
    { "index": {} }
    { "id": 8, "name": "张一", "sex": "男", "age": 25, "school": "深圳大学" }
    { "index": {} }
    { "id": 9, "name": "张六", "sex": "男", "age": 26, "school": "深圳大学" }
    { "index": {} }
    { "id": 10, "name": "张六", "sex": "男", "age": 27, "school": "深圳大学" }
    { "index": {} }
    { "id": 11, "name": "张五", "sex": "男", "age": 28, "school": "深圳大学" }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    聚合查询出人名重复数大于1并且年龄等于18的

    POST peron_test/_search
    {
      "size": 0,
      "aggs": {
        "duplicate_names": {
          "terms": {
            "field": "name",
            "min_doc_count": 2
          },
          "aggs": {
            "age_filter": {
              "filter": {
                "term": {
                  "age": 18
                }
              }
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    java:

    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.aggregations.AggregationBuilders;
    import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
    import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
    
    public class ElasticsearchQuery {
    
        public static void main(String[] args) {
            // 初始化 Elasticsearch 客户端
            RestHighLevelClient client = createElasticsearchClient();
    
            // 创建搜索请求
            SearchRequest searchRequest = new SearchRequest("person_test");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
    
            // 创建主聚合
            TermsAggregationBuilder duplicateNamesAggregation = AggregationBuilders
                .terms("duplicate_names")
                .field("name")
                .minDocCount(2);
    
            // 创建子聚合 - 年龄过滤
            FilterAggregationBuilder ageFilterAggregation = AggregationBuilders
                .filter("age_filter", QueryBuilders.termQuery("age", 18));
            
            // 将子聚合添加到主聚合
            duplicateNamesAggregation.subAggregation(ageFilterAggregation);
    
            // 设置聚合到搜索请求中
            sourceBuilder.aggregation(duplicateNamesAggregation);
            searchRequest.source(sourceBuilder);
    
            try {
                // 执行搜索请求
                SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    
                // 处理搜索结果
                // ...
    
                // 关闭 Elasticsearch 客户端
                client.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        // 初始化 Elasticsearch 客户端
        private static RestHighLevelClient createElasticsearchClient() {
            // 创建和配置 Elasticsearch 客户端
            // ...
    
            return client;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    Java登录管理功能的自我理解(尚庭公寓)
    微调大模型工具-LoRA
    SpringCloud 微服务全栈体系(二)
    小黑完成了最后一节健身课,顺利完成了跳绳比赛,乘飞机到达南京准备第二天领物资和南京城内闲逛的leetcode之旅:215. 数组中的第K个最大元素
    jSignature 横屏手写签名
    MySQL学习(八)——锁
    算法金 | 再见!!!K-means
    基于SSM的计算机技术论坛管理系统,高质量论文范例,可拿去就用
    使用R语言自带数据“USArrests”,利用K-means进行聚类分析。建议按照如下步骤进行
    分析Jetpack Compose动画内部是如何实现的
  • 原文地址:https://blog.csdn.net/jifgnie/article/details/134052619