# Sorting and querying semantic version strings

**URL:** <https://discuss.elastic.co/t/sorting-and-querying-semantic-version-strings/20727>\
**Category:** Elasticsearch\
**Created:** [November 13, 2014, 3:52pm UTC](https://discuss.elastic.co/t/sorting-and-querying-semantic-version-strings/20727 "2014-11-13T15:52:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff\_Potts](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jeff_potts/32/1047_2.png) [@Jeff\_Potts](https://discuss.elastic.co/u/Jeff_Potts)\
**Post date:** [November 13, 2014, 3:52pm UTC](https://discuss.elastic.co/t/sorting-and-querying-semantic-version-strings/20727/1 "2014-11-13T15:52:48Z")

</div>

I have a property called "version" that will have semantic version strings  
like "1.1.0", "10.0.1", and "2.0.0". There will always be three integers  
separated by periods and no other characters. I need to be able to sort my  
docs by version and do bool comparisons. For example, using the values  
above I need the ascending order to be "1.1.0", "2.0.0", "10.0.1".

My approach so far, documented in this gist  
[https://gist.github.com/jpotts/375e3c8894f93648995d](https://gist.github.com/jpotts/375e3c8894f93648995d), is to use a groovy  
script to normalize the string at index time using a transform. The script  
pads each element in the string with spaces to a length of eight and stores  
the result in a field (version.sortable). So "1.1.0" would become "  
1 1 0" while "10.0.1" would become " 10 0 1".

As I put in the gist, this works, but I suspect there is a better way to  
handle this. One drawback of my current approach is that queries have to  
use the normalized term. I'd much rather use the natural format and have  
the normalization be invisible to the front-end.

I suspect I could handle this with a custom analyzer but I'm not exactly  
sure where to start with that.

Any suggestions appreciated.

Jeff

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [November 13, 2014, 7:52pm UTC](https://discuss.elastic.co/t/sorting-and-querying-semantic-version-strings/20727/2 "2014-11-13T19:52:47Z")

</div>

You can use my plugin

> **[GitHub - jprante/elasticsearch-analysis-naturalsort: Natural sort plugin for...](https://github.com/jprante/elasticsearch-analysis-naturalsort)**
>
> Natural sort plugin for Elasticsearch. Contribute to jprante/elasticsearch-analysis-naturalsort development by creating an account on GitHub.

if you want to sort on such fields

[https://github.com/jprante/elasticsearch-analysis-naturalsort/blob/master/src/test/java/org/xbib/elasticsearch/naturalsort/NaturalSortKeyTests.java#L164](https://github.com/jprante/elasticsearch-analysis-naturalsort/blob/master/src/test/java/org/xbib/elasticsearch/naturalsort/NaturalSortKeyTests.java#L164)

I haven't tested search yet though.

Jörg

On Thu, Nov 13, 2014 at 4:52 PM, Jeff Potts [jeffpotts01@gmail.com](mailto:jeffpotts01@gmail.com) wrote:

> I have a property called "version" that will have semantic version strings  
> like "1.1.0", "10.0.1", and "2.0.0". There will always be three integers  
> separated by periods and no other characters. I need to be able to sort my  
> docs by version and do bool comparisons. For example, using the values  
> above I need the ascending order to be "1.1.0", "2.0.0", "10.0.1".
> 
> My approach so far, documented in this gist  
> [https://gist.github.com/jpotts/375e3c8894f93648995d](https://gist.github.com/jpotts/375e3c8894f93648995d), is to use a groovy  
> script to normalize the string at index time using a transform. The script  
> pads each element in the string with spaces to a length of eight and stores  
> the result in a field (version.sortable). So "1.1.0" would become "  
> 1 1 0" while "10.0.1" would become " 10 0 1".
> 
> As I put in the gist, this works, but I suspect there is a better way to  
> handle this. One drawback of my current approach is that queries have to  
> use the normalized term. I'd much rather use the natural format and have  
> the normalization be invisible to the front-end.
> 
> I suspect I could handle this with a custom analyzer but I'm not exactly  
> sure where to start with that.
> 
> Any suggestions appreciated.
> 
> Jeff
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/f708ffff-e46c-4447-bd26-808fbbf20211%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoE1FA2FoCoU6KYCanaymw\_k2dYi7ExyyjLbPXWP6T%2BnWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoE1FA2FoCoU6KYCanaymw_k2dYi7ExyyjLbPXWP6T%2BnWw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 12:50am UTC](https://discuss.elastic.co/t/sorting-and-querying-semantic-version-strings/20727/3 "2017-07-06T00:50:06Z")

</div>


