Elasticsearch插件之分詞ik

Elasticsearch本身對中文的分詞支持不是很好,可以通過安裝elasticsearch的ik分詞器插件elasticsearch-analysis-ik來實現(xiàn)中文分詞。

一、下載安裝篇

方法一:
官方網(wǎng)站下載, 告訴你,可以下載源碼,然后自己去編譯,這樣比較麻煩,可以直接它的版本庫中下載編譯好的歷史版本

方法二:
下載elasticsearch-analysis-ik-v1.9.5編譯包,注意要下載編譯好的包,而不是源碼包。
例如:得到壓縮包 elasticsearch-analysis-ik-1.9.5.zip 進(jìn)入elasticsearchplugins目錄,解壓,重命名 analysis-ik ,然后將 analysis-ik/config 整個目錄文件拷貝到 elasticsearch-2.3.5/config/ik/ 下面 , 如果elasticsearch-2.3.5/config 下面沒有ik目錄,需要新建ik目錄。
相關(guān)命令:
unzip elasticsearch-analysis-ik-1.9.5.zip -d ./analysis-ik/ -d參數(shù),解壓到指定目錄
cp -rf analysis-ik/config/ ../config/ik/ -rf參數(shù),復(fù)制文檔到指定目錄

進(jìn)入 elasticsearch-2.3.5/bin 重啟elasticsearch服務(wù)

二、驗證ik分詞案例

  1. create index (創(chuàng)建索引(創(chuàng)建庫))
curl -XPUT http://localhost:9200/index
  1. create a mapping (創(chuàng)建映射(表設(shè)計))
curl -XPOST http://localhost:9200/index/fulltext/_mapping -d'
{
    "fulltext": {
             "_all": {
            "analyzer": "ik_max_word",
            "search_analyzer": "ik_max_word",
            "term_vector": "no",
            "store": "false"
        },
        "properties": {
            "content": {
                "type": "string",
                "store": "no",
                "term_vector": "with_positions_offsets",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word",
                "include_in_all": "true",
                "boost": 8
            }
        }
    }
}'
  1. index some docs (插入數(shù)據(jù)內(nèi)容)
curl -XPOST http://localhost:9200/index/fulltext/1 -d'
{"content":"美國留給伊拉克的是個爛攤子嗎"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -d'
{"content":"公安部:各地校車將享最高路權(quán)"}
'

curl -XPOST http://localhost:9200/index/fulltext/3 -d'
{"content":"中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -d'
{"content":"中國駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首"}
'
  1. query with highlighting(高亮查詢)
curl -XPOST http://localhost:9200/index/fulltext/_search  -d'
{
    "query" : { "term" : { "content" : "中國" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'

Result(結(jié)果)

{
    "took": 6,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 1.5,
        "hits": [
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "4",
                "_score": 1.5,
                "_source": {
                    "content": "中國駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首"
                },
                "highlight": {
                    "content": [
                        "<tag1>中國</tag1>駐洛杉磯領(lǐng)事館遭亞裔男子槍擊 嫌犯已自首"
                    ]
                }
            },
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "3",
                "_score": 0.53699243,
                "_source": {
                    "content": "中韓漁警沖突調(diào)查:韓警平均每天扣1艘中國漁船"
                },
                "highlight": {
                    "content": [
                        "中韓漁警沖突調(diào)查:韓警平均每天扣1艘<tag1>中國</tag1>漁船"
                    ]
                }
            }
        ]
    }
}

至此,我們的ik分詞器就可以告一段落了,剩下就是繼續(xù)探索!??!

熱詞更新配置:

待研究
參考文檔:Elasticsearch 中文分詞器 IK 配置和使用

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容