要做到中文全文检索还需要按照中文分词库 ,这里就使用 IK来设置
安装中文分词库
相关命令: whereis elasticsearch 找到目录
进入 到/usr/elasticsearch/bin
执行 ./elasticsearch-plugin插件命令 安装插件 ./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.6.1/elasticsearch-analysis-ik-6.6.1.zip service elasticsearch restart 重启服务 安装好之后可以看到 plugin 下有 analysis-ik 接下来按照官方的步骤往下面走 、创建一个索引 curl -XPUT http://localhost:9200/myfulltext 、创建 curl -XPOST http://localhost:9200/liyouming/liyoumingtext/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"mytext": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
}
} }'
这里要注意 content 不能冲突 ,分析器字段要定义
Mapper for [content] conflicts with existing mapping in other types:\n[mapper [content] has different [analyzer]]
Mapper for[content]与其他类型的现有映射冲突:[mapper[content]有不同的[分析器]
这里我们还是通过WebAPI来测试
首先创建我们的索引
OK后创建 全文检索相关设置 设置字段、分析器配置 ik_smart 、ik_max_word
分别添加如下数据
{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"深夜还在写代码的人只有黎又铭"}
}
{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"中午黎又铭在操场上打篮球"}
}
{
"url":"liyouming/liyoumingtext",
"param":{"mytext":"黎又铭早上吃了一碗面"}
}
查询下所有数据可以看到
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 3.0561461,
"hits": [
{
"_index": "liyouming",
"_type": "liyoumingtext",
"_id": "c7eQAWoB0Mh7sqcTGY-K",
"_score": 3.0561461,
"_source": {
"mytext": "中午黎又铭在操场上打篮球"
}
},
{
"_index": "liyouming",
"_type": "liyoumingtext",
"_id": "dLeQAWoB0Mh7sqcTdo9b",
"_score": 2.1251993,
"_source": {
"mytext": "深夜还在写代码的人只有黎又铭"
}
},
{
"_index": "liyouming",
"_type": "liyoumingtext",
"_id": "crePAWoB0Mh7sqcTzY-2",
"_score": 0.8630463,
"_source": {
"mytext": "黎又铭早上吃了一碗面"
}
}
]
}
}
检索下篮球并高亮文本内容可以看到下面的结果 <tag1>篮球</tag1> 已经被高亮标签处理
{
"took": ,
"timed_out": false,
"_shards": {
"total": ,
"successful": ,
"skipped": ,
"failed":
},
"hits": {
"total": ,
"max_score": 1.0187154,
"hits": [
{
"_index": "liyouming",
"_type": "liyoumingtext",
"_id": "c7eQAWoB0Mh7sqcTGY-K",
"_score": 1.0187154,
"_source": {
"mytext": "中午黎又铭在操场上打篮球"
},
"highlight": {
"mytext": [
"中午黎又铭在操场上打<tag1>篮球</tag1>"
]
}
}
]
}
}