如何在 java 中生成弹性搜索嵌套聚合?

How to generate elastic search nested aggregations in java?

我有以下聚合查询,我需要将其转换为 java

以下聚合查询需要使用弹性搜索客户端 RestHighLevelCleint 转换为 java 不确定我尝试了多次,但 java 代码无法按照以下查询进行翻译。

{
  "aggs": {
    "recommendations": {
      "nested": {
        "path": "events.recommendationData"
      },
      "aggs": {
        "exception": {
          "filter": {
            "terms": {
              "events.recommendationData.exceptionId": [
                "2"
              ]
            }
          },
          "aggs": {
            "exceptionIds": {
              "terms": {
                "field": "events.recommendationData.exceptionId.keyword",
                "size": 10
              },
              "aggs": {
                "recommendations": {
                  "nested": {
                    "path": "events.recommendationData.recommendations"
                  },
                  "aggs": {
                    "recommendationType": {
                      "terms": {
                        "field": "events.recommendationData.recommendations.recommendationType",
                        "size": 10
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

将以下代码与 RestHighLevelClient 一起使用

AggregationBuilder recommendations =
                AggregationBuilders.nested("recommendations", "events.recommendationData");

        AggregationBuilder exception = AggregationBuilders
                    .filter("exception", QueryBuilders.termsQuery("events.recommendationData.exceptionId", "2"));

        AggregationBuilder exceptionIds = AggregationBuilders.terms("exceptionIds")
                .field("events.recommendationData.exceptionId.keyword").size(10);

        AggregationBuilder recommendations2 =
                AggregationBuilders.nested("recommendations", "events.recommendationData.recommendations");

        AggregationBuilder recommendationType = AggregationBuilders.terms("recommendationType")
                .field("events.recommendationData.recommendations.recommendationType").size(10);

        AggregationBuilder build = 
                recommendations
                        .subAggregation(exception)
                        .subAggregation(exceptionIds)
                        .subAggregation(recommendations2)
                        .subAggregation(recommendationType);

它正在生成我在下面发布的错误查询,该查询不起作用。

{
  "aggregations": {
    "recommendations": {
      "nested": {
        "path": "events.recommendationData"
      },
      "aggregations": {
        "exception": {
          "filter": {
            "terms": {
              "events.recommendationData.exceptionId": [
                "1",
                "2"
              ],
              "boost": 1
            }
          }
        },
        "exceptionIds": {
          "terms": {
            "field": "events.recommendationData.exceptionId.keyword",
            "size": 10,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        },
        "recommendations": {
          "nested": {
            "path": "events.recommendationData.recommendations"
          }
        },
        "recommendationType": {
          "terms": {
            "field": "events.recommendationData.recommendations.recommendationType",
            "size": 10,
            "min_doc_count": 1,
            "shard_min_doc_count": 0,
            "show_term_doc_count_error": false,
            "order": [
              {
                "_count": "desc"
              },
              {
                "_key": "asc"
              }
            ]
          }
        }
      }
    }
  }
}

预期:每个聚合都是前一个聚合的子聚合。

因此,如果您看到预期的查询,recommendationTyperecommendations2 的子聚合。这些一起是 exceptionIds 等的子聚合。因此这里只需要更改一行,而不是

 AggregationBuilder build = 
                recommendations
                        .subAggregation(exception)
                        .subAggregation(exceptionIds)
                        .subAggregation(recommendations2)
                        .subAggregation(recommendationType);

使用这个,

recommendations.subAggregation(
        exception.subAggregation(
            exceptionIds.subAggregation(
                recommendations2.subAggregation(recommendationType)
            )
        )
    );