设为首页 加入收藏

TOP

Elasticsearch Analyzer 内置分词器(一)
2023-07-25 21:30:46 】 浏览:54
Tags:Elasticsearch Analyzer

Elasticsearch Analyzer 内置分词器

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

image-20221103233624275

前置知识

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 查询时候 需要对查询文本 进行分词

image-20221103163336660

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",
  &qu
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Midjourney:一步一步教你如何使.. 下一篇常用类.String类

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目