ElasticSearch: compare dotted version strings -
i'm looking way save dotted version string (e.g "1.2.23") in elastic , use range query on field. e.g
{ "query": { "range": { "version": {"gte": "1.2.3", "lt": "1.3"} } } }
i have 3 components (major, minor, build). need able determine
- 1.20.3 > 1.2.3
- 1.02.4 > 1.2.3
- 1.3 > 1.2.3
i thought following approaches:
- pad zeros (e.g "1.2.3" -> "000001.000002.000003"). assumes know max length of each component
- split 3 different integer fields (i.e "major", "minor", "build"). writing queries seems pain, i'd happy suggestions this.
- perhaps sort of custom analyser? saw this: elasticsearch analysis plugin natural sort might start.
any other ideas or recommendations?
if have latitude in indexing code massage semantic versions else, suggest transform each version unique integer , it's easy compare numbers single range
query.
the algorithm simple:
- you split version on dots:
1.2.34
=>1
,2
,34
- you multiply major version 1000000:
1
=>1000000
- you multiply minor version 1000:
2
=>2000
- you sum 3 numbers up:
1000000 + 2000 + 34
=>1002034
- you store resulting number es documents , use compare versions in
range
queries
Comments
Post a Comment