# Aggregate to collect data in array

**URL:** https://discuss.elastic.co/t/aggregate-to-collect-data-in-array/351290
**Category:** Elasticsearch
**Created:** [January 17, 2024, 2:54pm UTC](https://discuss.elastic.co/t/aggregate-to-collect-data-in-array/351290 "2024-01-17T14:54:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![masoud\_darvishi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/masoud_darvishi/32/114483_2.png) [@masoud\_darvishi](https://discuss.elastic.co/u/masoud_darvishi)
#### Post date: [January 17, 2024, 2:54pm UTC](https://discuss.elastic.co/t/aggregate-to-collect-data-in-array/351290/1 "2024-01-17T14:54:25Z")

</div>

hello  
this is my data:

```auto
[
   {
      type: "a", // value is "a" or "b"
      number: 15 // number between 1 to 100
  },
  {
      type: "b",
      number: 56
  },
  {
      type: "a",
      number: 40
  },
  {
      type: "b",
      number: 2
  }
]

```

is there aggregation that is can group by "type" field and collect "number" values in array? i want aggregation result be something like this :

```auto
result: [
    {
       key: "a",
       doc_count: 2
       numbers: {
            value: [15,40]
       }
     },
     {
        key: "b",
        doc_count: 2
        numbers: {
             value: [56,2]
        }
     },
]

```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 17, 2024, 5:12pm UTC](https://discuss.elastic.co/t/aggregate-to-collect-data-in-array/351290/2 "2024-01-17T17:12:09Z")

</div>

If you want the unique values of the numbers, you can use this:

```auto
DELETE test
POST test/_doc
{ "type": "a", "number": 15 }
POST test/_doc
{ "type": "a", "number": 15 }
POST test/_doc
{ "type": "b", "number": 56 }
POST test/_doc
{ "type": "a", "number": 40 }
POST test/_doc
{ "type": "b", "number": 2 }
GET test/_search
{
  "size": 0,
  "aggs": {
    "by_type": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "by_number": {
          "terms": {
            "field": "number"
          }
        }
      }
    }
  }
}

```

This gives:

```auto
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 5,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "by_type": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "a",
          "doc_count": 3,
          "by_number": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 15,
                "doc_count": 2
              },
              {
                "key": 40,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "b",
          "doc_count": 2,
          "by_number": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
              {
                "key": 2,
                "doc_count": 1
              },
              {
                "key": 56,
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

```

But if you want to get all the values, even though some of them are already collected, you could do that instead:

```auto
GET test/_search?filter_path=aggregations.by_type.buckets.key,aggregations.by_type.buckets.all_numbers.hits.hits.fields.number
{
  "size": 0,
  "aggs": {
    "by_type": {
      "terms": {
        "field": "type.keyword"
      },
      "aggs": {
        "all_numbers": {
          "top_hits": {
            "size": 1000,
            "docvalue_fields": ["number"],
            "_source": false
          }
        }
      }
    }
  }
}

```

```auto
{
  "aggregations": {
    "by_type": {
      "buckets": [
        {
          "key": "a",
          "all_numbers": {
            "hits": {
              "hits": [
                {
                  "fields": {
                    "number": [
                      15
                    ]
                  }
                },
                {
                  "fields": {
                    "number": [
                      15
                    ]
                  }
                },
                {
                  "fields": {
                    "number": [
                      40
                    ]
                  }
                }
              ]
            }
          }
        },
        {
          "key": "b",
          "all_numbers": {
            "hits": {
              "hits": [
                {
                  "fields": {
                    "number": [
                      56
                    ]
                  }
                },
                {
                  "fields": {
                    "number": [
                      2
                    ]
                  }
                }
              ]
            }
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 14, 2024, 5:12pm UTC](https://discuss.elastic.co/t/aggregate-to-collect-data-in-array/351290/3 "2024-02-14T17:12:34Z")

</div>

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