nested字段聚合

bash 复制代码
PUT test_aggr
{
    "settings": {
        "number_of_shards": 3,
        "number_of_replicas": 2
    },
    "mappings": {
        "properties": {
            "id": {
                "type": "keyword"
            },
            "aa": {
                "type": "nested",
                "properties": {
                    "bb": {
                        "type": "keyword"
                    }
                }
            }
        }
    }
}
bash 复制代码
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": [  

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

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

bash 复制代码
POST test_aggr/_search
{
  "query": {
    "bool": {
      "must_not": {
        "nested": {
          "path": "aa",
          "query": {
            "exists": {
              "field": "aa.bb"
            }
          }
        }
      }
    }
  }
}
bash 复制代码
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

}

]

}

}

}

}

相关推荐
吃肉不能购1 小时前
Label-studio-ml-backend 和YOLOV8 YOLO11自动化标注,目标检测,实例分割,图像分类,关键点估计,视频跟踪
运维·yolo·自动化
学Linux的语莫1 小时前
Ansible使用简介和基础使用
linux·运维·服务器·nginx·云计算·ansible
qq_312920111 小时前
docker 部署 kvm 图形化管理工具 WebVirtMgr
运维·docker·容器
学Linux的语莫1 小时前
搭建服务器VPN,Linux客户端连接WireGuard,Windows客户端连接WireGuard
linux·运维·服务器
黑牛先生1 小时前
【Linux】进程-PCB
linux·运维·服务器
Karoku0661 小时前
【企业级分布式系统】ELK优化
运维·服务器·数据库·elk·elasticsearch
安迁岚3 小时前
【SQL Server】华中农业大学空间数据库实验报告 实验三 数据操作
运维·服务器·数据库·sql·mysql
打码人的日常分享3 小时前
商用密码应用安全性评估,密评整体方案,密评管理测评要求和指南,运维文档,软件项目安全设计相关文档合集(Word原件)
运维·安全·web安全·系统安全·规格说明书
上优4 小时前
uniapp 选择 省市区 省市 以及 回显
大数据·elasticsearch·uni-app
追风赶月、4 小时前
【Linux】线程概念与线程控制
linux·运维·服务器