# Best way to find the length of array in elasticsearch

**URL:** <https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645>\
**Category:** Elasticsearch\
**Created:** [February 20, 2018, 1:10pm UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645 "2018-02-20T13:10:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_khanduri](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Amit\_khanduri](https://discuss.elastic.co/u/Amit_khanduri)\
**Post date:** [February 20, 2018, 1:10pm UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/1 "2018-02-20T13:10:56Z")

</div>

```
> "asst_re": [
> {
> "rep_type": 345,
> "repo_id": 7144220
> }
> ,
> {
> "rep_type": 545,
> "repo_id": 7084555
> }]

```

I have to filter the "asst\_re" array on the basis of length of the array.  
What is the best and optimise way to do and achieve this thing in elasticsearch 6.2 ?  
Thanks in Advance !!

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [February 20, 2018, 1:52pm UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/2 "2018-02-20T13:52:32Z")

</div>

best way: is to have the array length in another field at index-time  
other way: do a script query (search for this has been done many times)

---

<div class="post-metadata">

**Author:** ![Amit\_khanduri](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Amit\_khanduri](https://discuss.elastic.co/u/Amit_khanduri)\
**Post date:** [February 20, 2018, 8:28pm UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/3 "2018-02-20T20:28:59Z")

</div>

@ddorian43 thanks for the answer !!!👍  
I have one more query, what is the best way to count the number of words in a field ?  
`Ex - "name":"John doe bishop"` , So the name field have 3 words, what should be the query for this type of filtering ?

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [February 20, 2018, 9:06pm UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/4 "2018-02-20T21:06:00Z")

</div>

the same as with array-length

---

<div class="post-metadata">

**Author:** ![Amit\_khanduri](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Amit\_khanduri](https://discuss.elastic.co/u/Amit_khanduri)\
**Post date:** [February 21, 2018, 6:00am UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/5 "2018-02-21T06:00:13Z")

</div>

Thanks @ddorian43 !!!  
What is the query for that using script ? Can you help me out for this ?😀

---

<div class="post-metadata">

**Author:** ![ddorian43](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ddorian43/32/36093_2.png) [@ddorian43](https://discuss.elastic.co/u/ddorian43)\
**Post date:** [February 21, 2018, 7:43am UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/6 "2018-02-21T07:43:14Z")

</div>

Sorry you gotta search for it. But that is SLOW!  
Better to always store as separate field `"array_length": 5`

---

<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:** [February 21, 2018, 8:06am UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/7 "2018-02-21T08:06:55Z")

</div>

Agreed with @ddorian43

Specifically because you asked for:

> What is the best and optimise way

---

<div class="post-metadata">

**Author:** ![Amit\_khanduri](https://avatars.discourse-cdn.com/v4/letter/a/a3d4f5/32.png) [@Amit\_khanduri](https://discuss.elastic.co/u/Amit_khanduri)\
**Post date:** [February 21, 2018, 9:25am UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/8 "2018-02-21T09:25:01Z")

</div>

@ddorian43 thanks for the justification !!👍  
@dadoonet Yeah correct 😀

---

<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:** [March 21, 2018, 9:25am UTC](https://discuss.elastic.co/t/best-way-to-find-the-length-of-array-in-elasticsearch/120645/9 "2018-03-21T09:25:14Z")

</div>

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