我这里直接下载解压包,然后安装,参考官方文档完成,分词器有很多种,大家可以自行官网了解
./bin/elasticsearch-plugin install /opt/elasticsearch-analysis-ik-6.3.0.zip
直接在命令行里搞定也很容易
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.3.0/elasticsearch-analysis-ik-6.3.0.zip
1.create a index
curl -XPUT http://localhost:9200/index
2.create a mapping 数据结构必须先指定分析器
curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
}
}'
3.index some docs 插入数据,可以多插几条类似的文档,
curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
4.query with highlighting 查询分词效果如何
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" : {}
}
}
}
可以结合自己的业务去分析,查询数据还是很精确,基本满足日常使用