Elasticsearch Analyzer 内置分词器

篇主要介绍一下 Elasticsearch中 Analyzer 分词器的构成 和一些Es中内置的分词器 以及如何使用它们

前置知识

es 提供了 analyze api 可以方便我们快速的指定 某个分词器 然后对输入的text文本进行分词 帮助我们学习和实验分词器

POST _analyze
{
"analyzer": "standard",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} [ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog's, bone ]

1.Analyzer

在ES中有很重要的一个概念就是 分词,ES的全文检索也是基于分词结合倒排索引做的。所以这一文我们来看下何谓之分词。如何分词。

分词器是专门处理分词的组件,在很多中间件设计中每个组件的职责都划分的很清楚,单一职责原则,以后改的时候好扩展。

分词器由三部分组成。

  • Character Filters : 主要对原文本做处理, 例如 去除 html 标签
  • Tokenizer : 按照规则 把文本切分为单词, 也就是分词
  • Token Filters : 将切分后的单词 进行加工处理, 小写,删除stopwords 停顿词, 增加同义词 , 扩展一些

分词场景:

  1. 数据写入index 的时候进行分词
  2. query 查询时候 需要对查询文本 进行分词

2.Elasticsearch 内置分词器

在es中有不少内置分词器

  • Standard Analyzer : 默认分词器, 按Unicode文本分割算法拆分 , 转化为小写 , 支持中文(但是中文按照每个文字拆分,没啥意义)
  • Simple Analyzer : 按照非字母切分 并且转化为小写
  • Stop Analyzer : 和 simple 一样 但是多了 过滤停用词(the a is) 默认使用 stop token filter 的 _ _ english _ _ 预定义
  • Whitespace Analyzer : 每当遇到 空格的时候 会进行分词 , 不会转小写
  • Keyword Analyzer : 不分词 直接将输入当做输出
  • Patter Analyzer : 正则表达式
  • Language : 语言分词器 30多种
  • Customer Analyzer : 自定义分词器

3. Standard Analyzer

Standard 是es中默认的分词器 , 它是按照 Unicode 文本分割算法去 对文本进行分词的

POST _analyze
{
"analyzer": "standard",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} [ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog's, bone ]

3.1 Definition

包括了 转小写的 token filter 和 stop token filter 去除停顿词

Tokenizer

  • [Standard Tokenizer]

Token Filters

  • [Standard Token Filter] : 没用只是作为保留的token filter (The standard token filter currently does nothing. It remains as a placeholder in case some filtering function needs to be added in a future version.)
  • [Lower Case Token Filter] : 转小写的 token filter
  • [Stop Token Filter] : 停顿词 token filter 默认是没有开启

3.2 Configuration

  • max_token_length : 最大的分词长度,如果超过此长度 则直接分词 default 255
  • stopwords : 预定义的停顿词列表 如: _ _ englisth _ _ 或者 停顿词数组[] 默认 none 不设置
  • stopwords_path : 包含停顿词的文件路径

3.3 实验

// 使用 自定义的分词器 基于 standard
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_english_analyzer": {
"type": "standard",
"max_token_length": 5, // 最大词数
"stopwords": "_english_" // 开启过滤停顿词 使用 englisth 语法
}
}
}
}
} GET my_index/_analyze
{
"analyzer": "my_english_analyzer",
"text": "The hellogoodname jack"
}
// 可以看到 最长5个字符 就需要进行分词了, 并且停顿词 the 没有了
["hello", "goodn", "ame", "jack"]

4. Simple Analyzer

简单的分词器 分词规则就是 遇到 非字母的 就分词, 并且转化为小写,(lowercase tokennizer )

POST _analyze
{
"analyzer": "simple",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} [ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

4.1 Definition

Tokenizer

  • Lower Case Tokenizer

4.2 Configuation

无配置参数

4.3 实验

simple analyzer 分词器的实现 就是如下

PUT /simple_example
{
"settings": {
"analysis": {
"analyzer": {
"rebuilt_simple": {
"tokenizer": "lowercase",
"filter": [
]
}
}
}
}
}

5. Stop Analyzer

stop analyzer 和 simple analyzer 一样, 只是多了 过滤 stop word 的 token filter , 并且默认使用 english 停顿词规则

POST _analyze
{
"analyzer": "stop",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
// 可以看到 非字母进行分词 并且转小写 然后 去除了停顿词
[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]

5.1 Definition

Tokenizer

  • Lower Case Tokenizer : 转小写的

Token filters

  • Stop Token Filter : 过滤停顿词 默认使用 规则 english

5.2 Configuration

  • stopwords : 指定分词的规则 默认 english , 或者分词的数组
  • stopwords_path : 指定分词停顿词文件

5.3 实验

如下就是对 Stop Analyzer 的实现 , 先转小写 后进行停顿词的过滤

PUT /stop_example
{
"settings": {
"analysis": {
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_"
}
},
"analyzer": {
"rebuilt_stop": {
"tokenizer": "lowercase",
"filter": [
"english_stop"
]
}
}
}
}
}

设置 stopwords 参数 指定过滤的停顿词列表

PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_stop_analyzer": {
"type": "stop",
"stopwords": ["the", "over"]
}
}
}
}
} POST my_index/_analyze
{
"analyzer": "my_stop_analyzer",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} [ quick, brown, foxes, jumped, lazy, dog, s, bone ]

6. Whitespace Analyzer

空格 分词器, 顾名思义 遇到空格就进行分词, 不会转小写

POST _analyze
{
"analyzer": "whitespace",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} [ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]

6.1 Definition

Tokenizer

  • Whitespace Tokenizer

6.2 Configuration

无配置

6.3 实验

whitespace analyzer 的实现就是如下, 可以根据实际情况进行 添加 filter

PUT /whitespace_example
{
"settings": {
"analysis": {
"analyzer": {
"rebuilt_whitespace": {
"tokenizer": "whitespace",
"filter": [
]
}
}
}
}
}

7. Keyword Analyzer

很特殊 它不会进行分词, 怎么输入 就怎么输出

POST _analyze
{
"analyzer": "keyword",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
} //注意 这里并没有进行分词 而是原样输出
[ The 2 QUICK Brown-Foxes jumped over the lazy dog's bone. ]

7.1 Definition

Tokennizer

  • Keyword Tokenizer

7.2 Configuration

无配置

7.3 实验

rebuit 如下 就是 Keyword Analyzer 实现

PUT /keyword_example
{
"settings": {
"analysis": {
"analyzer": {
"rebuilt_keyword": {
"tokenizer": "keyword",
"filter": [
]
}
}
}
}
}

8. Patter Analyzer

正则表达式 进行拆分 ,注意 正则匹配的是 标记, 就是要被分词的标记 默认是 按照 \w+ 正则分词

POST _analyze
{
"analyzer": "pattern",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
// 默认是 按照 \w+ 正则
[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

8.1 Definition

Tokennizer

  • Pattern Tokenizer

Token Filters

  • Lower Case Token Filter
  • Stop Token Filter (默认未开启)

8.2 Configuration

pattern A Java regular expression, defaults to \W+.
flags Java regular expression.
lowercase 转小写 默认开启 true.
stopwords 停顿词过滤 默认none 未开启 , Defaults to _none_.
stopwords_path 停顿词文件路径

8.3 实验

Pattern Analyzer 的实现 就是如下

PUT /pattern_example
{
"settings": {
"analysis": {
"tokenizer": {
"split_on_non_word": {
"type": "pattern",
"pattern": "\\W+"
}
},
"analyzer": {
"rebuilt_pattern": {
"tokenizer": "split_on_non_word",
"filter": [
"lowercase"
]
}
}
}
}
}

9. Language Analyzer

提供了如下 这么多语言分词器 , 其中 english 也在其中

arabic, armenian, basque, bengali, bulgarian, catalan, czech, dutch, english, finnish, french, galician, german, hindi, hungarian, indonesian, irish, italian, latvian, lithuanian, norwegian, portuguese, romanian, russian, sorani, spanish, swedish, turkish.

GET _analyze
{
"analyzer": "english",
"text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
}
[ 2, quick, brown, foxes, jumped, over, lazy, dog, bone ]

10. Customer Analyzer

没啥好说的 就是当提供的 内置分词器不满足你的需求的时候 ,你可以结合 如下3部分

  • Character Filters : 主要对原文本做处理, 例如 去除 html 标签
  • Tokenizer : 按照规则 把文本切分为单词, 也就是分词
  • Token Filters : 将切分后的单词 进行加工处理, 小写,删除stopwords 停顿词, 增加同义词 , 扩展一些
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"char_filter": [
"emoticons"
],
"tokenizer": "punctuation",
"filter": [
"lowercase",
"english_stop"
]
}
},
"tokenizer": {
"punctuation": {
"type": "pattern",
"pattern": "[ .,!?]"
}
},
"char_filter": {
"emoticons": {
"type": "mapping",
"mappings": [
":) => _happy_",
":( => _sad_"
]
}
},
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_"
}
}
}
}
} POST my_index/_analyze
{
"analyzer": "my_custom_analyzer",
"text": "I'm a :) person, and you?"
} [ i'm, _happy_, person, you ]

总结

本篇主要介绍了 Elasticsearch 中 的一些 内置的 Analyzer 分词器, 这些内置分词器可能不会常用,但是如果你能好好梳理一下这些内置 分词器,一定会对你理解Analyzer 有很大的帮助, 可以帮助你理解 Character Filters , Tokenizer 和 Token Filters 的用处.

有机会再聊聊 一些中文分词器 如 IKAnalyzer, ICU Analyzer ,Thulac 等等.. 毕竟开发中 中文分词器用到更多些

欢迎大家访问 个人博客 Johnny小屋

欢迎关注个人公众号

Elasticsearch Analyzer 内置分词器的更多相关文章

  1. Elasticsearch(10) --- 内置分词器、中文分词器

    Elasticsearch(10) --- 内置分词器.中文分词器 这篇博客主要讲:分词器概念.ES内置分词器.ES中文分词器. 一.分词器概念 1.Analysis 和 Analyzer Analy ...

  2. Elasticsearch(ES)分词器的那些事儿

    1. 概述 分词器是Elasticsearch中很重要的一个组件,用来将一段文本分析成一个一个的词,Elasticsearch再根据这些词去做倒排索引. 今天我们就来聊聊分词器的相关知识. 2. 内置 ...

  3. elasticsearch使用ik中文分词器

    elasticsearch使用ik中文分词器 一.背景 二.安装 ik 分词器 1.从 github 上找到和本次 es 版本匹配上的 分词器 2.使用 es 自带的插件管理 elasticsearc ...

  4. yii框架中验证器声明一组内置验证器可以使用短名称引用

    1.内置验证器的短名称分别有: boolean: yii\validators\BooleanValidator captcha: yii\captcha\CaptchaValidator compa ...

  5. Unity3D内置着色器

    Unity内部提供了一些可以直接使用的着色器,这些内置着色器包括以下6个方面: (1)Performance of Unity shaders 着色器的性能和两个方面有关:shader本身和rende ...

  6. Flex 内置验证器—验证用户输入

    今晚对于Flex中的Validator类(所有验证器的父类)测试一下 ---->其中常用的验证类有StringValidator,NumberValidator,DateValidator 测试 ...

  7. Elasticsearch拼音和ik分词器的结合应用

    一.创建索引时,自定义拼音分词和ik分词 PUT /my_index { "index": { "analysis": { "analyzer&quo ...

  8. python的内置下载器

    python有个内置下载器,有时候在内部提供文件下载很好用. 进入提供下载的目录 # ls abc.aaa chpw.py finance.py lsdir.py ping.py u2d-partia ...

  9. Struts2内置校验器——完整实例代码

    一.校验器的配置风格 1.字段校验器: <field name="被校验的字段"> <field-validator type="校验器名"& ...

随机推荐

  1. virsh edit 很慢 的bug

    创建虚拟机,发现virsh edit很慢. strace的结果: 09:26:03 close(10) = -1 EBADF (Bad file descriptor)09:26:03 close(1 ...

  2. Prometheus+Grafana监控-基于docker-compose搭建

    前言 Prometheus Prometheus 是有 SoundCloud 开发的开源监控系统和时序数据库,基于 Go 语言开发.通过基于 HTTP 的 pull 方式采集时序数据,通过服务发现或静 ...

  3. [CF1498D] Bananas in a Microwave (DP)

    题面&翻译 题解 虽然 m m m 很大,但是 n n n 很小,因此题目允许我们在 O ( n m ) O(nm) O(nm) 以内解决这道题. 定义一个 dp[i][j]=0/1 ? 如果 ...

  4. 食之无味?App Startup 可能比你想象中要简单

    请点赞关注,你的支持对我意义重大. Hi,我是小彭.本文已收录到 GitHub · AndroidFamily 中.这里有 Android 进阶成长知识体系,有志同道合的朋友,关注公众号 [彭旭锐] ...

  5. linux使用iptables屏蔽ip地址

    一.iptables命令介绍: netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,在安装系统的时 ...

  6. [apue] 标准 I/O 库那些事儿

    前言 标准 IO 库自 1975 年诞生以来,至今接近 50 年了,令人惊讶的是,这期间只对它做了非常小的修改.除了耳熟能详的 printf/scanf,回过头来对它做个全方位的审视,看看到底优秀在哪 ...

  7. Java根据Freemarker模板生成Word文件

    1.  准备模板 模板 + 数据 = 模型 1.将准备好的Word模板文件另存为.xml文件(PS:建议使用WPS来创建Word文件,不建议用Office) 2.将.xml文件重命名为.ftl文件 3 ...

  8. 单台主机MySQL多实例部署

    二进制安装mysql-5.7.26 [root@mysql ~]# cd /server/tools/ [root@mysql tools]# ll total 629756 -rw-r--r-- 1 ...

  9. C/C++ 关于默认构造函数

    前言: 在C++中,对于一个类,C++的编译器都会为这个类提供四个默认函数,分别是: A() //默认构造函数 ~A() //默认析构函数 A(const A&) //默认拷贝构造函数 A&a ...

  10. nsis插件nsisSlideshow.dll更新

    更新至1.7版本,作者wiz0u已解决关于ie9的兼容问题.Good 下载地址: http://wiz0u.free.fr/prog/nsisSlideshow/latest.php