Skip to content

ES 数据类型与 Mapping

Mapping 概述

Mapping 定义索引中字段的类型、分词方式、索引规则等,类似数据库的 Schema。ES 有两种方式定义 Mapping:显式 Mapping(手动定义)和动态 Mapping(ES 自动推断)。


一、核心数据类型

字符串类型

类型说明适用场景
text全文索引,会被分词文章内容、商品名称
keyword精确匹配,不分词订单号、状态、邮箱、标签

数值类型

类型范围
long-2^63 ~ 2^63-1
integer-2^31 ~ 2^31-1
short-32768 ~ 32767
byte-128 ~ 127
double双精度浮点
float单精度浮点
scaled_float按缩放因子存储为 long

日期类型

类型说明
date日期时间
date_nanos纳秒精度

布尔与范围

类型说明
booleantrue / false
integer_range, date_range范围类型

复合类型

类型说明
objectJSON 嵌套对象
nested独立可查询的嵌套文档

其他类型

类型说明
binaryBase64 二进制
geo_point经纬度
ipIP 地址

二、常用 Mapping 参数

通用参数

参数说明默认值
index是否索引(false 则不可搜索)true
store是否独立存储false
doc_values列式存储(排序/聚合需要)true

text 专用参数

参数说明示例
analyzer索引时分词器ik_max_word
search_analyzer搜索时分词器ik_smart
fields多字段映射(同时创建 keyword 子字段)见下方示例
json
{
  "name": {
    "type": "text",
    "analyzer": "ik_max_word",
    "search_analyzer": "ik_smart",
    "fields": {
      "keyword": { "type": "keyword" }
    }
  }
}

name 字段支持全文搜索,name.keyword 支持精确匹配和排序。

keyword 专用参数

参数说明
ignore_above超此长度的值不被索引(默认 2147483647)

数值/日期参数

参数说明
coerce是否自动转换类型(默认 true)
scaling_factorscaled_float 的缩放因子
format日期格式,如 yyyy-MM-dd HH:mm:ss

三、完整 Mapping 示例

商品索引

json
PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "id":          { "type": "long" },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "fields": { "keyword": { "type": "keyword" } }
      },
      "brand":       { "type": "keyword" },
      "category_id": { "type": "integer" },
      "price":       { "type": "scaled_float", "scaling_factor": 100 },
      "stock":       { "type": "integer" },
      "status":      { "type": "keyword" },
      "tags":        { "type": "keyword" },
      "description": { "type": "text", "analyzer": "ik_max_word" },
      "created_at":  { "type": "date", "format": "yyyy-MM-dd HH:mm:ss" },
      "updated_at":  { "type": "date" },
      "location":    { "type": "geo_point" },
      "specs": {
        "type": "nested",
        "properties": {
          "name":  { "type": "keyword" },
          "value": { "type": "keyword" }
        }
      }
    }
  }
}

用户索引

json
PUT /users
{
  "mappings": {
    "properties": {
      "id":       { "type": "long" },
      "nickname": {
        "type": "text",
        "analyzer": "ik_max_word",
        "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
      },
      "email":    { "type": "keyword" },
      "phone":    { "type": "keyword" },
      "age":      { "type": "byte" },
      "is_vip":   { "type": "boolean" },
      "balance":  { "type": "double" },
      "address":  { "type": "text", "analyzer": "ik_smart" },
      "tags":     { "type": "keyword" },
      "birthday": { "type": "date", "format": "yyyy-MM-dd" },
      "register_time": { "type": "date" }
    }
  }
}

四、Dynamic Template(动态模板)

不预先定义 Mapping,而是定义字段名到类型的匹配规则:

json
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keyword": {
          "match_mapping_type": "string",
          "mapping": { "type": "keyword" }
        }
      },
      {
        "age_and_count": {
          "match_mapping_type": "long",
          "match": "age|level|count|stock|score",
          "mapping": { "type": "integer" }
        }
      }
    ]
  }
}

规则

  • match_mapping_type:匹配 ES 推断的类型
  • match / unmatch:按字段名正则匹配/排除
  • path_match:按嵌套路径匹配
最近更新