# 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" } } } } } } '