建议先安装kibana 参考 kibana-6-8-4 win 安装
一、安装
elasticsaerch目录下的plugins新建ik目录
下载的ik解压至该目录
二、验证
以下操作均在kibana的“Dev Tools”
1、创建索引(相当于创建数据库及表结构)
索引名:article
字段:title,content
PUT article { "mappings": { "_doc": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" }, "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } } } }
2、插入数据
POST /article/_doc/?pretty { "title": "Elasticsearch 6.x Mapping设置", "content":" Mapping,映射,相当于关系型数据库创建语句,定义文档字段及其类型、索引与存储方式" }
3、查询刚刚插入的数据
GET /article/_search?q=*
4、IK查询并高亮
GET /article/_search { "query" : { "match" : { "content" : "数据库" }}, "highlight" : { "pre_tags" : ["<em>"], "post_tags" : ["</em>"], "fields" : { "content" : {} } } }
三、ik中ik_max_word和ik_smart区别
ik_max_word:最细粒度拆分,ik_smart:最粗粒度
POST /_analyze { "text":"中华人民共和国人民大会堂", "analyzer":"ik_max_word" } 返回 { "tokens" : [ { "token" : "中华人民共和国", "start_offset" : 0, "end_offset" : 7, "type" : "CN_WORD", "position" : 0 }, { "token" : "中华人民", "start_offset" : 0, "end_offset" : 4, "type" : "CN_WORD", "position" : 1 }, { "token" : "中华", "start_offset" : 0, "end_offset" : 2, "type" : "CN_WORD", "position" : 2 }, { "token" : "华人", "start_offset" : 1, "end_offset" : 3, "type" : "CN_WORD", "position" : 3 }, { "token" : "人民共和国", "start_offset" : 2, "end_offset" : 7, "type" : "CN_WORD", "position" : 4 }, { "token" : "人民", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 5 }, { "token" : "共和国", "start_offset" : 4, "end_offset" : 7, "type" : "CN_WORD", "position" : 6 }, { "token" : "共和", "start_offset" : 4, "end_offset" : 6, "type" : "CN_WORD", "position" : 7 }, { "token" : "国人", "start_offset" : 6, "end_offset" : 8, "type" : "CN_WORD", "position" : 8 }, { "token" : "国", "start_offset" : 6, "end_offset" : 7, "type" : "CN_CHAR", "position" : 9 }, { "token" : "人民大会堂", "start_offset" : 7, "end_offset" : 12, "type" : "CN_WORD", "position" : 10 }, { "token" : "人民大会", "start_offset" : 7, "end_offset" : 11, "type" : "CN_WORD", "position" : 11 }, { "token" : "人民", "start_offset" : 7, "end_offset" : 9, "type" : "CN_WORD", "position" : 12 }, { "token" : "大会堂", "start_offset" : 9, "end_offset" : 12, "type" : "CN_WORD", "position" : 13 }, { "token" : "大会", "start_offset" : 9, "end_offset" : 11, "type" : "CN_WORD", "position" : 14 }, { "token" : "会堂", "start_offset" : 10, "end_offset" : 12, "type" : "CN_WORD", "position" : 15 } ] }
POST /_analyze { "text":"中华人民共和国人民大会堂", "analyzer":"ik_smart" } 返回 { "tokens" : [ { "token" : "中华人民共和国", "start_offset" : 0, "end_offset" : 7, "type" : "CN_WORD", "position" : 0 }, { "token" : "人民大会堂", "start_offset" : 7, "end_offset" : 12, "type" : "CN_WORD", "position" : 1 } ] }