elasticsearch第三步-中文分词

天王星天 天王星天     2022-09-09     275

关键词:

elasticsearch官方只提供smartcn这个中文分词插件,效果不是很好,好在国内有medcl大神(国内最早研究es的人之一)写的两个中文分词插件,一个是ik的,一个是mmseg的,下面分别介绍ik的用法,

当我们创建一个index(库db_news)时,easticsearch默认提供的分词器db_news,分词结果会把每个汉字分开,而不是我们想要的根据关键词来分词。例如:

代码如下:

GET /db_news/_analyze?analyzer=standard
{
    我爱北京天安门
}

分词结果如下:

复制代码
{
   "tokens": [
      {
         "token": "我",
         "start_offset": 6,
         "end_offset": 7,
         "type": "<IDEOGRAPHIC>",
         "position": 1
      },
      {
         "token": "爱",
         "start_offset": 7,
         "end_offset": 8,
         "type": "<IDEOGRAPHIC>",
         "position": 2
      },
      {
         "token": "北",
         "start_offset": 8,
         "end_offset": 9,
         "type": "<IDEOGRAPHIC>",
         "position": 3
      },
      {
         "token": "京",
         "start_offset": 9,
         "end_offset": 10,
         "type": "<IDEOGRAPHIC>",
         "position": 4
      },
      {
         "token": "天",
         "start_offset": 10,
         "end_offset": 11,
         "type": "<IDEOGRAPHIC>",
         "position": 5
      },
      {
         "token": "安",
         "start_offset": 11,
         "end_offset": 12,
         "type": "<IDEOGRAPHIC>",
         "position": 6
      },
      {
         "token": "门",
         "start_offset": 12,
         "end_offset": 13,
         "type": "<IDEOGRAPHIC>",
         "position": 7
      }
   ]
}
复制代码

 

正常情况下,这不是我们想要的结果,比如我们更希望 “我”,“爱”,“北京”,"天安门"这样的分词,这样我们就需要安装中文分词插件,ik就是实现这个功能的。

 安装ik插件

 

第一种方式是直接下载配置,这种方式比较麻烦(对于Windows用户来讲),这里我也不讲了

下载地址:https://github.com/medcl/elasticsearch-analysis-ik

 ********************************************************************************************

第二种方式是直接下载elasticsearch中文发行版。下载地址是:https://github.com/medcl/elasticsearch-rtf。重新运行安装。

执行命令:

GET /db_news/_analyze?analyzer=ik
{
    我爱北京天安门啊王军华
    
}

结果如下:

复制代码
{
   "tokens": [
      {
         "token": "我",
         "start_offset": 6,
         "end_offset": 7,
         "type": "CN_CHAR",
         "position": 1
      },
      {
         "token": "爱",
         "start_offset": 7,
         "end_offset": 8,
         "type": "CN_CHAR",
         "position": 2
      },
      {
         "token": "北京",
         "start_offset": 8,
         "end_offset": 10,
         "type": "CN_WORD",
         "position": 3
      },
      {
         "token": "天安门",
         "start_offset": 10,
         "end_offset": 13,
         "type": "CN_WORD",
         "position": 4
      },
      {
         "token": "啊",
         "start_offset": 13,
         "end_offset": 14,
         "type": "CN_CHAR",
         "position": 5
      },
      {
         "token": "王军",
         "start_offset": 14,
         "end_offset": 16,
         "type": "CN_WORD",
         "position": 6
      },
      {
         "token": "华",
         "start_offset": 16,
         "end_offset": 17,
         "type": "CN_CHAR",
         "position": 7
      }
   ]
}
复制代码

 

关于分词器定义需要注意的地方

如果我们直接创建索引库,会使用默认的分词进行分词,这不是我们想要的结果。这个时候我们再去更改分词器会报错如下:

{
   "error": "IndexAlreadyExistsException[[db_news] already exists]",
   "status": 400
}

而且没有办法解决冲突,唯一的办法是删除已经存在的索引,新建一个索引,并制定mapping使用新的分词器(注意要在数据插入之前,否则会使用elasticsearch默认的分词器)。

 新建索引命令如下:

复制代码
PUT /db_news
{
    
     "settings" : {
        "analysis" : {
            "analyzer" : {
                "stem" : {
                    "tokenizer" : "standard",
                    "filter" : ["standard", "lowercase", "stop", "porter_stem"]
                }
            }
        }
    },
    "mappings" : {
        "person" : {
            "dynamic" : true,
            "properties" : {
                "intro" : {
                    "type" : "string",
"indexAnalyzer" : "ik",
"searchAnalyzer":"ik"
                }
            }
        }
    }
}
复制代码

 

查看新建的索引:

GET /db_news/_mapping

结果如下:

复制代码
{
   "db_news": {
      "mappings": {
         "person": {
            "dynamic": "true",
            "properties": {
               "age": {
                  "type": "long"
               },
               "intro": {
                  "type": "string",
                  "analyzer": "ik"
               },
               "name": {
                  "type": "string"
               }
            }
         }
      }
   }
}
复制代码

 

更新映射

说明:对于db_news/news,开始没有字段msgs,后来添加了这个字段,那么要先修改索引方式,在新增数据

复制代码
PUT /db_news/_mapping/news
{
            "properties" : {
                "msgs" : {
                    "type" : "string",
                    "indexAnalyzer" : "ik",
                    "searchAnalyzer":"ik"
                }    
    }
}
复制代码

 

 

 

ElasticSearch系列学习

ElasticSearch第一步-环境配置

ElasticSearch第二步-CRUD之Sense 

ElasticSearch第三步-中文分词

ElasticSearch第四步-查询详解

ElasticSearch第五步-.net平台下c#操作ElasticSearch详解

 

elasticsearch中文分词器对比

参考技术A对以上分词器进行了一个粗略对比:截止到目前为止,他们的分词准确性从高到低依次是:结合准确性来看,选用中文分词器基于以下考虑:截止目前,IK分词器插件的优势是支持自定义热更新远程词典。IK分词器的github... 查看详情

elasticsearch之中文分词器插件es-ik

 前提什么是倒排索引?Elasticsearch之分词器的作用Elasticsearch之分词器的工作流程Elasticsearch之停用词Elasticsearch之中文分词器Elasticsearch之几个重要的分词器       elasticsearch官方默认的分词插件  1、el... 查看详情

elasticsearch中文分词(代码片段)

...hobby","text":"听音乐"     中文分词:  IK分词器Elasticsearch插件地址:https://github.com/medcl/elasticsearch-analysis-ik  安装方法:将下载到的elasticsearch-analysis-ik-6.5.4.zip解压到elasticsearch/plugins/ik目录下即可。    unzipelastics... 查看详情

elasticsearch安装中文分词器(代码片段)

发车   为什么要在elasticsearch中要使用ik这样的中文分词呢,那是因为es提供的分词是英文分词,对于中文的分词就做的非常不好了,因此我们需要一个中文分词器来用于搜索和使用。今天我们就尝试安装下IK分词。上... 查看详情

为elasticsearch添加中文分词,对比分词器效果

http://keenwon.com/1404.htmlElasticsearch中,内置了很多分词器(analyzers),例如standard (标准分词器)、english(英文分词)和chinese (中文分词)。其中standard 就是无脑的一个一个词(汉字)切分,所以适用范围广,但是精... 查看详情

elasticsearch实战(二十六)-ik中文分词器

        为什么要在elasticsearch中要使用ik这样的中文分词?因为ES提供的分词是英文分词,对于中文的分词就做的非常不好了,因此我们需要一个中文分词器来用于搜索和使用。一、安装   &... 查看详情

elasticsearch安装中文分词器插件smartcn

原文:http://blog.java1234.com/blog/articles/373.htmlelasticsearch安装中文分词器插件smartcn elasticsearch默认分词器比较坑,中文的话,直接分词成单个汉字。我们这里来介绍下smartcn插件,这个是官方推荐的,中科院搞的,基本能满足需求... 查看详情

elasticsearch中文分词(ik)

ElasticSearch安装官网:https://www.elastic.co1.ElasticSearch安装1.1.下载安装公共密钥rpm--importhttps://artifacts.elastic.co/GPG-KEY-elasticsearch1.2.在 /etc/yum.repos.d/目录下建立 elasticsearch.repo文件vim/etc/yum 查看详情

dockerfile构建elasticsearch镜像安装ik中文分词器插件(代码片段)

DockerFile构建ElasticSearch镜像安装IK中文分词器插件为什么要安装IK中文分词器?ES提供的分词是英文分词,对中文做分词时会拆成单字而不是词语,非常不好,因此索引信息含中文时需要使用中文分词器插件。一、环境及文件准备... 查看详情

elasticsearch中文分词器比对表

1.概述分词器分词粒度出错情况支持处理字符新词识别词性标注认证方法接口BosonNLP多选择无识别繁体字有有TokenRESTfulIKAnalyzer多选择无兼容韩文日文有无无JarNLPIR多选择中文间隔符未知有有无多语言接口SCWS多选择无未知有有无PHP... 查看详情

elasticsearch中文分词+全文搜索

...接下载打包好的多方便?下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases最上面elasticsearch-analysis-ik- 查看详情

elasticsearch安装中文分词插件ik

Elasticsearch默认提供的分词器,会把每一个汉字分开,而不是我们想要的依据关键词来分词。比如:curl-XPOST"http://localhost:9200/userinfo/_analyze?analyzer=standard&pretty=true&text=我是中国人"我们会得到这种结果:{tokens:[{token:textstart_offs... 查看详情

elasticsearch:java操作elasticsearch基于smartcn中文分词查询

1packagecom.gxy.ESChap01;23importjava.net.InetAddress;45importorg.elasticsearch.action.search.SearchRequestBuilder;6importorg.elasticsearch.action.search.SearchResponse;7importorg.elasticsearch.client 查看详情

如何在elasticsearch中安装中文分词器(ik)和拼音分词器?

  声明:我使用的Elasticsearch的版本是5.4.0,安装分词器前请先安装maven一:安装mavenhttps://github.com/apache/maven说明:安装maven需要java1.7+编译安装分词器时,可能会报错,报错信息如下:[ERROR]COMPILATIONERROR:[INFO]---------------------------... 查看详情

elasticsearch实战(二十六)-ik中文分词器(代码片段)

        为什么要在elasticsearch中要使用ik这样的中文分词?因为ES提供的分词是英文分词,对于中文的分词就做的非常不好了,因此我们需要一个中文分词器来用于搜索和使用。一、安装        我们可以从 官... 查看详情

elasticsearch安装elasticsearch-analysis-ik中文分词器(代码片段)

1,讲分词器的文件夹放入es安装包的plugins,重新启动elasticsearch//查询es运行中的进程pidps-aux|grepelasticsearch//杀死进程kill-9pid//使用es账户启动nohup./elasticsearch&2,重启es,然后在新建index的type表结构时,需要指定将来可能要使用... 查看详情

elasticsearch安装elasticsearch-analysis-ik中文分词器(代码片段)

1,讲分词器的文件夹放入es安装包的plugins,重新启动elasticsearch//查询es运行中的进程pidps-aux|grepelasticsearch//杀死进程kill-9pid//使用es账户启动nohup./elasticsearch&2,重启es,然后在新建index的type表结构时,需要指定将来可能要使用... 查看详情

elasticsearch速学-ik中文分词器远程字典设置

...词的,我们可以丰富这个词库。 IK分词器(IKAnalysisforElasticsearch)给了我们一个基本的配置: https://github.com/medcl/elasti 查看详情