环境

  • Elasticsearch 2.3.5
  • Elasticsearch-ik-plugin

实现

  • 寻建议之对象
    一旦来以下简单只json对象,需要针对中tags字段进行搜索建议:

//对象Product1
{
    "title": "Product1",
    "description": "Product1 Description",
    "tags": [
        "山东",
        "山东高新开发区",
        "山东大学",
        "two columns",
        "wordpress"
    ]
}

//对象Product2
{
    "title": "Product2",
    "description": "Product2 Description",
    "tags": [
        "山东省",
        "山东公安局",
        "山东检察院",
        "skrill",
        "wordland"
    ]
}
  • 安装索引mapping
    建立索引suggester_ik_test和mapping,如下:
    专注运用的suggester类型为completion

curl -XPUT "http://10.110.13.57:9200/suggester_ik_test?pretty" -d'
    {
   "mappings": {
      "product": {
         "properties": {
            "description": {
               "type": "string"
            },
            "tags": {
               "type": "string"
            },
            "title": {
               "type": "string"
            },
            "tag_suggest": {
               "type": "completion",
               "analyzer": "ik_max_word",
               "search_analyzer": "ik_max_word",
               "payloads": false
            }
         }
      }
   }
}'
  • 目录数据
    根据上述索引和mapping对json数据建立目录:

//Product1
curl -XPUT "http://10.110.13.57:9200/suggester_ik_test/product/1?pretty" -d'
    {
   "title": "Product1",
   "description": "Product1 Description",
   "tags": [
         "山东",
         "山东高新开发区",
         "山东大学",
         "two columns",
         "wordpress"
   ],
   "tag_suggest": {
      "input": [
         "山东",
         "山东高新开发区",
         "山东大学",
         "two columns",
         "wordpress"
      ]
   }
}'

//Product2
curl -XPUT "http://10.110.13.57:9200/suggester_ik_test/product/2?pretty" -d'
{
    "title": "Product2",
    "description": "Product2 Description",
    "tags": [
        "山东省",
        "山东公安局",
        "山东检察院",
        "skrill",
        "wordland"
    ],
   "tag_suggest": {
      "input": [
        "山东省",
        "山东公安局",
        "山东检察院",
        "skrill",
        "wordland"
      ]
   }
}'
  • 测试
    摸索“山东”,查看是否来追寻建议提拔生效:

curl -XPOST "http://10.110.13.57:9200/suggester_ik_test/_suggest?pretty" -d'
    {
    "product_suggest":{
        "text":"山东",
        "completion": {
            "field" : "tag_suggest"
        }
    }
}'

//应该得到以下数据
{
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "product_suggest" : [ {
    "text" : "山东",
    "offset" : 0,
    "length" : 2,
    "options" : [ {
      "text" : "山东",
      "score" : 1.0
    }, {
      "text" : "山东公安局",
      "score" : 1.0
    }, {
      "text" : "山东大学",
      "score" : 1.0
    }, {
      "text" : "山东检察院",
      "score" : 1.0
    }, {
      "text" : "山东省",
      "score" : 1.0
    } ]
  } ]
}

参考资料

Quick and Dirty Autocomplete with Elasticsearch Completion
Suggest

相关文章

网站地图xml地图