Skip to content

Instantly share code, notes, and snippets.

@byronvoorbach
Created October 4, 2017 13:52
Show Gist options
  • Save byronvoorbach/0c7dac8cb98c905c65515fac04fb0815 to your computer and use it in GitHub Desktop.
Save byronvoorbach/0c7dac8cb98c905c65515fac04fb0815 to your computer and use it in GitHub Desktop.
(Graphical) Filters
#LEXISNEXIS
DELETE shop
PUT shop
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"product": {
"properties": {
"title": {
"type": "text"
},
"colour": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"brand": {
"type": "keyword"
},
"size": {
"type": "keyword"
},
"price": {
"type": "double"
},
"family_id": {
"type": "keyword"
}
}
}
}
}
POST shop/product/1
{
"title": "iPad Air 2",
"colour": "Silver",
"brand": "Apple",
"size": "32gb",
"price": 399,
"family_id": "apple-1234"
}
POST shop/product/2
{
"title": "iPad Air 2",
"colour": "Gold",
"brand": "Apple",
"size": "32gb",
"price": 399,
"family_id": "apple-1234"
}
POST shop/product/3
{
"title": "iPad Air 2",
"colour": "Space Grey",
"brand": "Apple",
"size": "32gb",
"price": 399,
"family_id": "apple-1234"
}
POST shop/product/4
{
"title": "iPad Air 2",
"colour": "Space Grey",
"brand": "Apple",
"size": "128gb",
"price": 499,
"family_id": "apple-1234"
}
POST shop/product/5
{
"title": "iPad Pro",
"colour": "Space Grey",
"brand": "Apple",
"size": "128gb",
"price": 899,
"family_id": "apple-5678"
}
POST shop/product/6
{
"title": "iPad Pro",
"colour": "Space Grey",
"brand": "Apple",
"size": "256gb",
"price": 999,
"family_id": "apple-5678"
}
POST shop/product/7
{
"title": "Galaxy Tab 2 - Worse than the iPad",
"colour": "Piano Black",
"brand": "Samsung",
"size": "64gb",
"price": 249,
"family_id": "samsung-1234"
}
GET shop/_search
{
"query": {
"match": {
"title": "ipad"
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
GET shop/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "ipad"
}
}
],
"filter": {
"term": {
"colour.raw": "Space Grey"
}
}
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
#colours is filtered, losing information
GET shop/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "ipad"
}
}
]
}
},
"post_filter": {
"term": {
"colour.raw": "Space Grey"
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
#Fixed using post-filter
GET shop/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "ipad"
}
}
]
}
},
"post_filter": {
"term": {
"colour.raw": "Space Grey"
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
},
"filtered_aggs": {
"filter": {
"term": {
"colour.raw": "Space Grey"
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
}
}
#fix using seperate queries
GET shop/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"title": "ipad"
}
}
]
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
GET shop/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "ipad"
}
}
],
"filter": {
"term": {
"colour.raw": "Space Grey"
}
}
}
},
"aggs": {
"colours": {
"terms": {
"field": "colour.raw",
"size": 10
}
},
"sizes": {
"terms": {
"field": "size",
"size": 10
}
},
"brands": {
"terms": {
"field": "brand",
"size": 10
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment