Aggregate "array" length's across documents

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

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

Query

{
  "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).

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

Thanks for any help :slight_smile:

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

This 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.

{
  "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:

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

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