Elasticsearch(三)Analysis

本文介绍了Elasticsearch的多种分析器,包括标准分析器、简单分析器、空白分析器、停用词分析器、关键字分析器、模式分析器、指纹分析器和自定义分析器。各分析器的特点和配置选项进行了详细讲解,例如标准分析器支持小写化和停用词过滤,模式分析器允许通过正则表达式定制分词规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.standard analyzer 标准分析器

  由以下使用分词器和分词过滤器组成

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)单个词最大长度(max_token_length 默认255,超过则按照255切分)

(2)停用词(stopwords   即配置过滤词如the to等  默认为_none_)

(3)停用词文件路径(stopwords_path   默认没配)

   修改配置,需新建一个分析器,如下

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_standard_analyzer": {
          "type": "standard",
          "max_token_length": 10,
          "stopwords": "_english_"
        }
      }
    }
  }
}

   通过以上配置,在my_index中新增了一个my_standard_analyzer分析器,之后字段定义可直接使用此分析器。

   stopwords支持一些国家的语言进行分词,_english_为标准英文单词分词,支持如下,不支持中文。

  _arabic__armenian__basque__bengali__brazilian__bulgarian__catalan__czech__danish__dutch__english__finnish__french__galician__german__greek__hindi__hungarian__indonesian__irish__italian__latvian__norwegian__persian__portuguese__romanian__russian__sorani__spanish__swedish__thai__turkish_

  还可以通过如下配置该分析器具体过滤的词

"stopwords": ["and", "is", "the"]

   使用当前配置分词结果如下   

[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]

2.simple analyzer 简单分析器

由以下使用分词器组成

只支持自动小写化词,不支持配置。

3.whitespace  analyzer  空白分析器

由以下使用分词器组成

区分大小写分词,不做小写化处理,分词结果如下

[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog's, bone. ]

4.stop  analyzer  停用分析器

  由以下使用分词器和分词过滤器组成

  默认自动小写化词,默认使用_english_ 方式停用词。

  可配置,配置参数stopwords和stopwords_path,与standard 分析器配置方式一致,参考1例子。

5.keyword  analyzer 关键字分析器 

由以下使用分词器组成

   无自动小写化,返回整段词,无配置,分析结果如下。

[ The 2 QUICK Brown-Foxes jumped over the lazy dog's bone. ]

6.pattern analyzer  模式分析器

  由以下使用分词器和分词过滤器组成

   自动小写化,通过正则匹配分割词,默认是\W+,即匹配所有非数字、字母和下划线的字符,默认分词结果如下。

[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]

    可配置参数如下

(1)pattern   匹配分隔符的正则表达式,默认\W+,正则可参考此链接Java regular expression

(2)flags    匹配模式,多个用|分隔(CASE_INSENSITIVE|COMMENTS),参考java Pattern类field,入口

(3)lowercase   自动小写化,默认true

(4)stopwords    停用词

(5)stopwords_path   停用词文件路径

配置例子如下:以所有非字母和数字作为切割符,自动小写化

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_email_analyzer": {
          "type":      "pattern",
          "pattern":   "\W|_", 
          "lowercase": true
        }
      }
    }
  }
}

7.fingerprint  analyzers

  由以下使用分词器和分词过滤器组成

返回单一结果,结果自动排序,去除重复词。

POST _analyze
{
  "analyzer": "fingerprint",
  "text": "Yes yes, Gödel said this sentence is consistent and."
}
[ and consistent godel is said sentence this yes ]

配置参数max_output_size,stopwords,stopwords_path与标准分析器一致

separator参数    分词结果中每个单词的分隔方式,默认是空格

8.custom analyzer 自定义分析器

  通过配置tokenizercharacter filterstoken filters实现定制的分析器。

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type":      "custom", 
          "tokenizer": "standard",
          "char_filter": [
            "html_strip"
          ],
          "filter": [
            "lowercase",
            "asciifolding"
          ]
        }
      }
    }
  }
}

POST my_index/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "Is this <b>déjà vu</b>?"
}

分词结果

[ is, this, deja, vu ]

参考自官方文档: https://www.elastic.co/guide/en/elasticsearch/reference/6.x/analysis.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值