当前位置: 首页 > article >正文

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

文章目录

      • 思维导图
      • 0. 前言
      • 1. 光速上手
        • 1.1 指定分词器
        • 1.2 测试分词器
      • 2. 分词流程(重要)
        • 2.1 基本介绍
        • 2.2 深入如何测试分词器
      • 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 字符
{
  "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
    }
  ]
}

http://www.kler.cn/news/290123.html

相关文章:

  • 统计学习与方法实战——K近邻算法
  • Python:解锁高效编程与数据分析的钥匙
  • 传统CV算法——边缘算子与图像金字塔算法介绍
  • ES6基础----proxy的使用
  • 10、Django Admin修改标题
  • Redis面经
  • MySql开机自启动
  • Excel vloopup应用案例
  • 业务解耦-Spring事件监听的三种实现方式
  • SPP/SPPF/Focal Module
  • 无人机 PX4 飞控 | ROS应用层开发:offboard 模式切换详细总结
  • Linux系统的字体管理
  • 12、Django Admin在列表视图页面上显示计算字段
  • Codeforces Round 968 (Div. 2)
  • 代码随想录算法训练营第36天|1049. 最后一块石头的重量、494. 目标和、474.一和零
  • 注册中心 Eureka Nacos
  • 重塑视频监控体验:WebRTC技术如何赋能智慧工厂视频高效管理场景
  • 负载均衡--资源申请说明(三)
  • Android随记
  • 坑——fastjson将字符串转到带枚举的java对象
  • ElasticSearch-数据建模
  • Go语言 Go程基础
  • rust feature 简介
  • shell 学习笔记:向脚本传递参数
  • Android Camera系列(三):GLSurfaceView+Camera
  • 分类预测|基于灰狼GWO优化BP神经网络的数据分类预测Matlab程序GWO-BP|基于鲸鱼WOA优化BP神经网络的数据分类预测Matlab程序WOA-BP
  • 智能提醒助理系列-基础设施准备
  • getLocation:fail, the permission value is offline verifying
  • Flutter--- 常规知识点
  • Redis从入门到入门(上)