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"
            }
        }
    }
}

es如何聚合查询同一字段文档数大于1的文档_第1张图片

查询结果
es如何聚合查询同一字段文档数大于1的文档_第2张图片

导入数据:

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并且年龄等于18的

POST peron_test/_search
{
  "size": 0,
  "aggs": {
    "duplicate_names": {
      "terms": {
        "field": "name",
        "min_doc_count": 2
      },
      "aggs": {
        "age_filter": {
          "filter": {
            "term": {
              "age": 18
            }
          }
        }
      }
    }
  }
}

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;
    }
}

你可能感兴趣的:(elasticsearch,chrome,数据库)