Skip to content

Instantly share code, notes, and snippets.

@luizgpsantos
Last active August 29, 2015 14:11
Show Gist options
  • Save luizgpsantos/e7347e8574ab6eede37b to your computer and use it in GitHub Desktop.
Save luizgpsantos/e7347e8574ab6eede37b to your computer and use it in GitHub Desktop.
context suggester
# create an index called "services" with suggest_field mapped as "completion" with payloads
POST services
{
"mappings": {
"service": {
"properties": {
"name": {
"type": "string"
},
"suggest_field": {
"type": "completion",
"payloads": true,
"context": {
"color": {
"type": "category"
}
}
}
}
}
}
}
# index a document with id = 1. The input of the completion is "same_input" with
# weight = 100 and payload {"url": "first_document"}
PUT services/service/1
{
"name": "same_input",
"suggest_field": {
"input": "same_input",
"weight": 100,
"payload": {
"url": "first_document"
},
"context": {
"color": ["red", "yellow"]
}
}
}
# index a document with id = 2. The input of the completion is "same_input" too with
# weight = 200 and payload {"url": "second_document"}
PUT services/service/2
{
"name": "same_input",
"suggest_field": {
"input": "same_input",
"weight": 200,
"payload": {
"url": "second_document"
},
"context": {
"color": ["red", "green"]
}
}
}
# query for suggest using "same_input" as text
POST services/_suggest?pretty'
{
"suggest" : {
"text" : "same_input",
"completion" : {
"field" : "suggest_field",
"size": 10,
"context": {
"color": ["red"]
}
}
}
}
# the response includes only one document with the score 200, which is the
# weigth of the document id = 2 but with the payload of the document
# of id = 1 when using context "red".
{
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"suggest": [
{
"text": "same_input",
"offset": 0,
"length": 10,
"options": [
{
"text": "same_input",
"score": 200,
"payload": {
"url": "first_document"
}
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment