当前位置:网站首页>es 中 mapping 简介
es 中 mapping 简介
2022-06-26 07:02:00 【話吥哆先森丶】
默认mapping
elasticsearch(以下简称ES)是没有模式(schema)的,当我们执行以下命令:
curl -XPUT http://localhost:9200/test/item/1 -d '{"name":"bob", "description": "A Pretty cool guy."}'ES能非常聪明的识别出"name"和"description"字段的类型是string, ES默认会创建以下的mapping:
mappings: {
item: {
properties: {
description: {
type: string
}
name: {
type: string
}
}
}
}什么是mapping
ES的mapping非常类似于静态语言中的数据类型:声明一个变量为int类型的变量, 以后这个变量都只能存储int类型的数据。同样的, 一个number类型的mapping字段只能存储number类型的数据。
同语言的数据类型相比,mapping还有一些其他的含义,mapping不仅告诉ES一个field中是什么类型的值, 它还告诉ES如何索引数据以及数据是否能被搜索到。
注:当你的查询没有返回相应的数据, 你的mapping很有可能有问题。当你拿不准的时候, 直接检查你的mapping。
剖析mapping
一个mapping由一个或多个analyzer组成, 一个analyzer又由一个或多个filter组成的。当ES索引文档的时候,它把字段中的内容传递给相应的analyzer,analyzer再传递给各自的filters。
filter的功能很容易理解:一个filter就是一个转换数据的方法, 输入一个字符串,这个方法返回另一个字符串,比如一个将字符串转为小写的方法就是一个filter很好的例子。
一个analyzer由一组顺序排列的filter组成,执行分析的过程就是按顺序一个filter一个filter依次调用, ES存储和索引最后得到的结果。
总结来说, mapping的作用就是执行一系列的指令将输入的数据转成可搜索的索引项。
默认analyzer
回到我们的例子, ES猜测description字段是string类型,于是默认创建一个string类型的mapping,它使用默认的全局analyzer, 默认的analyzer是标准analyzer, 这个标准analyzer有三个filter:token filter, lowercase filter和stop token filter。
我们可以在做查询的时候键入_analyze关键字查看分析的过程。使用以下指令查看description字段的转换过程:
curl -X GET "http://localhost:9200/test/_analyze?analyzer=standard&pretty=true" -d "A Pretty cool guy."
{
"tokens" : [ {
"token" : "pretty",
"start_offset" : 2,
"end_offset" : 8,
"type" : "<ALPHANUM>",
"position" : 2
}, {
"token" : "cool",
"start_offset" : 9,
"end_offset" : 13,
"type" : "<ALPHANUM>",
"position" : 3
}, {
"token" : "guy",
"start_offset" : 14,
"end_offset" : 17,
"type" : "<ALPHANUM>",
"position" : 4
} ]可以看到, 我们的description字段的值转换成了[pretty], [cool], [guy], 在转换过程中大写的A, 标点符号都被filter过滤掉了, Pretty也转成了全小写的pretty, 这里比较重要的是, 即使ES存储数据的时候仍然存储的是完整的数据, 但是可以搜索到这条数据的关键字只剩下这三个单词了, 其他的都是抛弃掉了。
看看以单词a来搜索的结果:
$ curl -X GET "http://localhost:9200/test/_search?pretty=true" -d '{
"query" : {
"text" : { "description": "a" }
}
}'
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : null,
"hits" : [ ]
}
}
text类型的搜索在查询过程中使用了和之前插入数据相同的分析/过滤系统, 所以我们输入"a",mapping不会有任何返回, 因为单词“a”不会被ES存储和索引。反过来,如果我们使用单词"cool"进行搜索:
curl -X GET "http://localhost:9200/test/_search?pretty=true" -d '{
"query" : {
"text" : { "description": "cool" }
}
}'
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "test",
"_type" : "item",
"_id" : "1",
"_score" : 0.15342641, "_source" : {"name":"zach", "description": "A pretty cool guy"}
} ]
}
}现在就能得到正确的结果,这是一个公认的简单例子, 但是它描述了ES是如何工作的, 不要把mapping想成是数据类型, 把它想象成是搜索数据的指令集合。如果你不想字符"a"被删除, 你需要修改你的analyzer。
边栏推荐
- 分析 NFT 项目的 5 个指标
- China polyimide film market demand and future investment risk outlook report 2022-2027
- 面试官:测试计划和测试方案有什么区别?
- MATLAB线性规划模型学习笔记
- Shell编程-用户信息管理
- One chip realizes functions such as spray 𞓜 ws2812 drive | key touch | LED display | voice broadcast chip and simplifies the design of humidifier products
- [image detection] image target size measurement system based on morphology with matlab code
- I have been testing at Tencent for several years
- Show statement usage supplement
- 【特征提取】基于稀疏PCA实现目标识别信息特征选择附matlab源码
猜你喜欢

Analyze 5 indicators of NFT project

面试被问Redis主从复制不会答?这13张图让你彻底弄明白
![[image enhancement] image defogging based on artificial multiple exposure fusion amef with matlab code](/img/4e/3317b2b99fc8da4d8af190f402e696.png)
[image enhancement] image defogging based on artificial multiple exposure fusion amef with matlab code

【推荐一款实体类转换工具 MapStruct,性能强劲,简单易上手 】

Procedure macros in rust

Invalid problem of self defined map used by Gaode map

“试用期避免被辞退“ 指南攻略

【推荐10个 让你轻松的 IDEA 插件,少些繁琐又重复的代码】

浅析一道经典题

Analyse d'un problème classique
随机推荐
If you meet a female driver who drives didi as an amateur, you can earn 500 yuan a day!
NumPy学习挑战第一关-NumPy的下载与安装
Network IO, disk IO
Numpy learning challenge level 5 - create array
China's Ni MH battery industry development overview analysis and investment trend forecast report 2022 Edition
Here comes the apple ar/vr device exclusive system! Or named realityos
[004] [stm32] MDK project configuration and commissioning
My SQL (II)
MySQL operation database
Big factory interview TCP protocol classic 15 consecutive questions! 22 pictures to make you fully understand
Cocoscreator plays spine animation
i3wm 获取window class
STM 32 uses cube to generate Tim to trigger ADC and transmit through DMA
PyTorch搭建CNN-LSTM混合模型实现多变量多步长时间序列预测(负荷预测)
MySQL basic usage 01
Turris omnia: an open source router technology favored by hackers
. Net 20th anniversary! Microsoft sends a document to celebrate
Six stones Management: exaggerating the achievements, whether the parties themselves know
Analyse d'un problème classique
Market survey of China's coal to liquid industry and analysis report on investment competitiveness during the "14th five year plan" 2022-2027