This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

Multi-terms aggregations

Similar to the terms bucket aggregation, you can also search for multiple terms using the multi_terms aggregation. Multi-terms aggregations are useful when you need to sort by document count, or when you need to sort by a metric aggregation on a composite key and get the top n results. For example, you could search for a specific number of documents (e.g., 1000) and the number of servers per location that show CPU usage greater than 90%. The top number of results would be returned for this multi-term query.

The multi_terms aggregation does consume more memory than a terms aggregation, so its performance might be slower.

Multi-terms aggregation parameters

ParameterDescription
multi_termsIndicates a multi-terms aggregation that gathers buckets of documents together based on criteria specified by multiple terms.
sizeSpecifies the number of buckets to return. Default is 10.
orderIndicates the order to sort the buckets. By default, buckets are ordered according to document count per bucket. If the buckets contain the same document count, then order can be explicitly set to the term value instead of document count. (e.g., set order to “max-cpu”).
doc_countSpecifies the number of documents to be returned in each bucket. By default, the top 10 terms are returned.

Example request

  1. GET sample-index100/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "hot": {
  6. "multi_terms": {
  7. "terms": [{
  8. "field": "region"
  9. },{
  10. "field": "host"
  11. }],
  12. "order": {"max-cpu": "desc"}
  13. },
  14. "aggs": {
  15. "max-cpu": { "max": { "field": "cpu" } }
  16. }
  17. }
  18. }
  19. }

copy

Example response

  1. {
  2. "took": 118,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 8,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": []
  17. },
  18. "aggregations": {
  19. "multi-terms": {
  20. "doc_count_error_upper_bound": 0,
  21. "sum_other_doc_count": 0,
  22. "buckets": [
  23. {
  24. "key": [
  25. "dub",
  26. "h1"
  27. ],
  28. "key_as_string": "dub|h1",
  29. "doc_count": 2,
  30. "max-cpu": {
  31. "value": 90.0
  32. }
  33. },
  34. {
  35. "key": [
  36. "dub",
  37. "h2"
  38. ],
  39. "key_as_string": "dub|h2",
  40. "doc_count": 2,
  41. "max-cpu": {
  42. "value": 70.0
  43. }
  44. },
  45. {
  46. "key": [
  47. "iad",
  48. "h2"
  49. ],
  50. "key_as_string": "iad|h2",
  51. "doc_count": 2,
  52. "max-cpu": {
  53. "value": 50.0
  54. }
  55. },
  56. {
  57. "key": [
  58. "iad",
  59. "h1"
  60. ],
  61. "key_as_string": "iad|h1",
  62. "doc_count": 2,
  63. "max-cpu": {
  64. "value": 15.0
  65. }
  66. }
  67. ]
  68. }
  69. }
  70. }