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

文章目录

      • 思维导图
      • [0. 前言](#0. 前言)
      • [1. 光速上手](#1. 光速上手)
        • [1.1 指定分词器](#1.1 指定分词器)
        • [1.2 测试分词器](#1.2 测试分词器)
      • [2. 分词流程(重要)](#2. 分词流程(重要))
        • [2.1 基本介绍](#2.1 基本介绍)
        • [2.2 深入如何测试分词器](#2.2 深入如何测试分词器)
      • [3. 自定义一个简单的分词器](#3. 自定义一个简单的分词器)

思维导图

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 字符
json 复制代码
{
  "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
    }
  ]
}
相关推荐
Elasticsearch31 分钟前
如何比较两个 Elasticsearch 索引并找出缺失的文档
elasticsearch
海兰35 分钟前
使用 Elastic Workflows 监控 Kibana 仪表板访问数据
android·人工智能·elasticsearch·rxjava
希望永不加班2 小时前
SpringBoot 整合 Elasticsearch 实现全文检索
java·spring boot·后端·elasticsearch·全文检索
risc1234562 小时前
【Elasticsearch】副本分片(Replica Shard)的 globalCheckpoint 更新与推进机制
elasticsearch
Makoto_Kimur11 小时前
Elasticsearch面试八股整理
elasticsearch
青稞社区.20 小时前
Claude Code 源码深度解析:运行机制与 Memory 模块详解
大数据·人工智能·elasticsearch·搜索引擎·agi
Aktx20FNz21 小时前
iFlow CLI 完整工作流指南
大数据·elasticsearch·搜索引擎
学习3人组1 天前
TortoiseGit冲突解决实战上机练习
大数据·elasticsearch·搜索引擎
A__tao1 天前
Elasticsearch Mapping 一键生成 Go Struct,支持嵌套解析
elasticsearch·es
zs宝来了1 天前
Elasticsearch 索引原理:倒排索引与 Segment 管理
elasticsearch·索引·倒排索引·源码解析·segment