Skip to content

Instantly share code, notes, and snippets.

@gjstockham
Created May 16, 2016 16:07
Show Gist options
  • Save gjstockham/bdd47c17a44afdd59968d0d5c306482e to your computer and use it in GitHub Desktop.
Save gjstockham/bdd47c17a44afdd59968d0d5c306482e to your computer and use it in GitHub Desktop.
Elasticsearch hierarchical aggregations
DELETE sandbox
PUT sandbox
{
"mappings": {
"test-cat": {
"properties": {
"title": {
"type": "string"
},
"categories": {
"type": "object",
"properties": {
"children": {
"type": "nested",
"properties": {
"children": {
"type": "nested",
"properties": {}
},
"name": {
"type": "string"
}
}
},
"name": {
"index": "not_analyzed",
"omit_norms": true,
"type": "string"
}
}
}
}
}
}
}
GET /_all
DELETE /sandbox/test-cat/1
POST /sandbox/test-cat/1
{
"title": "Item 1",
"categories": [
{
"name": "A",
"children": [
{
"name": "A1"
},
{
"name": "A2"
}]
},
{
"name": "B",
"children": [
{
"name": "B1"
}]
}]
}
DELETE /sandbox/test-cat/2
POST /sandbox/test-cat/2
{
"title": "Item 2",
"categories": [
{
"name": "A",
"children": [
{
"name": "A1"
}]
}]
}
POST /sandbox/test-cat/_search?search_type=count
{
"aggs": {
"category": {
"terms": {
"field": "categories.name"
},
"aggs": {
"nested_category" : {
"nested" : {
"path": "categories.children"
},
"aggs": {
"child_categories" : {
"terms": {
"field": "categories.children.name"
}
}
}
}
}
}
},
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment