Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Need a better approach to sorting semantic version strings in Elasticsearch
# 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"
}
}
}
]
}
}
}'
res = '';
for (el in ctx._source['version'].tokenize('.')) {
res = res + el.padLeft(8);
}
ctx._source['version.sortable'] = res;
@ejsmith
Copy link

ejsmith commented Jan 26, 2015

Is this the approach that you ended up going with? I am wanting to be able to search by version ranges as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment