elasticsearch7.6.2+ik分词器,比如现在有一句话 ”这是一个视频“,我想搜”频视“、”这个“等,也能够有结果,并且出现的字都高亮,应该怎么实现呢
ik 分词器是默认支持远程词典和热跟新的
我们打开 分词器的配置文件,可以看到以下内容
配置文件路径:plugins/ik/config/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典 -->
<entry key="ext_dict"></entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords"></entry>
<!--用户可以在这里配置远程扩展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用户可以在这里配置远程扩展停止词字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
从配置文件可以看出,前两项分别配置本地静态的包含词典和排除词典,但是修改这两项需要重启 es,不是很方便,不推荐使用
后两项很明显就是我们需要远程词库配置
确保已经正确安装和配置了 Elasticsearch 7.6.2 和 IK 分词器。
创建一个索引,指定字段的映射类型为 text,并设置合适的分词器。示例请求如下:
PUT your_index
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}
}
插入文档到索引中,该文档包含要搜索的内容。示例请求如下:
PUT your_index/_doc/1
{
"content": "这是一个视频"
}
执行查询时,使用全文搜索查询字符串查询并指定高亮字段。示例请求如下:
POST your_index/_search
{
"query": {
"match": {
"content": "频视 或者 这个"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}
"这是一个视频",如果'频视',想搜出来的话,分词可能要按字来分才行,可以用standard这个分词器,但是这样召回会有很大概率不相关
PUT your_index
{
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "standard"
}
}
}
}
PUT your_index/_doc/1
{
"content": "这是一个视频"
}
POST your_index/_search
{
"query": {
"match": {
"content": "频视 OR 这个"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}