Skip to content

Instantly share code, notes, and snippets.

@dazraf
Created April 2, 2014 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dazraf/9937198 to your computer and use it in GitHub Desktop.
Save dazraf/9937198 to your computer and use it in GitHub Desktop.
Aggregation of nested arrays using dynamic templates for nested types
curl -XPUT "http://localhost:9200/_template/logstash" -d'
{
"template": "logstash-*",
"settings": {
"index.refresh_interval": "5s"
},
"mappings": {
"_default_": {
"dynamic_templates": [
{
"string_fields": {
"mapping": {
"index": "analyzed",
"omit_norms": true,
"type": "string",
"fields": {
"raw": {
"index": "not_analyzed",
"ignore_above": 256,
"type": "string"
}
}
},
"match_mapping_type": "string",
"match": "*"
}
},
{
"nested_objects": {
"mapping": {
"type": "nested",
"stored": "true"
},
"match": "*",
"match_mapping_type": "object"
}
}
],
"properties": {
"geoip": {
"dynamic": true,
"path": "full",
"properties": {
"location": {
"type": "geo_point"
}
},
"type": "object"
},
"@version": {
"index": "not_analyzed",
"type": "string"
}
},
"_all": {
"enabled": true
}
}
}
}'
curl -XDELETE "http://localhost:9200/logstash-1"
curl -XPUT "http://localhost:9200/logstash-1/log/1" -d'
{"statistics":{"contestant":[{"id":"alice","data":[{"timing":1,"expectedTiming":2},{"timing":2,"expectedTiming":2}]},{"id":"jane","data":[{"timing":3,"expectedTiming":2},{"timing":4,"expectedTiming":2}]}]}}'
curl -XPUT "http://localhost:9200/logstash-1/log/2" -d'
{"statistics":{"contestant":[{"id":"alice","data":[{"timing":1,"expectedTiming":2},{"timing":2,"expectedTiming":2}]},{"id":"jane","data":[{"timing":3,"expectedTiming":2},{"timing":4,"expectedTiming":2}]}]}}'
curl -XGET "http://localhost:9200/logstash-1/_search" -d'
{
"aggs": {
"by_contestant": {
"terms": {
"field": "statistics.contestant.id"
},
"aggs": {
"total_time": {
"sum": {
"field": "statistics.contestant.data.timing"
}
}
}
}
}
}'
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "logstash-1",
"_type": "log",
"_id": "2",
"_score": 1,
"_source": {
"statistics": {
"contestant": [
{
"id": "alice",
"data": [
{
"timing": 1,
"expectedTiming": 2
},
{
"timing": 2,
"expectedTiming": 2
}
]
},
{
"id": "jane",
"data": [
{
"timing": 3,
"expectedTiming": 2
},
{
"timing": 4,
"expectedTiming": 2
}
]
}
]
}
}
},
{
"_index": "logstash-1",
"_type": "log",
"_id": "1",
"_score": 1,
"_source": {
"statistics": {
"contestant": [
{
"id": "alice",
"data": [
{
"timing": 1,
"expectedTiming": 2
},
{
"timing": 2,
"expectedTiming": 2
}
]
},
{
"id": "jane",
"data": [
{
"timing": 3,
"expectedTiming": 2
},
{
"timing": 4,
"expectedTiming": 2
}
]
}
]
}
}
}
]
},
"aggregations": {
"by_contestant": {
"buckets": []
}
}
}
curl -XGET "http://localhost:9200/logstash-1/_search" -d'
{
"aggs": {
"nested": {
"path": "statistics.contestant"
},
"per_contestant": {
"terms": {
"field": "id"
},
"aggs": {
"nested": "data",
"total_time": {
"sum": {
"field": "timing"
}
}
}
}
}
}'
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "logstash-1",
"_type": "log",
"_id": "2",
"_score": 1,
"_source": {
"statistics": {
"contestant": [
{
"id": "alice",
"data": [
{
"timing": 1,
"expectedTiming": 2
},
{
"timing": 2,
"expectedTiming": 2
}
]
},
{
"id": "jane",
"data": [
{
"timing": 3,
"expectedTiming": 2
},
{
"timing": 4,
"expectedTiming": 2
}
]
}
]
}
}
},
{
"_index": "logstash-1",
"_type": "log",
"_id": "1",
"_score": 1,
"_source": {
"statistics": {
"contestant": [
{
"id": "alice",
"data": [
{
"timing": 1,
"expectedTiming": 2
},
{
"timing": 2,
"expectedTiming": 2
}
]
},
{
"id": "jane",
"data": [
{
"timing": 3,
"expectedTiming": 2
},
{
"timing": 4,
"expectedTiming": 2
}
]
}
]
}
}
}
]
},
"aggregations": {
"by_contestant": {
"buckets": []
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment