-
-
Save sursir/433b4e1aaebb0c00182820ed999aba11 to your computer and use it in GitHub Desktop.
Need a better approach to sorting semantic version strings in Elasticsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Delete the example index | |
curl -XDELETE "http://localhost:9200/sortable-version-test?pretty=true" | |
# Create a new example index | |
curl -X POST "http://localhost:9200/sortable-version-test?pretty=true" | |
# Set the mapping. Assumes version.groovy resides in $ES_HOME/config/scripts | |
curl -XPOST "http://localhost:9200/sortable-version-test/version/_mapping?pretty=true" -d' | |
{ | |
"version": { | |
"transform": { | |
"lang":"groovy", | |
"script":"version" | |
}, | |
"properties": { | |
"version": { | |
"type": "string", | |
"fields": { | |
"sortable": { | |
"type": "string", | |
"index": "not_analyzed" | |
} | |
} | |
} | |
} | |
} | |
}' | |
# Post some test docs | |
curl -X POST "http://localhost:9200/sortable-version-test/version/1?pretty=true" -d' | |
{ | |
"version": "10.10.1" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/2?pretty=true" -d' | |
{ | |
"version": "10.1.1" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/3?pretty=true" -d' | |
{ | |
"version": "2.11.0" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/4?pretty=true" -d' | |
{ | |
"version": "2.10.1" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/5?pretty=true" -d' | |
{ | |
"version": "2.1.1" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/6?pretty=true" -d' | |
{ | |
"version": "1.10.0" | |
}' | |
curl -X POST "http://localhost:9200/sortable-version-test/version/7?pretty=true" -d' | |
{ | |
"version": "1.0.0" | |
}' | |
# Wait for ES to be synced (aka refresh indices) | |
curl -X POST "http://localhost:9200/sortable-version-test/_refresh" | |
# Can successfully sort the version field | |
curl -X GET "http://localhost:9200/sortable-version-test/version/_search?sort=version.sortable&pretty=true" | |
# Can successfully do a comparison, assuming string is normalized in the query | |
curl -X GET "http://localhost:9200/sortable-version-test/version/_search?sort=version.sortable:desc&pretty=true" -d' | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"range": { | |
"version.sortable": { | |
"le": " 2 11 1" | |
} | |
} | |
} | |
] | |
} | |
} | |
}' | |
# Would rather be able to specify the un-normalized semantic version string, like this | |
curl -X GET "http://localhost:9200/sortable-version-test/version/_search?sort=version.sortable:desc&pretty=true" -d' | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"range": { | |
"version.sortable": { | |
"le": "2.11.1" | |
} | |
} | |
} | |
] | |
} | |
} | |
}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res = ''; | |
for (el in ctx._source['version'].tokenize('.')) { | |
res = res + el.padLeft(8); | |
} | |
ctx._source['version.sortable'] = res; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment