ElasticSearch配置ik中文分詞器

一、Versions

ES Version: 2.1.1

IK Version: 1.6.2

說明:ES和IK的版本一定要相互兼容,具體兼容性可以查看下面的git說明

 

二、Install

  1. IK的下載:https://github.com/medcl/elasticsearch-analysis-ik
    裏面附有安裝步驟可以參考,本步驟是略作補充和說明
  2. compile
    mvn package
    說明:這個使用mvn命令時很可能會出問題,建議使用工具如IDEA等打包

    複製 target/releases/elasticsearch-analysis-ik-{version}.zip to your-es-root/plugins/ik,將zip包解壓
    說明:僅將zip包裏的文件放到ik文件夾中
  3. 配置config文件
    將項目中config文件下的ik文件夾複製到your-es-root/config
  4. 配置elasticsearch.yml
    在elasticsearch.yml文件的最後添加配置(最簡單的配置):

    index :
        analysis :
            analyzer :
                ik :
                    type: custom
                    tokenizer: ik_max_word
  5. restart ES

三、Quick Example

(以下大部分摘抄github中的步驟,不同的地方會做標紅)可以使用chrome的插件postman,非常好用

  1. 創建index

    curl -XPUT http://localhost:9200/index

     

     

  2. 創建mapping(此處與github上不同)

    curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
    {
        "fulltext": {
                 "_all": {
                "analyzer""ik",
                "search_analyzer""ik_max_word",
                "term_vector""no",
                "store""false"
            },
            "properties": {
                "content": {
                    "type""string",
                    "store""no",
                    "term_vector""with_positions_offsets",
                    "analyzer""ik",
                    "search_analyzer""ik_max_word",
                    "include_in_all""true",
                    "boost"8
                }
            }
        }
    }
  3. 添加文檔

    curl -XPOST http://localhost:9200/index/fulltext/1 -d'
    {"content":"美國留給伊拉克的是個爛攤子嗎"}
    '
    curl -XPOST http://localhost:9200/index/fulltext/2 -d'
    {"content":"公安部:各地校車將享最高路權"}
    '
    curl -XPOST http://localhost:9200/index/fulltext/3 -d'
    {"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
    '
    curl -XPOST http://localhost:9200/index/fulltext/4 -d'
    {"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"}
    '
  4. 查詢

    curl -XPOST http://localhost:9200/index/fulltext/_search  -d'
    {
        "query" : { "term" : { "content" "中國" }},
        "highlight" : {
            "pre_tags" : ["<tag1>""<tag2>"],
            "post_tags" : ["</tag1>""</tag2>"],
            "fields" : {
                "content" : {}
            }
        }
    }
    '
  5. 結果

四、其他

參考github上的相關說明:https://github.com/medcl/elasticsearch-analysis-ik

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章