# Aggregate "array" length's across documents

**URL:** <https://discuss.elastic.co/t/aggregate-array-lengths-across-documents/254646>\
**Category:** Elasticsearch\
**Created:** [November 8, 2020, 12:30am UTC](https://discuss.elastic.co/t/aggregate-array-lengths-across-documents/254646 "2020-11-08T00:30:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Courtan](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@Courtan](https://discuss.elastic.co/u/Courtan)\
**Post date:** [November 8, 2020, 12:30am UTC](https://discuss.elastic.co/t/aggregate-array-lengths-across-documents/254646/1 "2020-11-08T00:30:26Z")

</div>

I am trying to count Array Elements across all documents and sum them up, but I keep getting this error: `"No field found for [offers] in mapping with types []"`

**Mappings**

```auto
{
  "mappings": {
    "properties": {
      "offers": {
        "properties": {
          "name": { "type": "text" },
          "id": { "type": "keyword" }
        }
      },
      "requirements": {
        "properties": {
          "name": { "type": "text" },
          "id": { "type": "keyword" }
        }
      }
    }
  }
}

```

**Query**

```auto
{
  "aggs": {
    "count_offers": {
      "sum": {
        "script" : {
          "lang": "painless",
          "source": "doc['offers'].value.length"
        }
      }
    },
    "count_requirements": {
      "sum": {
        "script" : {
          "lang": "painless",
          "source": "doc['requirements'].value.length"
        }
      }
    }
  }
}

```

In the end I want to be able to retrieve the total number of offers or requirements.

I suspect that painless cannot deal with the field "offers" as an array. But this confuses me, as the ES documentation states that there is no dedicated array type ([over here](https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html)).

I found some related topics here and on Stackoverflow, but I could not solve my problem with them.

Thanks for any help 🙂

---

<div class="post-metadata">

**Author:** ![Courtan](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@Courtan](https://discuss.elastic.co/u/Courtan)\
**Post date:** [November 8, 2020, 2:53pm UTC](https://discuss.elastic.co/t/aggregate-array-lengths-across-documents/254646/2 "2020-11-08T14:53:55Z")

</div>

Ok found a solution. Maybe this can help someone in the future:

[This](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html) part of the documentation showed me how to access the properties. Apparently the `_source` gets passed into the script via the `params` object. But I still don't understand why I could access some properties like this `doc['key']` and others not.

```auto
{
  "size": 0, 
  "aggs" : {
        "count_offers" : {
            "sum": {
              "script" : {
                "source": "params._source.offers.length"
              }
            }
        },
        "count_requirements" : {
            "sum": {
              "script" : {
                "source": "params._source.requirements.length"
              }
            }
        }
    }
}

```

This worked but was **painfully** slow (several hundred ms) and is probably not the intended way to do it. After a bit of digging I found a blazing fast solution [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-valuecount-aggregation.html):

```auto
{
  "size": 0,
  "aggs" : {
    "count_offers" : {
      "value_count" : {
        "field" : "offers.id"
      }
    },
    "count_requirements" : {
      "value_count" : {
        "field" : "requirements.id"
      }
    }
  }
}

```

---

<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:** [December 6, 2020, 2:53pm UTC](https://discuss.elastic.co/t/aggregate-array-lengths-across-documents/254646/3 "2020-12-06T14:53:57Z")

</div>

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