elasticsearch Suggester实现智能提示

Posted lqblog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了elasticsearch Suggester实现智能提示相关的知识,希望对你有一定的参考价值。

Completion Suggester

智能提示

{
    "settings": {
    
    },
    "mappings": {
        "doc": {
            "properties": {
                "productName": {
                  "type" : "text",
                   "analyzer":"ik_max_word"
                },
                "productNameSuggester":{
                    "type" : "completion",//智能提示字段
                    "search_analyzer":"ik_max_word",//搜索使用分词默认跟索引一直
                    "analyzer":"ik_max_word",//索引分词
                    "max_input_length":50,//提示字段长度 如果为2 雪花啤酒 输入雪 提示雪花  输入雪花 也提示雪花
                    "preserve_separators":true,//默认值  如果希望Foo Fighters  输入foof能提示的话则改为false
                    "preserve_position_increments":true //启用停用分词器
                }
            }
        }
    }

}

 

插入数据

{
    "productName":"雪花纯生8度500ml统一标准专用白瓶1*6纸箱手提",
    "productNameSuggester":"雪花纯生8度500ml统一标准专用白瓶1*6纸箱手提"
}
{
    "productName":"雪花勇闯天涯普啤8度500ml白瓶1*12纸箱活动版",
    "productNameSuggester":"雪花勇闯天涯普啤8度500ml白瓶1*12纸箱活动版"
}

post请求:http://127.0.0.1:9200/custom/doc/_search

{
  "_source":false,// ["productNameSuggester","productName"] 不返回_source
  "suggest": {
    "my-suggest-1": {
      "text": "雪",//用户输入文本
      "completion": {
        "field": "productNameSuggester",//字段
        "size": 2,//显示条数
        "skip_duplicates": true//建议去重
      }
    }
  }
}

返回结果

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": 0,
        "hits": []
    },
    "suggest": {
        "my-suggest-1": [
            {
                "text": "雪",
                "offset": 0,
                "length": 1,
                "options": [
                    {
                        "text": "雪花勇闯天涯普啤8度500ml白瓶1*12纸箱活动版",
                        "_index": "custom",
                        "_type": "doc",
                        "_id": "1",
                        "_score": 1
                    },
                    {
                        "text": "雪花纯生8度500ml统一标准专用白瓶1*6纸箱手提",
                        "_index": "custom",
                        "_type": "doc",
                        "_id": "2",
                        "_score": 1
                    }
                ]
            }
        ]
    }
}

设置权重

input为保存的值 weight为权重(_score)如果是数组使用[{"input":""},{"input":""}]

{
    "productName":"雪花纯生8度500ml统一标准专用白瓶1*6纸箱手提",
    "productNameSuggester":{"input":"雪花纯生8度500ml统一标准专用白瓶1*6纸箱手提","weight" : 34}
}

 

以上是关于elasticsearch Suggester实现智能提示的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch:Completion suggester - 实现 Search-As-You-Type

未找到 Elasticsearch NEST Suggester 解析器

Elasticsearch Elasticsearch Suggester 自动纠错 详解

elasticsearch 中文 term & completion suggester

elasticsearch补全功能之只补全筛选后的部分数据context suggester

Elasticsearch Suggester API(自动补全)