Skip to content

Instantly share code, notes, and snippets.

@spinscale
Created March 4, 2020 15:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spinscale/259cc770eba53e91365f36cd6ca2598d to your computer and use it in GitHub Desktop.
Save spinscale/259cc770eba53e91365f36cd6ca2598d to your computer and use it in GitHub Desktop.
Percolator example (training MUC)
DELETE back_in_stock_notifications
DELETE test
PUT test/_bulk?refresh
{ "index" : { "_id" : "1"}}
{ "id": "1", "title" : "T-Shirt", "size" : "XL", "color": "red", "stock" : 0 }
{ "index" : { "_id" : 2}}
{ "id": "2", "title" : "T-Shirt", "size" : "XL", "color": "green", "stock" : 1 }
{ "index" : { "_id" : 3}}
{ "id": "3", "title" : "T-Shirt", "size" : "XL", "color": "yellow", "stock" : 2 }
GET test/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"stock": {
"gte": 1
}
}
},
{
"match": {
"title": "shirt"
}
}
]
}
}
}
# Damn red is not on stock! Let's get a notification if it is back in stock
PUT back_in_stock_notifications
{
"mappings": {
"properties": {
"query": {
"type": "percolator"
},
"stock" : { "type" : "long"},
"id" : { "type" : "keyword"}
}
}
}
PUT back_in_stock_notifications/_doc/1?refresh=true
{
"email": "alex@elastic.co",
"query": {
"bool": {
"must": [
{
"range": {
"stock": {
"gt": 0
}
}
},
{
"match": {
"id": "1"
}
}
]
}
}
}
# uhuh! back in stock
PUT test/_bulk?refresh
{ "index" : { "_id" : 1}}
{ "id": "1", "title" : "T-Shirt", "size" : "XL", "color": "red", "stock" : 100 }
GET back_in_stock_notifications/_search
{
"query": {
"percolate": {
"field": "query",
"document": {
"id": "1",
"title": "T-Shirt",
"size": "XL",
"color": "red",
"stock": 100
}
}
}
}
# show me the emails only!
GET back_in_stock_notifications/_search?filter_path=**.email
{
"query": {
"percolate": {
"field": "query",
"document": {
"id": "1",
"title": "T-Shirt",
"size": "XL",
"color": "red",
"stock": 100
}
}
}
}
GET back_in_stock_notifications/_search?filter_path=**.email
{
"query": {
"percolate": {
"field": "query",
"index" : "test",
"id" : "1"
}
}
}
# further use-cases
# get notified about changing stock values
# register criteria for searching a new flat/house
# simple classification for ads based on the document content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment