nested字段聚合

PUT test_aggr
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    },
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "aa": {
                "type": "nested",
                "properties": {
                    "bb": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}
PUT test_aggr/_doc/1  
{  
  "id":"1",
  "aa": [  
    {  
      "bb": "value1"  
    },  
    {  
      "bb": "value2"  
    }  
  ]  
}

PUT test_aggr/_doc/2  
{   
  "id":"2",
  "aa": [  
    {  
      "bb": "value3"  
    },  
    {  
      "bb": "value4"  
    }  
  ]  
}

PUT test_aggr/_doc/3
{  
  "id":"3" 
}


PUT test_aggr/_doc/4
{   
  "id":"4",
  "aa": [  
    {  
       
    }
  ]  
}

PUT test_aggr/_doc/5
{   
  "id":"5",
  "aa": [  

  ]  
}
POST test_aggr/_search
{
  "size": 0,
  "aggs": {
    "aa_values": {
      "nested": {
        "path": "aa"
      },
      "aggs": {
        "bb_terms": {
          "terms": {
            "field": "aa.bb"
          }
        }
      }
    }
  }
}

如何查询出这2个bb为空的数据?

POST test_aggr/_search
{
  "query": {
    "bool": {
      "must_not": {
        "nested": {
          "path": "aa",
          "query": {
            "exists": {
              "field": "aa.bb"
            }
          }
        }
      }
    }
  }
}
import org.apache.http.HttpHost;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ElasticsearchJavaExample {

    public static void main(String[] args) throws IOException {
        // 配置 Elasticsearch 主机和端口
        RestClientBuilder builder = RestClient.builder(new HttpHost("your_elasticsearch_host", 9200));

        // 创建高级 REST 客户端
        RestHighLevelClient client = new RestHighLevelClient(builder);

        try {
            // 创建查询请求
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.boolQuery()
                    .mustNot(QueryBuilders.nestedQuery("aa",
                            QueryBuilders.existsQuery("aa.bb"), ScoreMode.None)));

            Request request = new Request("POST", "test_aggr/_search");
            request.setJsonEntity(sourceBuilder.toString());
            request.setOptions(RequestOptions.DEFAULT);

            // 执行查询请求
            Response response = client.getLowLevelClient().performRequest(request);

            // 处理查询结果
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("Query Response: " + responseBody);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

对一层嵌套字段做聚合
{
“size”: 0,
“query”: {
“match_all”: {}
},
“aggs”: {
“aa_values”: {
“nested”: {
“path”: “aa”
},
“aggs”: {
“bb_terms”: {
“terms”: {
“field”: “aa.bb”
}
}
}
}
}
}

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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
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.nested.NestedAggregationBuilder;

import java.io.IOException;

public class ElasticsearchJavaQuery {
public static void main(String[] args) throws IOException {
// 创建Elasticsearch客户端
RestHighLevelClient client = new RestHighLevelClient();

    // 创建SearchRequest
    SearchRequest searchRequest = new SearchRequest("test_aggr");
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.size(0); // 设置size为0以仅获取聚合结果

    // 创建Nested Aggregation
    NestedAggregationBuilder nestedAggregation = AggregationBuilders.nested("aa_values", "aa");

    // 创建Terms Aggregation
    TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("bb_terms").field("aa.bb");

    // 将Terms Aggregation添加到Nested Aggregation中
    nestedAggregation.subAggregation(termsAggregation);

    // 将Nested Aggregation添加到SearchSourceBuilder
    searchSourceBuilder.aggregation(nestedAggregation);

    searchRequest.source(searchSourceBuilder);

    // 执行查询
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

    // 处理查询结果
    // 可以通过searchResponse获取聚合结果
    // searchResponse.getAggregations()...

    // 关闭Elasticsearch客户端
    client.close();
}

}

聚合结果:

{
“took” : 0,
“timed_out” : false,
“_shards” : {
“total” : 3,
“successful” : 3,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : null,
“hits” : [ ]
},
“aggregations” : {
“aa_values” : {
“doc_count” : 4,
“bb_terms” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [
{
“key” : “value1”,
“doc_count” : 1
},
{
“key” : “value2”,
“doc_count” : 1
},
{
“key” : “value3”,
“doc_count” : 1
},
{
“key” : “value4”,
“doc_count” : 1
}
]
}
}
}
}

===============================================================
二层嵌套
PUT test_aggr_double
{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 2
},
“mappings”: {
“properties”: {
“AA”: {
“type”: “nested”,
“properties”: {
“BB”: {
“type”: “nested”,
“properties”: {
“CC”: {
“type”: “keyword”
},
“DD”: {
“type”: “keyword”
}
}
}
}
}
}
}
}

PUT test_aggr_double/_doc/1
{
“AA”: [
{
“BB”: [
{
“CC”: “value1”,
“DD”: “value2”
},
{
“CC”: “value3”,
“DD”: “value4”
}
]
},
{
“BB”: [
{
“CC”: “value5”,
“DD”: “value6”
},
{
“CC”: “value7”,
“DD”: “value8”
}
]
}
]
}

PUT test_aggr_double/_doc/2
{
“AA”: [
{
“BB”: [
{
“CC”: “value9”,
“DD”: “value10”
},
{
“CC”: “value11”,
“DD”: “value12”
}
]
},
{
“BB”: [
{
“CC”: “value13”,
“DD”: “value14”
},
{
“CC”: “value15”,
“DD”: “value16”
}
]
}
]
}

GET test_aggr_double/_search

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 3,
“successful” : 3,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : 1.0,
“hits” : [
{
“_index” : “test_aggr_double”,
“_type” : “_doc”,
“_id” : “2”,
“_score” : 1.0,
“_source” : {
“AA” : [
{
“BB” : [
{
“CC” : “value9”,
“DD” : “value10”
},
{
“CC” : “value11”,
“DD” : “value12”
}
]
},
{
“BB” : [
{
“CC” : “value13”,
“DD” : “value14”
},
{
“CC” : “value15”,
“DD” : “value16”
}
]
}
]
}
},
{
“_index” : “test_aggr_double”,
“_type” : “_doc”,
“_id” : “1”,
“_score” : 1.0,
“_source” : {
“AA” : [
{
“BB” : [
{
“CC” : “value1”,
“DD” : “value2”
},
{
“CC” : “value3”,
“DD” : “value4”
}
]
},
{
“BB” : [
{
“CC” : “value5”,
“DD” : “value6”
},
{
“CC” : “value7”,
“DD” : “value8”
}
]
}
]
}
}
]
}
}

POST test_aggr_double/_search
{
“size”: 0,
“aggs”: {
“dd_aggregation”: {
“nested”: {
“path”: “AA.BB”
},
“aggs”: {
“dd_terms”: {
“terms”: {
“field”: “AA.BB.DD”,
“size”: 10
}
}
}
}
}
}

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.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ElasticsearchAggregationExample {
public static void main(String[] args) throws IOException {
// 创建Elasticsearch客户端
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost(“localhost”, 9200, “http”)
));

    // 创建聚合查询
    SearchRequest searchRequest = new SearchRequest("test_aggr_double");
    SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();

    // 设置大小为0,因为我们不需要返回文档,只需要聚合结果
    sourceBuilder.size(0);

    // 创建嵌套聚合
    NestedAggregationBuilder nestedAggregation = AggregationBuilders.nested("dd_aggregation", "AA.BB");
    sourceBuilder.aggregation(nestedAggregation);

    // 在嵌套聚合内创建terms聚合
    TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("dd_terms").field("AA.BB.DD").size(10);
    nestedAggregation.subAggregation(termsAggregation);

    searchRequest.source(sourceBuilder);

    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

        // 处理聚合结果
        // 这里可以提取聚合结果并对其进行进一步处理
    } catch (IOException e) {
        e.printStackTrace();
    }

    // 关闭Elasticsearch客户端
    client.close();
}

}

{
“took” : 1,
“timed_out” : false,
“_shards” : {
“total” : 3,
“successful” : 3,
“skipped” : 0,
“failed” : 0
},
“hits” : {
“total” : {
“value” : 2,
“relation” : “eq”
},
“max_score” : null,
“hits” : [ ]
},
“aggregations” : {
“dd_aggregation” : {
“doc_count” : 8,
“dd_terms” : {
“doc_count_error_upper_bound” : 0,
“sum_other_doc_count” : 0,
“buckets” : [
{
“key” : “value10”,
“doc_count” : 1
},
{
“key” : “value12”,
“doc_count” : 1
},
{
“key” : “value14”,
“doc_count” : 1
},
{
“key” : “value16”,
“doc_count” : 1
},
{
“key” : “value2”,
“doc_count” : 1
},
{
“key” : “value4”,
“doc_count” : 1
},
{
“key” : “value6”,
“doc_count” : 1
},
{
“key” : “value8”,
“doc_count” : 1
}
]
}
}
}
}

你可能感兴趣的:(运维,elasticsearch)