# Aggregate every field

**URL:** <https://discuss.elastic.co/t/aggregate-every-field/258467>\
**Category:** Elasticsearch\
**Created:** [December 12, 2020, 6:14am UTC](https://discuss.elastic.co/t/aggregate-every-field/258467 "2020-12-12T06:14:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![5minutes](https://avatars.discourse-cdn.com/v4/letter/5/848f3c/32.png) [@5minutes](https://discuss.elastic.co/u/5minutes)\
**Post date:** [December 12, 2020, 6:14am UTC](https://discuss.elastic.co/t/aggregate-every-field/258467/1 "2020-12-12T06:14:39Z")

</div>

hi,

lets consider we have 2 records:

```auto
    {
      "@timestamp": "2020-12-11T11:13:00",
      "time": {
        "a": 200,
        "b": 2
      }
    }

    {
      "@timestamp": "2020-12-11T12:13:00",
      "time": {
        "a": 100,
        "b": 10
      }
    }

```

I would like to get average value for "a" and "b". Following works fine:

```auto
    {
      "size": 0, 
      "aggs": {
        "foo": {
          "date_histogram": {
            "field": "@timestamp",
            "calendar_interval": "1d"
          },
          "aggs": {
            "avg_a": {
              "avg": {
                "field": "time.a"
              }
            },
            "avg_b": {
              "avg": {
                "field": "time.b"
              }
            }
          }
        }
      }
    }

```

But I dont know what fields wil available, or maybe I will have 100 of them and I just dont want to write large query. I am looking for query where I can specify that I am interested in any field from "time" e.g:

```auto
    {
          "size": 0, 
          "aggs": {
            "foo": {
              "date_histogram": {
                "field": "@timestamp",
                "calendar_interval": "1d"
              },
              "aggs": {
                "all_fields": {
                  "avg": {
                    "field": "time.*"
                  }
                }
              }
            }
          }
        }

```

Or maybe I could use different approach:

```auto
{
  "@timestamp": "2020-12-11T11:13:00",
  "time": [
    {
    "name": "a",
    "time": 200
  },
    {
    "name": "b",
    "time": 20
  }
  ]
}
{
  "@timestamp": "2020-12-11T12:13:00",
  "time": [
    {
    "name": "a",
    "time": 100
  },
    {
    "name": "b",
    "time": 10
  }
  ]
}

```

I could now group by terms

```auto
{
  "size": 0, 
  "aggs": {

    "foo": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "1d"
      },
      "aggs": {
          "avg_time": {
              "terms": {
                "field": "time.name.keyword"
              }
        }
      }
    }
    
  }
}

```

but how to get average value from single bucket?

thanks

---

<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:** [January 9, 2021, 6:15am UTC](https://discuss.elastic.co/t/aggregate-every-field/258467/2 "2021-01-09T06:15:00Z")

</div>

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