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

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
    • 默认值: \
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值