48 lines
1.4 KiB
Plaintext
48 lines
1.4 KiB
Plaintext
|
[[java-aggs-bucket-significantterms]]
|
||
|
==== Significant Terms Aggregation
|
||
|
|
||
|
Here is how you can use
|
||
|
{ref}/search-aggregations-bucket-significantterms-aggregation.html[Significant Terms Aggregation]
|
||
|
with Java API.
|
||
|
|
||
|
|
||
|
===== Prepare aggregation request
|
||
|
|
||
|
Here is an example on how to create the aggregation request:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
AggregationBuilder aggregation =
|
||
|
AggregationBuilders
|
||
|
.significantTerms("significant_countries")
|
||
|
.field("address.country");
|
||
|
|
||
|
// Let say you search for men only
|
||
|
SearchResponse sr = client.prepareSearch()
|
||
|
.setQuery(QueryBuilders.termQuery("gender", "male"))
|
||
|
.addAggregation(aggregation)
|
||
|
.get();
|
||
|
--------------------------------------------------
|
||
|
|
||
|
|
||
|
===== Use aggregation response
|
||
|
|
||
|
Import Aggregation definition classes:
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms;
|
||
|
--------------------------------------------------
|
||
|
|
||
|
[source,java]
|
||
|
--------------------------------------------------
|
||
|
// sr is here your SearchResponse object
|
||
|
SignificantTerms agg = sr.getAggregations().get("significant_countries");
|
||
|
|
||
|
// For each entry
|
||
|
for (SignificantTerms.Bucket entry : agg.getBuckets()) {
|
||
|
entry.getKey(); // Term
|
||
|
entry.getDocCount(); // Doc count
|
||
|
}
|
||
|
--------------------------------------------------
|