ndtreviv
(Nathan Trevivian)
February 19, 2024, 9:18pm
1
I have a document mapping that looks like this:
"domain": {
"type": "nested",
"properties": {
"name": {
"type": "keyword",
"store": true
},
"tld": {
"type": "keyword",
"store": true
},
"sld": {
"type": "keyword",
"store": true
},
"subdomains": {
"type": "keyword",
"store": true
}
}
},
I want to get a terms aggregation of domains.sld
, but I can't figure out how to do it using the Java SDK.
elasticsearchClient.search(s -> s.index(INDEX).size(0)
.aggregations("nesting", n -> n.nested(
nested -> nested.path("domain")... then what? The library offers nothing
))
, Void.class);
Can someone please give me an example of how to do this?
Many thanks for any help given
RabBit_BR
(andre.coelho)
February 20, 2024, 12:11pm
2
Hi @ndtreviv
This example using builder style but its simple transform to other.
Aggregation sub_aggregation = new Aggregation.Builder()
.terms(new TermsAggregation.Builder().field("domain.sld").size(5).build())
.build();
Aggregation nestedAggregation = new Aggregation.Builder()
.nested(new NestedAggregation.Builder().path("domain").build())
.aggregations(new HashMap<>() {{
put("avg_renevue", sub_aggregation);
}}).build();
SearchRequest searchRequest = new SearchRequest.Builder()
.index("idx")
.size(0)
.aggregations("nesting", nestedAggregation)
.build();
ndtreviv
(Nathan Trevivian)
February 20, 2024, 12:36pm
3
Thanks for this! I'll give it a go!
ndtreviv
(Nathan Trevivian)
February 20, 2024, 12:44pm
4
How do I get results out of it?
I'm expecting to get a list of buckets, being keyed by domain.sld
values and their doc_counts
.
Currently trying:
results.aggregations().get("nested").nested(). then what?
I really wish the docs/examples were better...
ndtreviv
(Nathan Trevivian)
February 20, 2024, 2:11pm
5
Trying to find a solution online. I feel like I'm the only one in the world using the newer Java client for elasticsearch.
ndtreviv
(Nathan Trevivian)
February 20, 2024, 2:27pm
6
This was an issue with version 7.16.2 of the client library.
Once upgraded to 7.17.18 then NestedAggregate
finally gets an aggregations
method whereby you can then access the sub aggregation, in this use case like so:
StringTermsAggregate termsAggregate = results.aggregations()
.get("nested")
.nested()
.aggregations()
.get("slds")
.sterms();
ndtreviv
(Nathan Trevivian)
February 20, 2024, 2:28pm
7
So calling aggregations and passing a static map didn't seem to work, but this did:
Aggregation subAggregation = new Aggregation.Builder()
.terms(new TermsAggregation.Builder().field("domain.subdomains").size(100).exclude(e -> e.terms(List.of(subdomain))).build())
.build();
Aggregation nestedAggregation = new Aggregation.Builder()
.nested(new NestedAggregation.Builder().path("domain").build())
.aggregations("subdomains", subAggregation)
.build();
Thanks for your help!
system
(system)
Closed
March 19, 2024, 2:28pm
8
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.