ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Elsticsearch 自动补全以及基于上下文的提示

2019-10-24 11:03:27  阅读:300  来源: 互联网

标签:completion comment 补全 category title index Elsticsearch 上下文 type


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 感知到用户当前在电影频道

只可以用于前缀索引

标签:completion,comment,补全,category,title,index,Elsticsearch,上下文,type
来源: https://blog.csdn.net/qq_40990836/article/details/102717281

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有