DELETE articles
PUT articles
{
"mappings": {
"properties": {
"title_completion":{
"type": "completion"
}
}
}
}
POST articles/_bulk
{ "index" : { } }
{ "title_completion": "lucene is very cool"}
{ "index" : { } }
{ "title_completion": "Elasticsearch builds on top of lucene"}
{ "index" : { } }
{ "title_completion": "Elasticsearch rocks"}
{ "index" : { } }
{ "title_completion": "elastic is the company behind ELK stack"}
{ "index" : { } }
{ "title_completion": "Elk stack rocks"}
{ "index" : {} }
POST articles/_search?pretty
{
"size": 0,
"suggest": {
"article-suggester": {
"prefix": "elk",
"completion": {
"field": "title_completion"
}
}
}
}
重点在于将这个字段的type指定为 completion
"properties": {
"title_completion":{
"type": "completion"
}
}
如何基于上下文的提示
PUT comments
{
"mappings": {
"properties": {
"comment_autocomplete":{
"type": "completion",
"contexts":[
{
"type":"category",
"name":"comment_category"
}
]
}
}
}
}
将字段comment_autocomplete
的type 指定为completion
,并且添加contexts
,type 为 category, 取名为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 can I find a Starbucks",
"comment_autocomplete":{
"input":["starbucks"],
"contexts":{
"comment_category":"coffee"
}
}
}
查看mapping
{
"comments" : {
"mappings" : {
"properties" : {
"comment_autocomplete" : {
"type" : "completion",
"analyzer" : "simple",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50,
"contexts" : [
{
"name" : "comment_category",
"type" : "CATEGORY"
}
]
}
}
}
}
}
搜索
POST comments/_search
{
"suggest": {
"my_suggestion": {
"prefix": "star",
"completion": {
"field": "comment_autocomplete",
"contexts":{
"comment_category":"movies"
}
}
}
}
}
通过comment_category
感知到用户当前在电影频道