安裝和使用分詞工具IK Analysis
安裝分詞工具IK Analysis
開啟命令列,轉到安裝elasticsearch路徑:
隨後 輸入命令:
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.4/elasticsearch-analysis-ik-6.2.4.zip
注意這裡的6.2.4是我的elasticsearch版本,你只需要改成你安裝的elasticsearch的版本就可以
使用 IK:
1.在視覺化介面kibana輸入:PUT /index 建立索引index
2.建立mapping:
POST /index/fulltext/_mapping
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}
3.輸入例子
curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d' {"content":"美國留給伊拉克的是個爛攤子嗎"} '
curl -XPOST http://localhost:9200/index/fulltext/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校車將享最高路權"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d'
{"content":"中韓漁警衝突調查:韓警平均每天扣1艘中國漁船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -H 'Content-Type:application/json' -d' {"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"} '
4.輸入查詢:
curl -XPOST http://localhost:9200/index/fulltext/_search -H 'Content-Type:application/json' -d'
{
"query" : { "match" : { "content" : "中國" }},
"highlight" : {
"pre_tags" : ["<tag1>", "<tag2>"],
"post_tags" : ["</tag1>", "</tag2>"],
"fields" : {
"content" : {}
}
}
}
'
執行結果:
在你資料已經插入的情況下是沒有辦法改變es自己建立的預設對映mapping,只能在插入資料前寫好mapping或者在預設設定裡關閉es的自動建立對映。否則,會像我一樣報錯:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Mapper for [summary] conflicts with existing mapping in other types:\n[mapper [summary] has different [analyzer]]"
}
],
"type": "illegal_argument_exception",
"reason": "Mapper for [summary] conflicts with existing mapping in other types:\n[mapper [summary] has different [analyzer]]"
},
"status": 400
}
解決方法:我決定先設定mapping做中文分詞 ,再把資料重新匯入一遍