服务热线
0220-409204972
前言 好比,当我们想将某个博客标志为“大神”时,博客系统却将这个单词粗暴的分成了如图所示的两个词“大”和“神”。显然,这并不切合用户的使用习惯。
这是 Elasticsearch 语言分析器上的限制,它并不能友好的处置惩罚所有语言,特别是中文。这种情况下,我们就需要分外的中文分词器来协助我们了。分词(Analysis) 将文本切分为一系列单词的历程,好比 "美国留给伊拉克的是个烂摊子吗?"经由分词后的结果为:美国、伊拉克、烂摊子。
分词器(Analyzer) elasticsearch中执行的分词的主体,官方把分词器分成三个条理:Character Filters:针对文档的原始文本举行处置惩罚,例如将印度语的阿拉伯数字"0 12345678 9"转换成拉丁语的阿拉伯数字"0123456789",或者去除HTML中的特殊标志符号,Character Filters可以有零或多个,安装顺序应用;PS:类似Servlet中的过滤器,或者拦截器,想象一下有一个过滤器链Tokenizer:焦点,将文档的原始文本根据一定规则切分为单词,Tokenizer只能有一个;PS:Tokenizer 卖力将文本拆分成单个token ,这里token就指的就是一个一个的单词。就是一段文本被支解成好几部门,相当于Java中的字符串的 splitToken Filter:对经由Tokenizer处置惩罚事后的单词举行二次加工,好比转换为小写,Token Filter也可以有多个,按顺序依次挪用。
token过滤器吸收token流,而且可能会添加、删除或更改tokens。不允许token过滤器更改每个token的位置或字符偏移量。一个分析器可能有0个或多个token过滤器,它们按顺序应用。
三者的挪用顺序:Character Filters--->Tokenizer--->Token Filter小结&回首analyzer(分析器)是一个包,这个包由三部门组成,划分是:character filters (字符过滤器)、tokenizer(分词器)、token filters(token过滤器)一个analyzer可以有0个或多个character filters一个analyzer有且只能有一个tokenizer一个analyzer可以有0个或多个token filterscharacter filter 是做字符转换的,它吸收的是文本字符流,输出也是字符流tokenizer 是做分词的,它吸收字符流,输出token流(文本拆分后酿成一个一个单词,这些单词叫token)token filter 是做token过滤的,它吸收token流,输出也是token流由此可见,整个analyzer要做的事情就是将文本拆分成单个单词,文本 ----> 字符 ----> token Analyze API es提供了endpoint为_analyze的语句来测试分词效果,你可以指定索引中的字段或者显式输入文原来测试分词效果 预界说的分词器es自带的分词器如下,默认是standard,建立索引的mapping(类似于表结构)时候可指定 因为文档中的每个字段都市建设倒排索引,所以你也可以在建立索引的mapping时指定每个字段的分词器。下面简朴的测试一下Standard、Simple、whitespace这三个分词器分词效果,其余的就不测试了。standardStandardSimplewhitespace 其余的分词器留给大家自己去测试,分词器的选择还是很重要的,根据你想要的切分方式切分文本获得的分词效果,既可以节约空间,又可以较好的解决搜索问题。
尤其是中文,如何切分是个难点,好比文本"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"如果经由默认的分词器standard analyzer切分的话,会获得"中、国、驻、洛、杉、矶、领、事、馆、遭、亚、裔、男、子、枪、击、嫌、犯、已、自、首",这显然不是我们想要的分词效果;再好比,"乒乓球拍卖完了",是切分为"乒乓球/拍卖/完了"还是切分为"乒乓球拍/卖完了"。这里分享一个常用的中文分词器:ik_smart,它能较好的切分中文及英文文本,支持自界说词库,开源分词器 ik 的github:https://github.com/medcl/elasticsearch-analysis-ik安装iksmart分词器如下:./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.4.0/elasticsearch-analysis-ik-6.4.0.zip注意: 替换6.4.0为自己安装的elasticsearch版本,安装好后的ik插件在/elasticsearch/plugins/目录下,接着就可以直接指定分词器为ik_smart了,ik内里提供了ik_smart、ik_max_word,大家可以通过如下测试下两种分词器分词效果: 分词使用时机 1.建立或更新文档时候,es会对相应的文档数据举行分词处置惩罚,好比你某个索引字段类型为text,那么插入一条文档时候就会对该字段举行分词处置惩罚,维护该字段文本内容的倒排索引,这种我们成为索引时分词; 2.查询时候,会对你的查询文本举行分词,好比你要查询"苹果手机",则会分词为"苹果、手机"两个单词;我们可以在建立索引时候指定该字段的分词器:建立索引mapping时候指定该字段的分词器也可以在查询时指定分词器:查询时指定分词器 实际使用时候我们需要明确文档中的某个字段是否要分词,如果没须要分词,请关闭,这能节约一定的空间及提高es的写入效率,同时实际生产中的详细的分词器选择要经由自己的实际测试。1. 测试分析器analyze API 是一个工具,可以资助我们检察分析的历程。
(PS:类似于执行计划)curl -X POST "192.168.1.134:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "whitespace", "text": "The quick brown fox."}'curl -X POST "192.168.1.134:9200/_analyze" -H 'Content-Type: application/json' -d'{ "tokenizer": "standard", "filter": [ "lowercase", "asciifolding" ], "text": "Is this déja vu?"}'输出:{ "tokens":[ { "token":"The", "start_offset":0, "end_offset":3, "type":"word", "position":0 }, { "token":"quick", "start_offset":4, "end_offset":9, "type":"word", "position":1 }, { "token":"brown", "start_offset":10, "end_offset":15, "type":"word", "position":2 }, { "token":"fox.", "start_offset":16, "end_offset":20, "type":"word", "position":3 } ]}可以看到,对于每个term,记载了它的位置和偏移量2. Analyzer2.1. 设置内置的分析器 内置的分析器不用任何设置就可以直接使用。固然,默认设置是可以更改的。例如,standard分析器可以设置为支持停止字列表:curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "std_english": { "type": "standard", "stopwords": "_english_" } } } }, "mappings": { "_doc": { "properties": { "my_text": { "type": "text", "analyzer": "standard", "fields": { "english": { "type": "text", "analyzer": "std_english" } } } } } }} 在这个例子中,我们基于standard分析器来界说了一个std_englisth分析器,同时设置为删除预界说的英语停止词列表。
后面的mapping中,界说了my_text字段用standard,my_text.english用std_english分析器。因此,下面两个的分词效果会是这样的:curl -X POST "localhost:9200/my_index/_analyze" -H 'Content-Type: application/json' -d'{ "field": "my_text", "text": "The old brown cow"}curl -X POST "localhost:9200/my_index/_analyze" -H 'Content-Type: application/json' -d'{ "field": "my_text.english", "text": "The old brown cow"}第一个由于用的standard分析器,因此分词的效果是:[ the, old, brown, cow ]第二个用std_english分析的效果是:[ old, brown, cow ]2.2. Standard Analyzer (默认) 如果没有特别指定的话,standard 是默认的分析器。它提供了基于语法的标志化(基于Unicode文天职割算法),适用于大多数语言。
例如:curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "standard", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}上面例子中,那段文本将会输出如下terms:[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog's, bone ]2.2.1. 设置尺度分析器接受下列参数:max_token_length : 最大token长度,默认255stopwords : 预界说的停止词列表,如_english_ 或 包罗停止词列表的数组,默认是 _none_stopwords_path : 包罗停止词的文件路径2.2.2. 示例设置curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "my_english_analyzer": { "type": "standard", "max_token_length": 5, "stopwords": "_english_" } } } }}curl -X POST "localhost:9200/my_index/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "my_english_analyzer", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}以上输出下列terms:[ 2, quick, brown, foxes, jumpe, d, over, lazy, dog's, bone ]2.2.3. 界说standard分析器由下列两部门组成:TokenizerStandard TokenizerToken FiltersStandard Token FilterLower Case Token FilterStop Token Filter (默认被禁用)你还可以自界说curl -X PUT "localhost:9200/standard_example" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "rebuilt_standard": { "tokenizer": "standard", "filter": [ "lowercase" ] } } } }}2.3. Simple Analyzer simple 分析器当它遇到只要不是字母的字符,就将文本剖析成term,而且所有的term都是小写的。例如:curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "simple", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}输入效果如下:[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]2.3.1. 自界说curl -X PUT "localhost:9200/simple_example" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "rebuilt_simple": { "tokenizer": "lowercase", "filter": [ ] } } } }}2.4. Whitespace Analyzerwhitespace 分析器,当它遇到空缺字符时,就将文本剖析成terms示例:curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "whitespace", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}'输出效果如下:[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]2.5. Stop Analyzer stop 分析器 和 simple 分析器很像,唯一差别的是,stop 分析器增加了对删除停止词的支持。
默认用的停止词是 _englisht_(PS:意思是,假设有一句话“this is a apple”,而且假设“this” 和 “is”都是停止词,那么用simple的话输出会是[ this , is , a , apple ],而用stop输出的效果会是[ a , apple ],到这里就看出二者的区别了,stop 不会输出停止词,也就是说它不认为停止词是一个term)(PS:所谓的停止词,可以明白为分开符)2.5.1. 示例输出curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "stop", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}'输出[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]2.5.2. 设置stop 接受以下参数:stopwords : 一个预界说的停止词列表(好比,_englisht_)或者是一个包罗停止词的列表。默认是 _english_stopwords_path : 包罗停止词的文件路径。这个路径是相对于Elasticsearch的config目录的一个路径2.5.3. 示例设置curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "my_stop_analyzer": { "type": "stop", "stopwords": ["the", "over"] } } } }}上面设置了一个stop分析器,它的停止词有两个:the 和 overcurl -X POST "localhost:9200/my_index/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "my_stop_analyzer", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}基于以上设置,这个请求输入会是这样的:[ quick, brown, foxes, jumped, lazy, dog, s, bone ]2.6. Pattern Analyzer用Java正则表达式来将文天职割成terms,默认的正则表达式是W+(非单词字符)2.6.1. 示例输出curl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "pattern", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}由于默认根据非单词字符支解,因此输出会是这样的:[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]2.6.2. 设置pattern 分析器接受如下参数:pattern : 一个Java正则表达式,默认 W+flags : Java正则表达式flags。
好比:CASE_INSENSITIVE 、COMMENTSlowercase : 是否将terms全部转成小写。默认truestopwords : 一个预界说的停止词列表,或者包罗停止词的一个列表。默认是 _none_stopwords_path : 停止词文件路径2.6.3. 示例设置curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "my_email_analyzer": { "type": "pattern", "pattern": "\W|_", "lowercase": true } } } }}上面的例子中设置了根据非单词字符或者下划线支解,而且输出的term都是小写curl -X POST "localhost:9200/my_index/_analyze" -H 'Content-Type: application/json' -d'{ "analyzer": "my_email_analyzer", "text": "John_Smith@foo-bar.com"}因此,基于以上设置,本例输出如下:[ john, smith, foo, bar, com ]2.7. Language Analyzers 支持差别语言情况下的文天职析。
内置(预界说)的语言有:arabic, armenian, basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english, finnish, french, galician, german, greek, hindi, hungarian, indonesian, irish, italian, latvian, lithuanian, norwegian, persian, portuguese, romanian, russian, sorani, spanish, swedish, turkish, thai2.8. 自界说Analyzer前面也说过,一个分析器由三部门组成:zero or more character filtersa tokenizerzero or more token filters2.8.1. 实例设置curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'{ "settings": { "analysis": { "analyzer": { "my_custom_analyzer": { "type": "custom", "tokenizer": "standard", "char_filter": [ "html_strip" ], "filter": [ "lowercase", "asciifolding" ] } } } }}3. Tokenizer 3.1. Standard Tokenizercurl -X POST "localhost:9200/_analyze" -H 'Content-Type: application/json' -d'{ "tokenizer": "standard", "text": "The 2 QUICK Brown-Foxes jumped over the lazy dogu0027s bone."}4. 中文分词器4.1. smartCN一个简朴的中文或中英文混淆文本的分词器这个插件提供 smartcn analyzer 和 smartcn_tokenizer tokenizer,而且不需要设置# 安装bin/elasticsearch-plugin install analysis-smartcn# 卸载bin/elasticsearch-plugin remove analysis-smartcn下面测试一下可以看到,“今天天气真好”用smartcn分析器的效果是:[ 今天 , 天气 , 真 , 好 ]如果用standard分析器的话,效果会是:[ 今 ,天 ,气 , 真 , 好 ]4.2. IK分词器到https://github.com/medcl/elasticsearch-analysis-ik/releases?after=v6.6.0下载对应的版本,这里我下载6.4.06.4.0版本然后,在Elasticsearch的plugins目录下建一个ik目录,将适才下载的文件解压到该目录下最后,重启Elasticsearch接下来,还是用适才那句话来测试一下输出效果如下:{ "tokens": [ { "token": "今天天气", "start_offset": 0, "end_offset": 4, "type": "CN_WORD", "position": 0 }, { "token": "今天", "start_offset": 0, "end_offset": 2, "type": "CN_WORD", "position": 1 }, { "token": "天天", "start_offset": 1, "end_offset": 3, "type": "CN_WORD", "position": 2 }, { "token": "天气", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 3 }, { "token": "真好", "start_offset": 4, "end_offset": 6, "type": "CN_WORD", "position": 4 } ]}显然比smartcn要更好一点参考:es官方文档https://www.elastic.co/guide/en/elasticsearch/reference/current/index.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.htmlhttps://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-tokenfilters.htmlhttps://github.com/medcl/elasticsearch-analysis-ik。
本文来源:博亚体育app官网-www.bairuicaiwu.com
如果您有任何问题,请跟我们联系!
联系我们
Copyright © 2001-2022 www.bairuicaiwu.com. 博亚体育app官网科技 版权所有 备案号:ICP备39057966号-4
地址:西藏自治区阿里地区同德县来瑞大楼985号