模式分析器
编辑模式分析器编辑
pattern
分析器使用正则表达式将文本拆分为词条。正则表达式应该匹配 词条分隔符 而不是词条本身。正则表达式的默认值为 \W+
(或所有非单词字符)。
警惕病态正则表达式
模式分析器使用 Java 正则表达式。
编写不良的正则表达式可能会运行非常缓慢,甚至抛出 StackOverflowError 并导致运行它的节点突然退出。
阅读更多关于 病态正则表达式以及如何避免它们 的信息。
示例输出编辑
response = client.indices.analyze( body: { analyzer: 'pattern', text: "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone." } ) puts response
POST _analyze { "analyzer": "pattern", "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 ]
配置编辑
pattern
分析器接受以下参数
|
一个 Java 正则表达式,默认为 |
|
Java 正则表达式 标志。标志应该用管道符号分隔,例如 |
|
词条是否应该小写。默认为 |
|
预定义的停用词列表,如 |
|
包含停用词的文件的路径。 |
有关停用词配置的更多信息,请参阅 停用词过滤器。
示例配置编辑
在此示例中,我们将配置 pattern
分析器以在非单词字符或下划线 (\W|_
) 处拆分电子邮件地址,并将结果转换为小写
response = client.indices.create( index: 'my-index-000001', body: { settings: { analysis: { analyzer: { my_email_analyzer: { type: 'pattern', pattern: '\\W|_', lowercase: true } } } } } ) puts response response = client.indices.analyze( index: 'my-index-000001', body: { analyzer: 'my_email_analyzer', text: '[email protected]' } ) puts response
PUT my-index-000001 { "settings": { "analysis": { "analyzer": { "my_email_analyzer": { "type": "pattern", "pattern": "\\W|_", "lowercase": true } } } } } POST my-index-000001/_analyze { "analyzer": "my_email_analyzer", "text": "[email protected]" }
上面的示例产生以下词条
[ john, smith, foo, bar, com ]
驼峰式分词器编辑
以下更复杂的示例将驼峰式文本拆分为词条
response = client.indices.create( index: 'my-index-000001', body: { settings: { analysis: { analyzer: { camel: { type: 'pattern', pattern: '([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])' } } } } } ) puts response response = client.indices.analyze( index: 'my-index-000001', body: { analyzer: 'camel', text: 'MooseX::FTPClass2_beta' } ) puts response
PUT my-index-000001 { "settings": { "analysis": { "analyzer": { "camel": { "type": "pattern", "pattern": "([^\\p{L}\\d]+)|(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)|(?<=[\\p{L}&&[^\\p{Lu}]])(?=\\p{Lu})|(?<=\\p{Lu})(?=\\p{Lu}[\\p{L}&&[^\\p{Lu}]])" } } } } } GET my-index-000001/_analyze { "analyzer": "camel", "text": "MooseX::FTPClass2_beta" }
上面的示例产生以下词条
[ moose, x, ftp, class, 2, beta ]
上面的正则表达式更容易理解为
([^\p{L}\d]+) # swallow non letters and numbers, | (?<=\D)(?=\d) # or non-number followed by number, | (?<=\d)(?=\D) # or number followed by non-number, | (?<=[ \p{L} && [^\p{Lu}]]) # or lower case (?=\p{Lu}) # followed by upper case, | (?<=\p{Lu}) # or upper case (?=\p{Lu} # followed by upper case [\p{L}&&[^\p{Lu}]] # then lower case )
定义编辑
pattern
分析器包含
如果需要自定义 pattern
分析器,使其超出配置参数的范围,则需要将其重新创建为 custom
分析器并对其进行修改,通常是通过添加词条过滤器。这将重新创建内置的 pattern
分析器,您可以将其用作进一步自定义的起点
response = client.indices.create( index: 'pattern_example', body: { settings: { analysis: { tokenizer: { split_on_non_word: { type: 'pattern', pattern: '\\W+' } }, analyzer: { rebuilt_pattern: { tokenizer: 'split_on_non_word', filter: [ 'lowercase' ] } } } } } ) puts response