ES的预置分词器

Elasticsearch(简称 ES)提供了多种预置的分词器(Analyzer),用于对文本进行分词处理。分词器通常由字符过滤器(Character Filters)、分词器(Tokenizer)和词元过滤器(Token Filters)组成。以下是一些常用的预置分词器及其示例:


1. Standard Analyzer(标准分词器)

  • 默认分词器,适用于大多数语言。

  • 处理步骤:

    1. 使用标准分词器(Standard Tokenizer)按空格和标点符号分词。
    2. 应用小写过滤器(Lowercase Token Filter)将词元转换为小写。
  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "standard",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["the", "2", "quick", "brown", "foxes", "jumped", "over", "the", "lazy", "dog's", "bone"]

2. Simple Analyzer(简单分词器)

  • 按非字母字符(如数字、标点符号)分词,并将词元转换为小写。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "simple",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["the", "quick", "brown", "foxes", "jumped", "over", "the", "lazy", "dog", "s", "bone"]

3. Whitespace Analyzer(空格分词器)

  • 仅按空格分词,不转换大小写,不处理标点符号。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "whitespace",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["The", "2", "QUICK", "Brown-Foxes", "jumped", "over", "the", "lazy", "dog's", "bone."]

4. Keyword Analyzer(关键词分词器)

  • 将整个文本作为一个单独的词元,不做任何分词处理。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "keyword",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."]

5. Stop Analyzer(停用词分词器)

  • 类似于简单分词器,但会过滤掉常见的停用词(如 "the", "and", "a" 等)。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "stop",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["quick", "brown", "foxes", "jumped", "over", "lazy", "dog", "s", "bone"]

6. Pattern Analyzer(正则分词器)

  • 使用正则表达式定义分词规则。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "pattern",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    默认按非字母字符分词,并转换为小写:

    json 复制代码
    ["the", "2", "quick", "brown", "foxes", "jumped", "over", "the", "lazy", "dog", "s", "bone"]

7. Language Analyzer(语言分词器)

  • 针对特定语言优化,支持多种语言(如英语、中文、法语等)。

  • 示例(英语)

    json 复制代码
    POST _analyze
    {
      "analyzer": "english",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["2", "quick", "brown", "fox", "jump", "over", "lazi", "dog", "bone"]

8. ICU Analyzer(国际化分词器)

  • 基于 ICU(International Components for Unicode)库,支持多语言分词。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "icu_analyzer",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["the", "2", "quick", "brown", "foxes", "jumped", "over", "the", "lazy", "dog's", "bone"]

9. Fingerprint Analyzer(指纹分词器)

  • 对文本进行分词、去重、排序,并生成唯一的"指纹"。

  • 示例

    json 复制代码
    POST _analyze
    {
      "analyzer": "fingerprint",
      "text": "The 2 QUICK Brown-Foxes jumped over the lazy dog's bone."
    }

    输出

    json 复制代码
    ["2", "bone", "brown", "dog", "foxes", "jumped", "lazy", "over", "quick", "the"]

总结

Elasticsearch 的预置分词器适用于不同的场景,开发者可以根据需求选择合适的分析器,或者自定义分词器以满足特定需求。

相关推荐
wingaso14 分钟前
[经验总结]删除gitlab仓库分支报错:错误:无法推送一些引用到“http:”
linux·数据仓库·git
独行soc19 分钟前
2025年渗透测试面试题总结-阿里云[实习]阿里云安全-安全工程师(题目+回答)
linux·经验分享·安全·阿里云·面试·职场和发展·云计算
勤不了一点30 分钟前
小白上手RPM包制作
linux·运维·服务器·软件工程
盛夏绽放1 小时前
Python字符串常用内置函数详解
服务器·开发语言·python
麦a~M了M2 小时前
ansible
linux·运维·ansible
zizle_lin2 小时前
优雅使用Gunicorn进程管理FastAPI
服务器·fastapi·gunicorn
2501_906314323 小时前
MCP-RAG 服务器:完整设置和使用指南
运维·服务器
QQ_4376643143 小时前
Linux下可执行程序的生成和运行详解(编译链接汇编图解)
linux·运维·c语言·汇编·caffe
IP管家3 小时前
物联网设备远程管理:基于代理IP的安全固件更新通道方案
服务器·网络·物联网·网络协议·tcp/ip·安全·ip
搬码临时工3 小时前
远程连接电脑的方法?异地远程桌面连接和三方软件实现
运维·服务器·网络·物联网·电脑·远程工作