Last active
August 29, 2015 14:11
-
-
Save luizgpsantos/e7347e8574ab6eede37b to your computer and use it in GitHub Desktop.
context suggester
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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