elasticsearch-搜索建议(八)

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}
}

 

posted @ 2019-03-18 15:04  意犹未尽  阅读(928)  评论(0编辑  收藏  举报