ElasticSearch六--ES--Analyzer分词

Analyzer分词

Analysis 和 Analyzer

Analysis - 文本分析是把全文本转换成一系列单词(term/token)的过程,也叫分词

Analysis 是通过 Analyzer 来实现的

  • 可使用 Elasticsearch 内置的分析器,或者按需制定分析器

除了在数据写入时转换词条,匹配 Query 语句时候也需要用相同的分析器对查询语句进行分析

Analyzer 的组成

分词器是专门处理分词的组件,由三部分组成

  • Character Filters (针对原始文本处理,例如去除html)
  • Tokenizer (按照规则切分为单词)
  • Token Filter(将切分的单词进行加工,小写,删除 stopwords,增加同义词)

例子:

Mastering Elasticsearch & Elasticsearch in Action 经过上面的步骤就会产生

  • master
  • elasticsearch
  • action

ES的内置分词器

  • Standard Analyzer - 默认分词器,按词切分,小写处理
  • Simple Analyzer - 按照非字母切分(符号被过滤),小写处理
  • Stop Analyzer - 小写处理,停用词过滤(the a is)
  • WhiteSpace Analyzer - 按照空格切分,不转小写
  • Keyword Analyzer - 不分词,直接将输入当做输出
  • Patter Analyzer - 正则表达式,默认\W+(非字符分隔)
  • Language - 提供了30多种常见语言的分词器
  • Customer Analyzer 自定义分词器

使用 _analyzer API

直接指定 Analyzer 进行测试

指令

GET /_analyze
{
    "analyzer":"standard",
    "text":"Mastering Elasticsearch & Elasticsearch in Action"
}

结果

{
  "tokens" : [
    {
      "token" : "mastering",
      "start_offset" : 0,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 10,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 25,
      "end_offset" : 38,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "in",
      "start_offset" : 39,
      "end_offset" : 41,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "action",
      "start_offset" : 42,
      "end_offset" : 48,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

指定索引的字段进行测试

指令

POST test_home/_analyze
{
  "field": "job_name",
  "text":"Mastering Elasticsearch, Elasticsearch in Action"
}

结果

{
  "tokens" : [
    {
      "token" : "mastering",
      "start_offset" : 0,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 10,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 25,
      "end_offset" : 38,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "in",
      "start_offset" : 39,
      "end_offset" : 41,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "action",
      "start_offset" : 42,
      "end_offset" : 48,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

自定义分词进行测试

指令

POST _analyze
{
  "tokenizer": "standard",
  "filter": ["lowercase"],
  "text":"Mastering Elasticsearch, Elasticsearch in Action"
}

结果

{
  "tokens" : [
    {
      "token" : "mastering",
      "start_offset" : 0,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 10,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 25,
      "end_offset" : 38,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "in",
      "start_offset" : 39,
      "end_offset" : 41,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "action",
      "start_offset" : 42,
      "end_offset" : 48,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

中文分词的难点

中文句子,切分成一个个词(不是一个个字)

英文字,单词有自然地空格进行切分

一句中文,在不同的上下文,有不同的理解

推荐的一些中文分词器:

  • icu
  • ik : https://github.com/medcl/elasticsearch-analysis-ik
  • thulac : https://github.com/microbun/elasticsearch-thulac-plugin

极客时间 ES 学习笔记

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值