elasicsearch suggest用法

elasicsearchsuggest用法

1 第一步:要使用suggest先配置好

    curl -XPUT 192.168.2.20:9200/xiaogu -d'  //新建一个xiaogu的索引
    {
       "mappings": {
          "product": { //这个是_type
             "properties": {
                "description": {
                   "type": "string"
                },
                "tags": {
                   "type": "string"
                },
                "title": {
                   "type": "string"
                },
                "tag_suggest": {
                   "type": "completion",  //设置为completion才能被suggest捕获
                   "index_analyzer": "simple",
                   "search_analyzer": "simple",
                   "payloads": false
                }
             }
          }
       }
    }'

2 第二步:添加测试数据

curl -XPUT  192.168.2.20:9200/xiaogu/product/1 -d'
{
   "title": "Product1",
   "description": "Product1 Description",
   "tags": [
      "blog",
      "magazine",
      "responsive",
      "two columns",
      "wordpress"
   ],
   "tag_suggest": {
      "input": [
         "blog",
         "magazine",
         "responsive",
         "two columns",
         "wordpress"
      ]
   }
}'

curl -XPUT 192.168.2.20:9200/xiaogu/product/1 -d'
{

    "title": "Product2",
    "description": "Product2 Description",
    "tags": [
        "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
    ],
   "tag_suggest": {
      "input": [
         "blog",
        "paypal",
        "responsive",
        "skrill",
        "wordland"
      ]
   }
}'

3 开始搜索

curl -XPOST 192.168.2.20:9200/xiaogu/_suggest -d'
{
    "product_suggest":{
        "text":"word",
        "completion": {
            "field" : "tag_suggest"
        }
    }
}'

4 结果

{
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "product_suggest": [
      {
         "text": "word",
         "offset": 0,
         "length": 4,
         "options": [
            {
               "text": "wordland",
               "score": 1
            },
            {
               "text": "wordpress",
               "score": 1
            }
         ]
      }
   ]
}

ps:

  1. 如果是在window记得转义” ,详情见 第一点

  2. 要在那个字段使用suggest,请记得设置其type:completion,否则报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值