Skip to content

Instantly share code, notes, and snippets.

@markharwood
Created May 17, 2016 14:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markharwood/86246ef2983b286611f4c4439dee5e99 to your computer and use it in GitHub Desktop.
Save markharwood/86246ef2983b286611f4c4439dee5e99 to your computer and use it in GitHub Desktop.
Accounting for items with multiple Categories and subcategories
DELETE sandbox
PUT sandbox
{
"mappings": {
"test-cat": {
"properties": {
"title": {
"type": "string"
},
"classifications": {
"type": "nested",
"properties": {
"category": {
"type": "string"
},
"subcategory": {
"type": "string"
}
}
}
}
}
}
}
DELETE /sandbox/test-cat/1
POST /sandbox/test-cat/1
{
"title": "Item 1",
"classifications": [
{
"category": "A",
"subcategory": [
"A1",
"A2"
]
},
{
"category": "B",
"subcategory": [
"B1"
]
}
]
}
DELETE /sandbox/test-cat/2
POST /sandbox/test-cat/2
{
"title": "Item 2",
"classifications": [
{
"category": "A",
"subcategory": [
"A1"
]
}
]
}
POST /sandbox/test-cat/_search?search_type=count
{
"aggs": {
"classifications": {
"nested": {
"path": "classifications"
},
"aggs": {
"categories": {
"terms": {
"field": "classifications.category"
},
"aggs": {
"sub_categories": {
"terms": {
"field": "classifications.subcategory"
}
}
}
}
}
}
},
"query": {
"filtered": {
"query": {
"match_all": {}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment