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

重生之我们在ES顶端相遇第 20 章 - Mapping 参数设置大全(进阶)

文章目录

      • 0. 前言
      • 1. 前置知识 - _source
      • 2. copy_to
      • 3. doc_values
      • 4. index
      • 5. enabled
      • 6. normalizer
      • 7. null_value
      • 8. 总结

0. 前言

在基础篇,我们只介绍了索引 Mapping 的基本用法。
本章将深入探讨日常中较经常使用的 Mapping 其他参数配置。
不想看过程,可直接看最后的总结。

1. 前置知识 - _source

在介绍本章内容时,这边先补充一个知识点:
如果细心观察你会发现,搜索 API 返回的结果都包含在一个叫 _source 的字段中。如下图所示
在这里插入图片描述

_source 在 ES 中非常重要。默认情况下,所有的字段都会存储在 _source 中。
以下 API 都需要用到 _source

  • 搜索返回结果
  • updateupdate by queryreindex API
  • 高亮显示 API

因此,虽说我们可以通过以下操作将 _source 禁用,但一般不禁用

PUT test20_source
{"mappings": {"_source": {"enabled": false}}
}

2. copy_to

  • 描述: 将多个字段的值拷贝至 1 个或多个字段
  • 作用: 一般用于优化多个 text 字段查询

其使用如下:

PUT test20_copyto
{"mappings": {"properties": {"first_name": {"type": "text","copy_to": "full_name" },"last_name": {"type": "text","copy_to": "full_name" },"full_name": {"type": "text"}}}
}GET test20_copyto/_search
{"query": {"match": {"full_name": "hello"}}
}

注意: copy_to 不会改变 _source 的值

3. doc_values

  • 描述: 专为字段值存储而设计的列式存储格式。默认情况下,除了 textannotated_text 不启用,其他字段默认启用。禁用该字段,可以节约磁盘存储。
  • 作用: 允许字段使用排序、聚合、script 字段访问。

其使用如下:

PUT test_doc_value
{"mappings": {"properties": {"name": { "type": "keyword"},"address": { "type": "keyword","doc_values": false}}}
}PUT test_doc_value/_doc/1
{"name": "elasticsearch","address": "china"
}# 该操作会报错, address 字段的 doc_values 被禁用了
GET test_doc_value/_search
{"sort": [{"address": {"order": "desc"}}]
}

注意: wildcard 字段(模糊匹配)无法禁用

4. index

  • 描述: 是否为字段建立索引(可以简单理解为倒排索引),默认 true。
  • 作用: 禁用时,字段无法搜索

其使用如下:

PUT test_index
{"mappings": {"properties": {"name": {"type": "keyword","index": false},"address": {"type": "keyword"},"age": {"type": "integer" ,"index": false}}}
}PUT test_index/_doc/1
{"name": "java","address": "china","age": 13
}# 不为其建立索引,所以无法被搜索
GET test_index/_search
{"query": {"bool": {"filter": [{"range": {"age": {"gte": 10}}}]}}
}# 依然可以排序
GET test_index/_search
{"query": {"term": {"address": {"value": "china"}}},"sort": [{"age": {"order": "desc"}}]
}

index 字段控制是否可以被搜索,doc_values 控制是否可以被排序、聚合。

5. enabled

  • 描述: enabled 只能应用在顶级映射定义和对象字段上。默认为 true。
  • 作用: 只存储字段值,不为该字段建立任何索引。禁用后,仅存在于 _source 字段中,其他任何地方都不会存储该字段,即可以认为字段无法被搜索、聚合、排序、script 字段访问。但可以获取、修改该字段值。

其使用如下:

# 该操作会报错,无法用于非对象字段
PUT test_enable
{"mappings": {"properties": {"name": { "type": "keyword","enabled": false}}}
}PUT test_enable
{"mappings": {"properties": {"name": { "type": "keyword"},"address": { "type": "keyword"},"relation": {"type": "object","enabled": false},"relation2": {"type": "object"}}}
}PUT test_enable/_doc/1
{"name": "java","address": "china","relation": {"fruit": "apple","hobby": "basketball"},"relation2": {"fruit": "apple","hobby": "basketball"}
}# 无法查询
GET test_enable/_search
{"query": {"term": {"relation.fruit": {"value": "apple"}}}
}# 正常查询
GET test_enable/_search
{"query": {"term": {"relation2.fruit": {"value": "apple"}}}
}

enableddoc_valuesindex 的结合体。被禁用后,仅能被查看、修改值。但 enabled 只能作用于顶级的映射定义和对象字段。

6. normalizer

  • 作用: 规范 keyword 字段值。例如,统一将值转化为小写

其使用如下:

PUT test20_normalizer
{"settings": {"analysis": {"normalizer": {"my_normalizer": {"type": "custom","char_filter": [],"filter": ["lowercase", "asciifolding"]}}}},"mappings": {"properties": {"name": {"type": "keyword","normalizer": "my_normalizer"},"name2": {"type": "keyword"}}}
}PUT test20_normalizer/_doc/1
{"name": "HELLO WORLD","name2":  "HELLO WORLD"
}# 因为 name 被转化为小写,因此我们使用小写也可以搜索到
GET test20_normalizer/_search
{"query": {"term": {"name": {"value": "hello world"}}}
}# name2 未被规范化处理,因此无法使用小写搜索到结果集
GET test20_normalizer/_search
{"query": {"term": {"name2": {"value": "hello world"}}}
}

7. null_value

  • 描述: 当一个字段没有值时,写入到 ES 文档中会没有该字段。查询时我们则使用 exists API,ES 提供了 null 值的方式,允许我们查询 null 值。
  • 作用: 写入时设置值为 null。搜索时可通过声明的 null 值来搜索。

其使用如下:

PUT test_null_value 
{"mappings": {"properties": {"name": {"type": "keyword","null_value": "NULL"},"address": {"type": "integer","null_value": -99999}}}
}POST /_bulk
{"index": {"_index":"test_null_value","_id": "1"}}
{"address": 12,"name": null}
{"index": {"_index":"test_null_value","_id": "2"}}
{"name": "hello","address": null}# 使用声明的 null_value 查询 null 值
GET test_null_value/_search
{"query": {"term": {"address": {"value": -99999}}}
}# 使用声明的 null_value 查询 null 值
GET test_null_value/_search
{"query": {"term": {"name": {"value": "NULL"}}}
}

注意: null_value 不改变 _source

8. 总结

  • _source
    • 作用: 所有的字段都会存储在 _source 中。搜索、updateupdate by queryreindex、高亮显示 API 需要用到该字段
    • 默认值: 默认启用
  • _copy_to
    • 作用: 将多个字段拷贝至 1 个或多个字段,用于优化多个 text 字段查询
    • 默认值: \
  • doc_values
    • 作用: 允许字段使用排序、聚合、script 字段访问
    • 默认值: 默认除了 textannotated_text 不启用,其他字段都启用
  • index
    • 作用: 是否允许字段被搜索
    • 默认值: true
  • enabled
    • 作用: 只能作用于顶级映射定义和对象字段,只存储于 _source 中,其他任何地方均不存储。false 时,只能查看、修改字段值,其他操作均被禁用
    • 默认值: true
  • normalizer
    • 作用: 规范 keyword 字段值
    • 默认值: \
  • null_value
    • 作用: 搜索 null
    • 默认值: \

http://www.mrgr.cn/news/43750.html

相关文章:

  • 【表达式的值II】
  • 终于有人把多模态大模型讲这么详细了
  • [Python] 轻松入门输出语句与条件语句
  • ElasticSearch 备考 -- Snapshot Restore
  • 《浔川社团官方通报 —— 为何明确 10 月 2 日上线的浔川 AI 翻译 v3.0 再次被告知延迟上线》
  • 教育技术革新:SpringBoot在线教育系统开发
  • 【数学分析笔记】第4章第4节 复合函数求导法则及其应用(3)
  • ffmpeg面向对象——拉流协议匹配机制探索
  • CSS盒子模型
  • 自动驾驶系列—线控系统:驱动自动驾驶的核心技术解读与应用指南
  • 独孤思维:闲得蛋疼才去做副业
  • C++面试速通宝典——10
  • ICM20948 DMP代码详解(64)
  • 降低大模型幻觉的5种方案
  • IDEA基础开发配置以及和git的联动
  • Leetcode—200. 岛屿数量【中等】
  • aws(学习笔记第一课) AWS CLI,创建ec2 server以及drawio进行aws画图
  • 什么是transformer大模型,答案就在这里
  • jQuery——事件处理补充
  • Leetcode—763. 划分字母区间【中等】