Skip to content

Instantly share code, notes, and snippets.

@markharwood
Created April 2, 2014 17:28
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 markharwood/9938890 to your computer and use it in GitHub Desktop.
Save markharwood/9938890 to your computer and use it in GitHub Desktop.
Rough Gist to aggregate nested values
#!/bin/sh
es_host="http://localhost:9200"
curl -XDELETE "$es_host/contests"
curl -XPUT "$es_host/contests" -d '
{
"settings": {
"index.number_of_replicas": 0,
"index.number_of_shards": 1,
"index.refresh_interval": -1
},
"mappings": {
"contest": {
"properties": {
"contestant": {
"type": "nested",
"properties": {
"id": {
"type": "string",
"index": "not_analyzed"
},
"timing": {
"type": "integer"
},
"expectedTiming": {
"type": "integer"
}
}
}
}
}
}
}'
curl -XPUT "http://localhost:9200/contests/contest/1" -d'
{
"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/contests/contest/2" -d'
{
"contestant": [
{
"id": "alice",
"data": [
{
"timing": 1,
"expectedTiming": 2
},
{
"timing": 2,
"expectedTiming": 2
}
]
},
{
"id": "jane",
"data": [
{
"timing": 3,
"expectedTiming": 2
},
{
"timing": 4,
"expectedTiming": 2
}
]
}
]
}'
curl -XPOST 'http://localhost:9200/contests/_flush'
curl -XGET "http://localhost:9200/contests/_search" -d'
{
"aggs": {
"myResults": {
"nested": {
"path": "contestant"
},
"aggs": {
"per_contestant": {
"terms": {
"field": "id"
},
"aggs": {
"timingInfo": {
"sum": {
"field": "contestant.data.timing"
}
}
}
}
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment