Elasticsearch数据类型 #

一、数据类型概览 #

1.1 类型分类 #

text
数据类型
├── 核心类型
│   ├── 字符串类型
│   ├── 数值类型
│   ├── 日期类型
│   ├── 布尔类型
│   ├── 二进制类型
│   └── 范围类型
├── 复杂类型
│   ├── 对象类型
│   ├── 嵌套类型
│   └── 扁平类型
├── 专用类型
│   ├── 地理类型
│   ├── IP类型
│   ├── 补全类型
│   └── 分词计数类型
└── 数组类型
    └── 所有类型都支持数组

二、字符串类型 #

2.1 text类型 #

text类型用于全文搜索,会被分词处理。

json
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "standard",
        "search_analyzer": "standard",
        "fielddata": false,
        "index": true,
        "norms": true
      }
    }
  }
}

参数说明

参数 说明 默认值
analyzer 索引分析器 standard
search_analyzer 搜索分析器 同analyzer
fielddata 是否启用字段数据 false
index 是否索引 true
norms 是否存储规范 true

使用场景

  • 文章内容
  • 产品描述
  • 日志消息

2.2 keyword类型 #

keyword类型用于精确匹配、排序和聚合,不分词。

json
{
  "mappings": {
    "properties": {
      "status": {
        "type": "keyword",
        "ignore_above": 256
      },
      "tags": {
        "type": "keyword"
      }
    }
  }
}

参数说明

参数 说明 默认值
ignore_above 超过长度不索引 256
index 是否索引 true
doc_values 是否存储doc_values true

使用场景

  • 状态字段
  • 标签
  • 分类
  • ID

2.3 text + keyword组合 #

json
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

使用方式:

json
{
  "query": {
    "match": { "title": "iPhone" }
  }
}

{
  "query": {
    "term": { "title.keyword": "iPhone 15 Pro" }
  }
}

三、数值类型 #

3.1 整数类型 #

类型 最小值 最大值 用途
byte -128 127 小范围整数
short -32768 32767 短整数
integer -2^31 2^31-1 普通整数
long -2^63 2^63-1 大整数
json
{
  "mappings": {
    "properties": {
      "age": { "type": "integer" },
      "quantity": { "type": "long" }
    }
  }
}

3.2 浮点类型 #

类型 精度 用途
float 32位IEEE 754 一般浮点数
double 64位IEEE 754 高精度浮点
half_float 16位IEEE 754 低精度浮点
scaled_float 带缩放因子 货币等固定精度
json
{
  "mappings": {
    "properties": {
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      },
      "rating": {
        "type": "half_float"
      },
      "distance": {
        "type": "double"
      }
    }
  }
}

3.3 无符号整数 #

类型 最小值 最大值
unsigned_long 0 2^64-1
json
{
  "mappings": {
    "properties": {
      "big_id": {
        "type": "unsigned_long"
      }
    }
  }
}

四、日期类型 #

4.1 date类型 #

json
{
  "mappings": {
    "properties": {
      "created_at": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
      }
    }
  }
}

内置格式

格式名 示例
epoch_millis 1234567890000
epoch_second 1234567890
date_optional_time 2024-01-01 或 2024-01-01T12:00:00
strict_date_optional_time 同上,更严格
basic_date 20240101
basic_date_time 20240101T120000.000Z

4.2 date_nanos类型 #

支持纳秒精度:

json
{
  "mappings": {
    "properties": {
      "timestamp": {
        "type": "date_nanos",
        "format": "strict_date_optional_time||epoch_millis"
      }
    }
  }
}

五、布尔类型 #

5.1 boolean类型 #

json
{
  "mappings": {
    "properties": {
      "is_active": {
        "type": "boolean"
      }
    }
  }
}

接受的值

真值 假值
true, “true” false, “false”
“1” “0”
1 0

六、二进制类型 #

6.1 binary类型 #

存储Base64编码的二进制数据:

json
{
  "mappings": {
    "properties": {
      "attachment": {
        "type": "binary"
      }
    }
  }
}

特点

  • 默认不索引
  • 默认不存储
  • 只用于存储,不用于搜索

七、范围类型 #

7.1 范围类型分类 #

类型 说明
integer_range 整数范围
long_range 长整数范围
date_range 日期范围
ip_range IP地址范围

7.2 使用示例 #

json
{
  "mappings": {
    "properties": {
      "age_range": {
        "type": "integer_range"
      },
      "valid_period": {
        "type": "date_range",
        "format": "yyyy-MM-dd"
      }
    }
  }
}

索引数据

json
{
  "age_range": {
    "gte": 18,
    "lte": 60
  },
  "valid_period": {
    "gte": "2024-01-01",
    "lte": "2024-12-31"
  }
}

范围查询

json
{
  "query": {
    "range": {
      "age_range": {
        "gte": 25,
        "lte": 35,
        "relation": "within"
      }
    }
  }
}

八、复杂类型 #

8.1 object类型 #

json
{
  "mappings": {
    "properties": {
      "user": {
        "type": "object",
        "properties": {
          "name": { "type": "text" },
          "email": { "type": "keyword" }
        }
      }
    }
  }
}

存储结构

text
文档:
{
  "user": {
    "name": "John",
    "email": "john@example.com"
  }
}

实际存储:
{
  "user.name": "John",
  "user.email": "john@example.com"
}

8.2 nested类型 #

解决对象数组的独立性问题:

json
{
  "mappings": {
    "properties": {
      "comments": {
        "type": "nested",
        "properties": {
          "user": { "type": "keyword" },
          "message": { "type": "text" }
        }
      }
    }
  }
}

问题示例

text
普通object数组问题:
文档:
{
  "comments": [
    { "user": "Alice", "message": "Good" },
    { "user": "Bob", "message": "Bad" }
  ]
}

扁平化存储:
comments.user: ["Alice", "Bob"]
comments.message: ["Good", "Bad"]

查询 user=Alice AND message=Bad 会错误匹配!

nested查询

json
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "bool": {
          "must": [
            { "term": { "comments.user": "Alice" } },
            { "match": { "comments.message": "Good" } }
          ]
        }
      }
    }
  }
}

8.3 flattened类型 #

将整个对象作为单个字段:

json
{
  "mappings": {
    "properties": {
      "labels": {
        "type": "flattened"
      }
    }
  }
}

索引数据

json
{
  "labels": {
    "env": "production",
    "app": "web",
    "version": "1.0"
  }
}

查询

json
{
  "query": {
    "term": { "labels": "production" }
  }
}

九、地理类型 #

9.1 geo_point类型 #

存储经纬度坐标:

json
{
  "mappings": {
    "properties": {
      "location": {
        "type": "geo_point"
      }
    }
  }
}

索引数据格式

json
{
  "location": {
    "lat": 40.7128,
    "lon": -74.0060
  }
}

{
  "location": "40.7128,-74.0060"
}

{
  "location": [-74.0060, 40.7128]
}

{
  "location": {
    "type": "point",
    "coordinates": [-74.0060, 40.7128]
  }
}

地理查询

json
{
  "query": {
    "geo_distance": {
      "distance": "10km",
      "location": {
        "lat": 40.7128,
        "lon": -74.0060
      }
    }
  }
}

9.2 geo_shape类型 #

存储地理形状:

json
{
  "mappings": {
    "properties": {
      "area": {
        "type": "geo_shape"
      }
    }
  }
}

索引数据

json
{
  "area": {
    "type": "polygon",
    "coordinates": [[
      [-74.0060, 40.7128],
      [-74.0060, 40.8128],
      [-73.9060, 40.8128],
      [-73.9060, 40.7128],
      [-74.0060, 40.7128]
    ]]
  }
}

十、专用类型 #

10.1 IP类型 #

json
{
  "mappings": {
    "properties": {
      "ip_address": {
        "type": "ip"
      }
    }
  }
}

索引数据

json
{
  "ip_address": "192.168.1.1"
}

{
  "ip_address": "2001:db8::1"
}

CIDR查询

json
{
  "query": {
    "term": {
      "ip_address": "192.168.0.0/16"
    }
  }
}

10.2 completion类型 #

用于自动补全:

json
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion",
        "analyzer": "standard"
      }
    }
  }
}

索引数据

json
{
  "suggest": {
    "input": ["iPhone", "iPad", "iMac"],
    "weight": 10
  }
}

10.3 token_count类型 #

统计分词数量:

json
{
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      },
      "content_length": {
        "type": "token_count",
        "analyzer": "standard"
      }
    }
  }
}

10.4 alias类型 #

字段别名:

json
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "heading": {
        "type": "alias",
        "path": "title"
      }
    }
  }
}

10.5 join类型 #

父子关系:

json
{
  "mappings": {
    "properties": {
      "relation": {
        "type": "join",
        "relations": {
          "question": "answer"
        }
      }
    }
  }
}

十一、数组类型 #

11.1 数组特性 #

Elasticsearch没有专门的数组类型,任何字段都可以包含多个值:

json
{
  "tags": ["tag1", "tag2", "tag3"],
  "scores": [85, 90, 78],
  "dates": ["2024-01-01", "2024-01-02"]
}

11.2 数组映射 #

json
{
  "mappings": {
    "properties": {
      "tags": {
        "type": "keyword"
      }
    }
  }
}

十二、多字段(multi-fields) #

12.1 多字段定义 #

json
{
  "mappings": {
    "properties": {
      "city": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          },
          "raw": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

12.2 使用场景 #

text
多字段应用
├── 全文搜索 + 精确匹配
│   └── text + keyword
├── 不同分词器
│   └── text + text(不同analyzer)
└── 不同精度
    └── date + date(不同format)

十三、类型选择建议 #

13.1 字符串选择 #

场景 类型
全文搜索 text
精确匹配、排序、聚合 keyword
两者都需要 text + fields.keyword

13.2 数值选择 #

场景 类型
整数计数 integer
大整数ID long
价格 scaled_float
百分比 half_float
科学计算 double

13.3 日期选择 #

场景 类型
一般日期 date
高精度时间戳 date_nanos

十四、总结 #

本章介绍了Elasticsearch的数据类型:

  1. text用于全文搜索,keyword用于精确匹配
  2. 数值类型根据范围和精度选择
  3. nested解决对象数组独立性问题
  4. geo_point和geo_shape用于地理数据
  5. 多字段实现同一字段多种用途
  6. 数组无需特殊定义,所有字段都支持

下一步,我们将学习索引操作和映射管理。

最后更新:2026-03-27