重生之我们在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
    }
  ]
}
相关推荐
大只鹅5 小时前
Springboot3.3.4使用spring-data-elasticsearch整合Elasticsearch7.12.1
spring boot·elasticsearch
二闹8 小时前
SLF4J/Logback 配置与 ELK 集成实战指南
后端·elasticsearch·logstash
G皮T15 小时前
【Elasticsearch】全文检索 & 组合检索
大数据·elasticsearch·搜索引擎·全文检索·match·query·组合检索
Elastic 中国社区官方博客10 天前
如何在 Python 中连接 Elasticsearch 并使用 Qwen3 来实现 RAG
大数据·人工智能·python·elasticsearch·搜索引擎·阿里云·全文检索
jiedaodezhuti10 天前
Elasticsearch 如果保证读写一致
大数据·elasticsearch·搜索引擎
Elasticsearch10 天前
了解公共部门中的数据网格:支柱、架构和示例
elasticsearch
曾燕辉10 天前
kibana和elasticsearch安装
大数据·elasticsearch·搜索引擎·kibana
花下的晚风10 天前
Spring Boot 使用 ElasticSearch
spring boot·后端·elasticsearch
jiedaodezhuti10 天前
Elasticsearch 搜索的流程
大数据·elasticsearch·搜索引擎
张敬之、11 天前
jenkins-2.439.1搭建
运维·elasticsearch·jenkins