重生之我们在ES顶端相遇第10 章- 分分分词器的基本使用

思维导图

在这里插入图片描述

0. 前言

分词器在 ES 搜索使用中非常关键,一个好的分词器能够提高搜索的质量,让用户搜索到其想要的内容。
下面我将带大家从整体了解分词器。

1. 光速上手

1.1 指定分词器
DELETE test8
PUT test8
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "whitespace"
      }
    }
  }
}

test8 索引的 title 字段,分词器为 whitespace

1.2 测试分词器
POST _analyze
{
  "analyzer": "whitespace",
  "text": "hello world"
}

2. 分词流程(重要)

2.1 基本介绍

ES 分词可以包含:

  1. 0个或多个 character filters
  2. 有且仅 1 个 tokenizer
  3. 0个或多个 token filters

工作流程如下:
在这里插入图片描述

  • character filters: 对输入进行预处理,比如删除 html 元素,比如将表情符号映射为文本
  • tokenizer: 分词器,上面说到的 standard,whitespace 都属于分词器
  • token filters: 对分词后的结果进行处理。例如输入 Is this déja vu, 如果按照空格分词的话,会被分为 Is, this, déja, vu。我们可以设置 asciifolding token filters, 将 déja, 转换为 deja
2.2 深入如何测试分词器
POST _analyze
{
	"char_filter": ["html_strip", {
		"type": "mapping",
		"mappings": [
			"😂 => happy"
		]
	}],
	"tokenizer": "standard",
	"filter": ["lowercase", "asciifolding"],
	"text": "Is this déja vu? 😂 <b>Important</b>"
}
  • html_strip 用于去掉 html 元素
  • mapping 则是将表情转换为文本
  • standard 用于分词
  • lowercase 用于将所有的大写转换为小写
  • asciifolding 用于将 Unicode 字符转换为 ASCII 字符
{
  "tokens" : [
    {
      "token" : "is",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "this",
      "start_offset" : 3,
      "end_offset" : 7,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "deja",
      "start_offset" : 8,
      "end_offset" : 12,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "vu",
      "start_offset" : 13,
      "end_offset" : 15,
      "type" : "<ALPHANUM>",
      "position" : 3
    },
    {
      "token" : "happy",
      "start_offset" : 17,
      "end_offset" : 19,
      "type" : "<ALPHANUM>",
      "position" : 4
    }
  ]
}

3. 自定义一个简单的分词器

DELETE test8
PUT test8
{
	"mappings": {
		"properties": {
			"name": {
				"type": "text",
				"analyzer": "my_custom_analyzer"
			}
		}
	},
	"settings": {
		"analysis": {
			"char_filter": {
				"cf_happy": {
					"type": "mapping",
					"mappings": ["😂 => happy"]
				}
			},
			"analyzer": {
				"my_custom_analyzer": {
					"type": "custom",
					"tokenizer": "standard",
					"char_filter": ["html_strip", "cf_happy"],
					"filter": ["lowercase", "asciifolding"]
				}
			}
		}
	}
}

测试分词器

POST test8/_analyze
{
  "analyzer": "my_custom_analyzer",
  "text": "😂 I Like Elasticsearch"
}

输出结果

{
  "tokens" : [
    {
      "token" : "happy",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "<ALPHANUM>",
      "position" : 0
    },
    {
      "token" : "i",
      "start_offset" : 3,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 1
    },
    {
      "token" : "like",
      "start_offset" : 5,
      "end_offset" : 9,
      "type" : "<ALPHANUM>",
      "position" : 2
    },
    {
      "token" : "elasticsearch",
      "start_offset" : 10,
      "end_offset" : 23,
      "type" : "<ALPHANUM>",
      "position" : 3
    }
  ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值