# Sorting on length of field in the result

**URL:** <https://discuss.elastic.co/t/sorting-on-length-of-field-in-the-result/10315>\
**Category:** Elasticsearch\
**Created:** [January 11, 2013, 10:23am UTC](https://discuss.elastic.co/t/sorting-on-length-of-field-in-the-result/10315 "2013-01-11T10:23:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea\_crotti](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@andrea\_crotti](https://discuss.elastic.co/u/andrea_crotti)\
**Post date:** [January 11, 2013, 10:23am UTC](https://discuss.elastic.co/t/sorting-on-length-of-field-in-the-result/10315/1 "2013-01-11T10:23:25Z")

</div>

We use elasticsearch with couchdb, with the couchdb-river installed.

Now everything works fine, but now I want to be able to sort the results  
we get from elasticsearch.

The first problem is that the date field is not a valid date, because  
the format is something like this from the database:  
2012-12-27T18:58:22Z

And there was a mapping to make it treat like a string. But now I would  
need to sort on it, so I should probably change the mapping.

But I have another question about that, should I create a new index  
with that mapping or use the PUT Mapping API? What's the difference  
between the two possibilities?

The second problem is that I want to sort on things that require some  
computation.

For example in the index I have the field:  
{'ideas': ['first', 'second', 'third']}

And I need to be able to sort on the length of that list.

How can I do it?  
I don't find a scripting way to do it, or a possible core type.

Is the only way to keep track of the number of ideas (and add the field)  
in couchdb maybe?

Thanks, Andrea

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 15, 2013, 12:49am UTC](https://discuss.elastic.co/t/sorting-on-length-of-field-in-the-result/10315/2 "2013-01-15T00:49:52Z")

</div>

There is no difference between creating an index with a mapping and putting  
mapping after you create an index.

You can use script to sort based on the number of ideas using Script Based  
Sorting [http://www.elasticsearch.org/guide/reference/api/search/sort.html](http://www.elasticsearch.org/guide/reference/api/search/sort.html).  
However, keeping the count in your application and indexing it as a  
separate field might be a much better idea from the performance  
perspective.

On Friday, January 11, 2013 5:23:25 AM UTC-5, andrea crotti wrote:

> We use elasticsearch with couchdb, with the couchdb-river installed.
> 
> Now everything works fine, but now I want to be able to sort the results  
> we get from elasticsearch.
> 
> The first problem is that the date field is not a valid date, because  
> the format is something like this from the database:  
> 2012-12-27T18:58:22Z
> 
> And there was a mapping to make it treat like a string. But now I would  
> need to sort on it, so I should probably change the mapping.
> 
> But I have another question about that, should I create a new index  
> with that mapping or use the PUT Mapping API? What's the difference  
> between the two possibilities?
> 
> The second problem is that I want to sort on things that require some  
> computation.
> 
> For example in the index I have the field:  
> {'ideas': ['first', 'second', 'third']}
> 
> And I need to be able to sort on the length of that list.
> 
> How can I do it?  
> I don't find a scripting way to do it, or a possible core type.
> 
> Is the only way to keep track of the number of ideas (and add the field)  
> in couchdb maybe?
> 
> Thanks, Andrea

--

---

<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:** [July 6, 2017, 2:56am UTC](https://discuss.elastic.co/t/sorting-on-length-of-field-in-the-result/10315/3 "2017-07-06T02:56:31Z")

</div>


