# How to get array size for each document

**URL:** <https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599>\
**Category:** Elasticsearch\
**Created:** [April 5, 2020, 10:42pm UTC](https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599 "2020-04-05T22:42:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntsh999](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ntsh999/32/51172_2.png) [@ntsh999](https://discuss.elastic.co/u/ntsh999)\
**Post date:** [April 5, 2020, 10:42pm UTC](https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599/1 "2020-04-05T22:42:07Z")

</div>

I want the size of an array of objects ('breaches' here) which is present in each document to be returned along with the fields present in the document once the query is executed. How can I do this? Below is my document structure and mapping. I tried with inline script but did not get success. I want name, age, email, contact, total number of breaches to be returned for each document.

```auto
{
	"age": 45,
	"email": "",
	"contact": 9899999999,
	"name": "xyx",
	"breaches": [{
		"_id": "5e833cae56cd2e0011ee8e4a",
		"citizen": "5e82e0f5fb4f240012fa9737",
		"breachType": "location_disabled",
		"timestamp": "2020-03-31T12:50:52.828Z",
		"division": null,
		"createdAt": "2020-03-31T12:50:54.371Z"
	}]
}

```

Mapping

```auto
{
	"properties": {
		"age": {
			"type": "long"
		},
		"email": {
			"type": "text",
			"fields": {
				"keyword": {
					"type": "keyword",
					"ignore_above": 256
				}
			}
		},
		"contact": {
			"type": "long"
		},
		"name": {
			"type": "text",
			"fields": {
				"keyword": {
					"type": "keyword",
					"ignore_above": 256
				}
			}
		},
		"breaches": {
			"type": "nested",
			"properties": {
				"_id": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"breachType": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"citizen": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				},
				"createdAt": {
					"type": "date"
				},
				"division": {
					"type": "text",
					"fields": {
						"keyword": {
							"type": "keyword",
							"ignore_above": 256
						}
					}
				}
			}
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![Luca\_Belluccini](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/luca_belluccini/32/33239_2.png) [@Luca\_Belluccini](https://discuss.elastic.co/u/Luca_Belluccini)\
**Post date:** [April 5, 2020, 11:08pm UTC](https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599/2 "2020-04-05T23:08:11Z")

</div>

Hello @ntsh999,

The "easiest" solution is using `scripted fields`, but it is gonna to be slow and consume resources, as you're accessing to the `_source`:

```auto
GET your_index_name/_search
{
  "script_fields": {
    "number_of_breaches": {
      "script": {
        "source": "params['_source'].breaches.length"
      }
    }
  },
  "_source": {
    "excludes": ["breaches"]
  }
}
# Response
{
  "took" : 672,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "mytest",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "contact" : 9899999999,
          "name" : "xyx",
          "age" : 45,
          "email" : ""
        },
        "fields" : {
          "number_of_breaches" : [
            1
          ]
        }
      }
    ]
  }
}

```

The best solution is usually to compute such fields at indexing time, for example using the Ingest Pipelines ([documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/ingest.html)):

```auto
PUT _ingest/pipeline/count_breaches
{
    "description": "Counts the breaches",
    "processors": [
      {
        "script": {
          "source": "ctx.number_of_breaches = ctx.containsKey('breaches') ? ctx.breaches.length : 0"
        }
      }
    ]
}

```

The pipeline can be used when indexing the document.

---

<div class="post-metadata">

**Author:** ![ntsh999](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ntsh999/32/51172_2.png) [@ntsh999](https://discuss.elastic.co/u/ntsh999)\
**Post date:** [April 6, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599/3 "2020-04-06T08:01:25Z")

</div>

> [@Luca\_Belluccini](#):
>
> { "script\_fields": { "number\_of\_breaches": { "script": { "source": "params['\_source'].breaches.length" } } }, "\_source": { "excludes": ["breaches"] } }

HI @Luca_Belluccini,  
Thanks the solution provided by you works absolutely fine.

---

<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:** [May 4, 2020, 8:01am UTC](https://discuss.elastic.co/t/how-to-get-array-size-for-each-document/226599/4 "2020-05-04T08:01:27Z")

</div>

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