tophits-aggregation.asciidoc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. [[java-aggs-metrics-tophits]]
  2. ==== Top Hits Aggregation
  3. Here is how you can use
  4. {ref}/search-aggregations-metrics-top-hits-aggregation.html[Top Hits Aggregation]
  5. with Java API.
  6. ===== Prepare aggregation request
  7. Here is an example on how to create the aggregation request:
  8. [source,java]
  9. --------------------------------------------------
  10. AggregationBuilder aggregation =
  11. AggregationBuilders
  12. .terms("agg").field("gender")
  13. .subAggregation(
  14. AggregationBuilders.topHits("top")
  15. );
  16. --------------------------------------------------
  17. You can use most of the options available for standard search such as `from`, `size`, `sort`, `highlight`, `explain`...
  18. [source,java]
  19. --------------------------------------------------
  20. AggregationBuilder aggregation =
  21. AggregationBuilders
  22. .terms("agg").field("gender")
  23. .subAggregation(
  24. AggregationBuilders.topHits("top")
  25. .setExplain(true)
  26. .setSize(1)
  27. .setFrom(10)
  28. );
  29. --------------------------------------------------
  30. ===== Use aggregation response
  31. Import Aggregation definition classes:
  32. [source,java]
  33. --------------------------------------------------
  34. import org.elasticsearch.search.aggregations.bucket.terms.Terms;
  35. import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
  36. --------------------------------------------------
  37. [source,java]
  38. --------------------------------------------------
  39. // sr is here your SearchResponse object
  40. Terms agg = sr.getAggregations().get("agg");
  41. // For each entry
  42. for (Terms.Bucket entry : agg.getBuckets()) {
  43. String key = entry.getKey(); // bucket key
  44. long docCount = entry.getDocCount(); // Doc count
  45. logger.info("key [{}], doc_count [{}]", key, docCount);
  46. // We ask for top_hits for each bucket
  47. TopHits topHits = entry.getAggregations().get("top");
  48. for (SearchHit hit : topHits.getHits().getHits()) {
  49. logger.info(" -> id [{}], _source [{}]", hit.getId(), hit.getSourceAsString());
  50. }
  51. }
  52. --------------------------------------------------
  53. This will basically produce for the first example:
  54. [source,text]
  55. --------------------------------------------------
  56. key [male], doc_count [5107]
  57. -> id [AUnzSZze9k7PKXtq04x2], _source [{"gender":"male",...}]
  58. -> id [AUnzSZzj9k7PKXtq04x4], _source [{"gender":"male",...}]
  59. -> id [AUnzSZzl9k7PKXtq04x5], _source [{"gender":"male",...}]
  60. key [female], doc_count [4893]
  61. -> id [AUnzSZzM9k7PKXtq04xy], _source [{"gender":"female",...}]
  62. -> id [AUnzSZzp9k7PKXtq04x8], _source [{"gender":"female",...}]
  63. -> id [AUnzSZ0W9k7PKXtq04yS], _source [{"gender":"female",...}]
  64. --------------------------------------------------