zoukankan      html  css  js  c++  java
  • Elasticsearch suggester搜索建议初步

    环境

    • 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

  • 相关阅读:
    精选30道Java笔试题解答
    ASM
    Java Decompiler Plugin For Eclipse IDE
    AMQ5540, AMQ5541 and AMQ5542, application did not supply a user ID and password, 2035 MQRC_NOT_AUTHORIZED
    Shell脚本中的export
    Linux set unset命令
    shell中${}的妙用
    ubuntu alsa2
    ubuntu alsa
    计算机启动boot
  • 原文地址:https://www.cnblogs.com/myitroad/p/5789202.html
Copyright © 2011-2022 走看看