# Sort by length of an array

**URL:** <https://discuss.elastic.co/t/sort-by-length-of-an-array/225742>\
**Category:** Elasticsearch\
**Created:** [March 30, 2020, 8:01pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742 "2020-03-30T20:01:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![RajaG](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@RajaG](https://discuss.elastic.co/u/RajaG)\
**Post date:** [March 30, 2020, 8:01pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/1 "2020-03-30T20:01:34Z")

</div>

```
PUT wc
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "text",
        "fields": {
          "word_count": {
            "type": "token_count",
            "analyzer": "standard"
          }
        }
      }
    }
  }
}

PUT wc/_doc/1
{
  "comments" : ["hi", "hello", "how r u"]
}

PUT wc/_doc/2
{
  "comments" : ["hi", "hello", "how r u" , "fine"]
}

GET wc/_search
{
  "sort": [
    {
      "comments.word_count": {
        "order": "desc"
      }
    }
  ]
}

```

this isn't giving correct results. how should i troubleshoot this?

---

<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:** [April 3, 2020, 4:14pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/2 "2020-04-03T16:14:32Z")

</div>

I was looking at this today to try to find a way to bypass this problem.  
I tried this but this is the same problem:

```auto
DELETE wc
PUT wc
{
  "mappings": {
    "properties": {
      "wc": {
        "type": "token_count",
        "analyzer": "standard"
      },
      "comments": {
        "type": "text",
        "copy_to": "wc"
      }
    }
  }
}
PUT wc/_doc/1
{
  "comments" : ["hi", "hello", "how r u"]
}
PUT wc/_doc/2
{
  "comments" : ["hi", "hello", "how r u" , "fine"]
}
GET wc/_search
{
  "sort": [
    {
      "wc": {
        "order": "desc"
      }
    }
  ]
}

```

I found this old issue which did not get a lot of traction:

> <https://github.com/elastic/elasticsearch/issues/5417>
>
> When indexing arrays of strings, the token\_count field type should add up all of the token counts, rather than storing a...

I wonder if this is doable with an ingest pipeline which would build a text field containing all the text from the array... I was expecting `copy_to` to do that but apparently it does not...

@jpountz I wonder if this is a bug or a feature. IMHO it looks like a bug. Or may be we should add an option for arrays? As here we don't want the number of tokens in the biggest string of the array but the count of all the tokens within the array.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [April 7, 2020, 4:38pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/3 "2020-04-07T16:38:52Z")

</div>

It's a feature to me. Out of curiosity, what is the high-level use-case, I wonder whether this is the right tool for the job.

---

<div class="post-metadata">

**Author:** ![RajaG](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@RajaG](https://discuss.elastic.co/u/RajaG)\
**Post date:** [April 7, 2020, 5:19pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/4 "2020-04-07T17:19:54Z")

</div>

We are developing an application called data-catalog, which holds all information about the data that our department is dealing with, something like supply chain data.  
In this application, we are allowing users to like or dislike(social recommendations) content of a document.  
So, while a user searching for a document, we would like to sort the list of matched documents in number of likes in descending order, followed by number of dislikes in ascending order.

We are storing who liked or disliked in the array of strings(userid). Hence, we arrived at the problem of sorting the result set by length of an array.

Hope this clarifies the requirement.

---

<div class="post-metadata">

**Author:** ![RajaG](https://avatars.discourse-cdn.com/v4/letter/r/b9bd4f/32.png) [@RajaG](https://discuss.elastic.co/u/RajaG)\
**Post date:** [April 19, 2020, 5:04pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/5 "2020-04-19T17:04:37Z")

</div>

Has anybody got a chance to look into this??

---

<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 17, 2020, 5:04pm UTC](https://discuss.elastic.co/t/sort-by-length-of-an-array/225742/6 "2020-05-17T17:04:41Z")

</div>

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