————Completion Suggester——————
- 定义Mapping,使用“completion"type;
- 索引数据
- 运行”suggest“查询,得到搜索建议
DELETE articles
PUT articles
{
"mappings": {
"properties": {
"title_completion":{
"type": "completion"
}
}
}
}
POST articles/_bulk
{"index":{}}
{"title_completion":"luncene is very cool"}
{"index":{}}
{"title_completion":"Elasticsearch builds on top of luncene"}
{"index":{}}
{"title_completion":"Elasticsearch rocks"}
{"index":{}}
{"title_completion":"elastic is the company behind ELK stack"}
{"index":{}}
{"title_completion":"Elk stack rocks"}
{"index":{}}
{"title_completion":"elasticsearch is rock solid"}
POST articles/_search
{
"size": 0,
"suggest": {
"article-suggester": {
"prefix": "elk",
"completion": {
"field": "title_completion"
}
}
}
}
————Completion Suggester By Context ——————
- 可以定义两种类型的Context
Category —— 任一的字符串
Geo —— 地理位置信息
- 实现Context Suggester的具体步骤
定制一个Mapping
索引数据,并且为每个文档加入Context信息
结合Context进行Suggestion 查询
PUT comments
PUT comments/_mapping
{
"properties": {
"comment_autocomplete":{
"type":"completion",
"contexts":{
"type":"category",
"name":"comment_category"
}
}
}
}
POST comments/_doc
{
"comment":"I love the star war movies",
"comment_autocomplete":{
"input":["star wars"],
"contexts":{
"comment_category":"movies"
}
}
}
POST comments/_doc
{
"comment":"Where I find a Starbucks",
"comment_autocomplete":{
"input":["starbucks"],
"contexts":{
"comment_category":"coffee"
}
}
}
POST comments/_search
{
"suggest": {
"mamj_suggestion": {
"prefix": "sta",
"completion":{
"field":"comment_autocomplete",
"contexts":{
"comment_category":"coffee"
}
}
}
}
}