Skip to content

Instantly share code, notes, and snippets.

@2e3s
Created March 3, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2e3s/599330bfef10e9375f6f to your computer and use it in GitHub Desktop.
Save 2e3s/599330bfef10e9375f6f to your computer and use it in GitHub Desktop.
# Remove old data
curl -XDELETE "http://localhost:9200/multisort"
# Create Document
curl -XPOST "http://localhost:9200/_bulk" -d '
{"create": {"_index": "multisort", "_type": "multisort"}}
{"value": 1, "group": 1, "sum": 10}
{"create": {"_index": "multisort", "_type": "multisort"}}
{"value": 2, "group": 1, "sum": 10}
{"create": {"_index": "multisort", "_type": "multisort"}}
{"value": 3, "group": 2, "sum": 1}
{"create": {"_index": "multisort", "_type": "multisort"}}
{"value": 4, "group": 2, "sum": 1}
{"create": {"_index": "multisort", "_type": "multisort"}}
{"value": 6, "group": 3, "sum": 2}
'
# Wait for ES to be synced (aka refresh indices)
curl -XPOST "http://localhost:9200/multisort/_refresh"
# The result is sorted by sum
curl -XPOST "http://localhost:9200/multisort/multisort/_search?pretty=true&search_type=count" -d '
{
"query":{"range": {"value": {"gte": 1}}},
"aggregations": {
"rows": {
"terms": {
"field": "group",
"order": {
"_count": "desc",
"sum": "desc"
}
},
"aggs": {
"sum": {
"sum": {
"field": "sum"
}
}
}
}
}
}
'
# The result:
#
# "aggregations" : {
# "rows" : {
# "buckets" : [ {
# "key" : 1,
# "doc_count" : 2,
# "sum" : {
# "value" : 20.0
# }
# }, {
# "key" : 3,
# "doc_count" : 1,
# "sum" : {
# "value" : 2.0
# }
# }, {
# "key" : 2,
# "doc_count" : 2,
# "sum" : {
# "value" : 2.0
# }
# } ]
# }
# }
# The result is still the same
curl -XPOST "http://localhost:9200/multisort/multisort/_search?pretty=true&search_type=count" -d '
{
"query":{"range": {"value": {"gte": 1}}},
"aggregations": {
"rows": {
"terms": {
"field": "group",
"order": {
"_count": "asc",
"sum": "desc"
}
},
"aggs": {
"sum": {
"sum": {
"field": "sum"
}
}
}
}
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment