索引Mapping

索引的结构化约束

  1. Mapping类同数据表的schema定义表结构,与ES定义索引内的结构
  2. ES的Mapping提供类比数据表Schema更多的多选择与灵活性

Mapping用途

  1. Mapping用来规范ES索引结构与行为,也是性能优化的重要一环
  2. 应用场景不同,Mapping的设置运用也不同,如日志与业务系统

image.png
创建mapping:

  1. PUT test-01
  2. {
  3. "mappings": {
  4. "properties": {
  5. "name":{
  6. "type": "text"
  7. },
  8. "email":{
  9. "type": "text"
  10. },
  11. "old":{
  12. "type": "integer"
  13. }
  14. }
  15. }
  16. }

修改mapping

  1. PUT test-01/_mapping
  2. {
  3. "properties": {
  4. "name":{
  5. "type": "text"
  6. },
  7. "email":{
  8. "type": "text"
  9. },
  10. "old":{
  11. "type": "integer"
  12. },
  13. "company":{
  14. "type": "text"
  15. }
  16. }
  17. }

Mapping映射

  • Dynamic设置为true时,如果有新增字段的文档写入,Mapping会更新,相应的对新增字段定义类型。
  • Dynamic设置为false,Mapping不会被更新,并且新增字段的数据无法被索引,但是,信息会出现在_source中。
  • Dynamic设置为Strict,文档会写入失败!
  • 设置为true时:文档可索引、字段可索引、mapping允许被更新。
    设置为false时:文档可索引、字段不可索引、mapping不允许被更新。
    设置为true时:文档不可索引、字段不可索引、mapping不允许被更新。

image.png

常用字段类型应用

Text:文本

文本类型:
按规则分词,分成至少1个词,默认粉刺器standard
应用场景:
text,用于需要分词检索的场景
image.png

  1. DELETE test-001
  2. PUT test-01/_mapping
  3. {
  4. "properties": {
  5. "name":{
  6. "type": "text"
  7. }
  8. }
  9. }
  10. PUT test-01/_doc/1
  11. {
  12. "name":"liwenjun,hunan,ChangSha"
  13. }
  14. POST _analyze
  15. {
  16. "analyzer": "standard",
  17. "text": ["liwenjun,hunan,ChangSha"]
  18. }

keyword:关键词

概念说明:

  1. 严格意义上就是不分词,仅仅分一个词
  2. 包括3中:

keyworld:一个单词
constant _keworld:默认插入一个固定值
wildcard:通配符的检索场景
应用场景

  1. 固定文本信息,无需全文检索场景
  2. 姓名,省份,商品类目
  3. 替代传统数据库字段检索,用于多条件检索
  4. 特殊场景下查询

image.png
测试:

  1. PUT test-006
  2. {
  3. "mappings": {
  4. "dynamic": false,
  5. "properties": {
  6. "name01":{
  7. "type": "keyword"
  8. },
  9. "name02":{
  10. "type": "constant_keyword",
  11. "value": "liwenjun001"
  12. },
  13. "name03":{
  14. "type": "wildcard"
  15. }
  16. }
  17. }
  18. }

插入数据:

  1. PUT test-006/_doc/1
  2. {
  3. "name01":"liwenjun,hunan,ChangSha",
  4. "name03":"liwenjun,hunan,ChangSha"
  5. }
  6. PUT test-006/_doc/2
  7. {
  8. "name01":"liwenjun,hunan,ChangSha",
  9. "name02":"liwenjun001",
  10. "name03":"liwenjun,hunan,ChangSha"
  11. }
  12. PUT test-006/_doc/3
  13. {
  14. "name01":"liwenjun,hunan,ChangSha",
  15. "name02":"liwenjun001",
  16. "name03":"liwenjun,sanya,ChangSha"
  17. }
  18. GET test-006/_search
  19. {
  20. "query": {
  21. "wildcard": {
  22. "name03": "*ny*"
  23. }
  24. }
  25. }

整数:

Long类型:空间占用:64bit
Integer类型:空间占用:32bit
short类型:空间占用:16bit
Byte类型:空间占用:8bit
image.png

浮点

浮点类型:
Double
Float
Half_float
Scaled_float: 缩放浮点类型,背后基于Long类型实现,会出现精度问题;
应用注意:
浮点类型精确统计会出现精确性问题
Java底层实现问题;
如何避免?使用整数类型来处理
image.png

date日期类型:

1.date
2.date_nano
日期格式化
format,支持多种自定义格式,注意,建议使用UTC时间格式,一定要使用UTC
image.png

字符类型背后算法

Text,keyword类型
倒排索引:Inverted index
image.png

数值类型背后算法:

整型,浮点型,日期类型
BDK树算法
底层存储压缩
image.png

复合字段类型的应用

object

表现形式
Json对象形式,可以嵌套多种子对象
内部存储实现
内部存储实际非json,通过分隔符建立嵌套关系,字段命名不可以采用分隔符;

  1. PUT test-006
  2. {
  3. "mappings": {
  4. "dynamic": false,
  5. "properties": {
  6. "name01":{
  7. "type": "keyword"
  8. },
  9. "name02":{
  10. "type": "constant_keyword",
  11. "value": "liwenjun001"
  12. },
  13. "name03":{
  14. "type": "wildcard"
  15. },
  16. "address":{
  17. "properties": {
  18. "pro":{
  19. "type":"text"
  20. },
  21. "city":{
  22. "type":"text"
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. PUT test-006/_doc/1
  30. {
  31. "name01":"liwenjun,hunan,ChangSha",
  32. "name03":"liwenjun,hunan,ChangSha",
  33. "address":{
  34. "pro":"hunan",
  35. "city":"changsha"
  36. }
  37. }

array:数组

  • 注意说明

ES未定义数组类型,仅仅是支持,类似object方式支持的,但无需设置,填充数据设时设定数据样本即可

  • 支持类型

一维数组
多位数组
对象数组

  1. PUT test-006/_doc/1
  2. {
  3. "name01":"liwenjun,hunan,ChangSha",
  4. "name03":"liwenjun,hunan,ChangSha",
  5. "address":[{
  6. "pro":"hunan",
  7. "city":"changsha"
  8. },
  9. {
  10. "pro":"hebei",
  11. "city":"baoding"
  12. }
  13. ]
  14. }
  15. GET test-006/_search
  16. {
  17. }
  18. {
  19. "took" : 106,
  20. "timed_out" : false,
  21. "_shards" : {
  22. "total" : 1,
  23. "successful" : 1,
  24. "skipped" : 0,
  25. "failed" : 0
  26. },
  27. "hits" : {
  28. "total" : {
  29. "value" : 1,
  30. "relation" : "eq"
  31. },
  32. "max_score" : 1.0,
  33. "hits" : [
  34. {
  35. "_index" : "test-006",
  36. "_type" : "_doc",
  37. "_id" : "1",
  38. "_score" : 1.0,
  39. "_source" : {
  40. "name01" : "liwenjun,hunan,ChangSha",
  41. "name03" : "liwenjun,hunan,ChangSha",
  42. "address" : [
  43. {
  44. "pro" : "hunan",
  45. "city" : "changsha"
  46. },
  47. {
  48. "pro" : "hebei",
  49. "city" : "baoding"
  50. }
  51. ]
  52. }
  53. }
  54. ]
  55. }
  56. }

范围类型:

  • 范围类型

整型范围
浮点范围
日期范围
IP范围

  • 应用场景:

如,衣服尺寸适合,XL(175-185)
BDK树
image.png

复杂对象单一化类型

flattened:单一化类型
概念定义:
将复杂对象下的字段归属到单一类型,避免索引mapping字段数量限制引起的错误
应用场景:
日志混合场景,可以避免接入不确定字段数量过多,避免错误;
避免字段膨胀:

  1. dynamic 设置为 strict
  2. 设置 “address02”:{

    1. "type": "flattened"<br /> }<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/322762/1657680815546-833465e0-0839-449f-83e5-648307a8b64c.png#clientId=u374441af-746d-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=541&id=u62dcf2bf&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1082&originWidth=832&originalType=binary&ratio=1&rotation=0&showTitle=false&size=677444&status=done&style=none&taskId=u19597901-2769-4170-9ddd-4f725670e89&title=&width=416)

    索引字段类型设计限制

    字段设置限制默认1024个字段,但是可以修改
    对象对象深度限制20层,

    Mapping源码解读