Skip to content

Commit a775322

Browse files
committed
added sub aggregation example
1 parent 38b76a3 commit a775322

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,4 +802,56 @@ curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/logstash-2
802802
}
803803
}
804804
}'
805+
```
806+
807+
## Sub aggregations
808+
809+
If you want to do an aggregation on text / string data you need to have a 'raw' version of that data in your index - otherwise query like the one below will not work!
810+
811+
So first the delete + mapping:
812+
813+
```
814+
`curl -H "Content-Type: application/json" -XDELETE http://localhost:9200/ratings`
815+
```
816+
817+
```
818+
curl -H "Content-Type: application/json" -XPUT 'http://localhost:9200/ratings?pretty' -d '
819+
{
820+
"mappings": {
821+
"rating": {
822+
"properties": {
823+
"title": {
824+
"type": "text",
825+
"fielddata": true,
826+
"fields": {
827+
"raw": {
828+
"type": "keyword"
829+
}
830+
}
831+
}
832+
}
833+
}
834+
}
835+
}'
836+
```
837+
838+
```
839+
curl -H "Content-Type: application/json" -XGET 'http://localhost:9200/ratings/rating/_search?size=0&pretty' -d '
840+
{
841+
"query": {
842+
"match_phrase": {
843+
"title": "Star Wars"
844+
}
845+
},
846+
"aggs": {
847+
"titles": {
848+
"terms": {
849+
"field": "title.raw"
850+
},
851+
"aggs": {
852+
"avg_rating": { "avg": { "field": "rating" } }
853+
}
854+
}
855+
}
856+
}'
805857
```

0 commit comments

Comments
 (0)