Skip to content

Instantly share code, notes, and snippets.

@mattweber
Last active August 29, 2015 14:00
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 mattweber/71033b1bf2ebed1afd8e to your computer and use it in GitHub Desktop.
Save mattweber/71033b1bf2ebed1afd8e to your computer and use it in GitHub Desktop.
Sum Aggregation Example
curl -XDELETE 'http://localhost:9200/aggtest'
curl -XPUT 'http://localhost:9200/aggtest/docs/1' -d '{"item_id": "foobar", "type": "cost", "value": 12.34, "timestamp": 149382734621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/2' -d '{"item_id": "bizbaz", "type": "sale", "value": 45.12, "timestamp": 149383464621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/3' -d '{"item_id": "foobar", "type": "sale", "value": 32.74, "timestamp": 149384824621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/4' -d '{"item_id": "foobar", "type": "cost", "value": 12.34, "timestamp": 149387435621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/5' -d '{"item_id": "bizbaz", "type": "sale", "value": 45.12, "timestamp": 149388434621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/6' -d '{"item_id": "bizbaz", "type": "cost", "value": 41.23, "timestamp": 149389424621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/7' -d '{"item_id": "foobar", "type": "sale", "value": 32.74, "timestamp": 149389914621}'
curl -XPUT 'http://localhost:9200/aggtest/docs/8?refresh=true' -d '{"item_id": "waahoo", "type": "sale", "value": 11.23, "timestamp": 149389914621}'
curl -XPOST 'http://localhost:9200/aggtest/_search?pretty' -d '{
"query": {
"match_all": {}
},
"aggs": {
"items_agg": {
"terms": {
"field": "item_id"
},
"aggs": {
"cost_agg": {
"filter": {
"term": {
"type": "cost"
}
},
"aggs": {
"cost_sum_agg": {
"sum": {
"field": "value"
}
}
}
},
"sale_agg": {
"filter": {
"term": {
"type": "sale"
}
},
"aggs": {
"sale_sum_agg": {
"sum": {
"field": "value"
}
}
}
}
}
}
}
}'
@mattweber
Copy link
Author

Output will look like:

"aggregations" : {
    "items_agg" : {
      "buckets" : [ {
        "key" : "foobar",
        "doc_count" : 4,
        "sale_agg" : {
          "doc_count" : 2,
          "sale_sum_agg" : {
            "value" : 65.48
          }
        },
        "cost_agg" : {
          "doc_count" : 2,
          "cost_sum_agg" : {
            "value" : 24.68
          }
        }
      }, {
        "key" : "bizbaz",
        "doc_count" : 3,
        "sale_agg" : {
          "doc_count" : 2,
          "sale_sum_agg" : {
            "value" : 90.24
          }
        },
        "cost_agg" : {
          "doc_count" : 1,
          "cost_sum_agg" : {
            "value" : 41.23
          }
        }
      }, {
        "key" : "waahoo",
        "doc_count" : 1,
        "cost_agg" : {
          "doc_count" : 0,
          "cost_sum_agg" : {
            "value" : 0.0
          }
        },
        "sale_agg" : {
          "doc_count" : 1,
          "sale_sum_agg" : {
            "value" : 11.23
          }
        }
      } ]
    }
  }
}

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