重生之我们在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
    }
  ]
}
相关推荐
Lill_bin5 小时前
深入理解ElasticSearch集群:架构、高可用性与数据一致性
大数据·分布式·elasticsearch·搜索引擎·zookeeper·架构·全文检索
RwTo7 小时前
Elasticsearch 聚合搜索
大数据·elasticsearch·搜索引擎·全文检索
求学小火龙8 小时前
ElasticSearch介绍+使用
java·大数据·elasticsearch
檀越剑指大厂8 小时前
【Elasticsearch系列六】系统命令API
大数据·elasticsearch·搜索引擎
bug菌¹15 小时前
滚雪球学SpringCloud[5.1讲]: Spring Cloud Config详解
spring·elasticsearch·spring cloud
Li小李同学Li15 小时前
git学习【持续更新中。。。】
git·学习·elasticsearch
nangonghen16 小时前
通过logstash同步elasticsearch数据
大数据·elasticsearch
檀越剑指大厂16 小时前
【Elasticsearch系列四】ELK Stack
大数据·elk·elasticsearch
2401_8401922717 小时前
ELFK日志分析平台,架构和通信
elk·elasticsearch·架构
cyt涛1 天前
搜索功能技术方案
mysql·elasticsearch·全文检索·canal·索引·数据同步·搜索