当前位置: 首页 > 工具软件 > Tag Suggest > 使用案例 >

elasicsearch suggest用法

宫高义
2023-12-01

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,否则报错

 类似资料: