Need help on aggregation and sub aggregation with ES 8.7 rest api

Hello,

I am working on migrating ES 6.8 java code to ES 8.7 rest api java. While working on aggregation, I am able to construct aggregation layer but not as per the required format.

----- **expected result** -----
 
"aggregations": {
		"unique_emp": {
			"cardinality": {
				"field": "email.keyword"
			}
		},
		"emplyee": {
			"terms": {
				"field": "email.keyword",
				"size": 10000,
				"min_doc_count": 1,
				"shard_min_doc_count": 0,
				"show_term_doc_count_error": false,
				"order": [
					{
						"totalexp": "desc"
					},
					{
						"_key": "asc"
					}
				]
			},
			"aggregations": {
				"uNum": {
					"cardinality": {
						"field": "unum.keyword"
					}
				},
				"totalexp": {
					"sum": {
						"field": "expense"
					}
				},
				"distinct": {
					"top_hits": {
						"from": 0,
						"size": 1,
						"version": false,
						"seq_no_primary_term": false,
						"explain": false,
						"_source": {
							"includes": [
								"all_uid",
								"name",
								"company"
							],
							"excludes": []
						}
					}
				},
				"expsort": {
					"bucket_sort": {
						"sort": [],
						"from": 0,
						"size": 10,
						"gap_policy": "SKIP"
					}
				}
			}
		}
	}

Above aggregation layer was generated using below ES 6.8 java.

termAggregation = AggregationBuilders.terms("emplyee").field("email.keyword").size(10000);
			cardinalityAggregationBuilder = AggregationBuilders.cardinality("unique_emp").field("email.keyword");
			termAggregation1 = AggregationBuilders.topHits("distinct")
					.size(1)
					.fetchSource(includeFields, excludeFields);
			
			termAggregation.subAggregation(AggregationBuilders.cardinality("uNum").field("unum.keyword"));
			termAggregation.subAggregation(AggregationBuilders.sum("totalexp").field("expense"));
			termAggregation.subAggregation(termAggregation1);
			termAggregation.subAggregation(new BucketSortPipelineAggregationBuilder("expsort", null).from(from-1).size(size));
			searchSourceBuilder.aggregation(cardinalityAggregationBuilder);

Now the same, I tried to achieve with ES 8.7 rest api.

Map<String, Aggregation> aggregates = new HashMap<>();
List<NamedValue<SortOrder>> orderList = new ArrayList<>();
orderList.add(NamedValue.of("totalexp", SortOrder.Desc));
orderList.add(NamedValue.of("_key", SortOrder.Asc));

Aggregation cardi_email_aggregt = new Aggregation.Builder()
	.cardinality(new CardinalityAggregation.Builder().field("email.keyword").build())
	.build();
Aggregation cardi_uNum_aggregt = new Aggregation.Builder()
		.cardinality(new CardinalityAggregation.Builder().field("uNum").build())
		.build();
Aggregation sum_tot_exp_aggregt = new Aggregation.Builder()
		.sum(new SumAggregation.Builder().field("expense").build())
		.build();
Aggregation topHit_distinct = new Aggregation.Builder()
		.topHits(new TopHitsAggregation.Builder().field("email.keyword").from(0).size(1).build())
		.build();
Aggregation termAggregation = AggregationBuilders.terms(ta -> ta.field("email.keyword").size(10000).order(attendeeOrderlist));
Aggregation aggregations = new Aggregation.Builder()
		.terms(new TermsAggregation.Builder().field("email.keyword")
				.order(orderList)
				.size(10000).build())
		.aggregations(new HashMap<>() {{
			put("uNumb", cardi_uNum_aggregt);
			put("totalexp", sum_tot_exp_aggregt);
			put("distinct", topHit_distinct);
		}}).build();	
aggregates.put("agg_director", aggregations);

But, above code is not working as expected so getting the below aggregate layer.

{agg_director=  Aggregation: {
	"aggregations": {
		"distinct": {
			"top_hits": {
				"field": "email.keyword",
				"from": 0,
				"size": 1
			}
		},
		"totalexp": {
			"sum": {
				"field": "expense"
			}
		},
		"uNumb": {
			"cardinality": {
				"field": "uNum"
			}
		}
	},
	"terms": {
		"field": "email.keyword",
		"order": [
			{
				"totalexp": "desc"
			},
			{
				"_key": "asc"
			}
		],
		"size": 10000
	}
}}

Above aggregation query is not correct as terms -> email.keyword is outside aggregation. Actually, the subaggregations such as "distinct", "totalexp", and "uNumb" should be inside the "terms -> field : email.keyword.

Could you please help in writing above ES 8.7 rest api java code for achieving expected result i.e. from the above first preformatted text?

Thanks in advance
Chetan

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.