7

I'm looking for a way to save a dotted version as string (e.g "1.2.23") in elastic and then use range query on this field. e.g

{
 "query": {
   "range": {
    "version": {"gte": "1.2.3", "lt": "1.3"}
   }
  }
}

I have only 3 components (major, minor, build). I need to be able to determine that

  • 1.20.3 > 1.2.3
  • 1.02.4 > 1.2.3
  • 1.3 > 1.2.3

I thought about the following approaches:

  1. Pad with zeros (e.g "1.2.3" -> "000001.000002.000003"). This assumes I know the max length of each component
  2. Split into 3 different integer fields (i.e "major", "minor", "build"). Writing queries for this seems to be a pain, but I'd be happy to get suggestions for this.
  3. Perhaps some sort of a custom analyser? I saw this: Elasticsearch analysis plugin for natural sort which might be a good start.

Any other ideas or recommendations?

Eldad
  • 1,067
  • 16
  • 36

2 Answers2

4

If you have some latitude in your indexing code to massage those semantic versions into something else, I would suggest to transform each version into a unique integer and then it's very easy to compare those numbers with a single range query.

The algorithm is simple:

  1. You split the version on dots: 1.2.34 => 1, 2, 34
  2. You multiply the major version by 1000000: 1 => 1000000
  3. You multiply the minor version by 1000: 2 => 2000
  4. You sum all three numbers up: 1000000 + 2000 + 34 => 1002034
  5. You store that resulting number into your ES documents and use it to compare versions in range queries
Val
  • 207,596
  • 13
  • 358
  • 360
  • That was my initial approach, but I'm afraid I don't know the constraints on the components. To be specific, I'm talking about iOS version numbers, and as far as I could tell, each component has a max size of MAX_INTEGER. So this won't work: 1.2000000000.1 > 2.1.0. I can't multiple by a bigger number as the maximum number in ES is a long of size 2^64-1. I guess I can't multiple this into a string.... – Eldad Jun 12 '16 at 09:52
0

Elasticsearch has added support for version comparison and sort starting from version 7.10. https://www.elastic.co/guide/en/elasticsearch/reference/current/version.html

SSG
  • 1,265
  • 2
  • 17
  • 29