> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 类型映射

> ClickHouse 与 Elasticsearch 之间的类型映射

> [!NOTE]
>
> 本页介绍的是 **Self-managed ClickHouse Observability**。全托管的 ClickHouse Observability 是推荐的默认方案，会自动为您处理数据库 schema 和类型映射。迁移时无需配置这些内部结构，请参阅 [Elasticsearch 迁移指南](https://clickhouse.com/docs/zh/clickstack/observability/migration/elastic.md) 开始使用。

Elasticsearch 和 ClickHouse 都支持丰富的数据类型，但二者的底层存储和查询模型存在根本差异。本节列出常用 Elasticsearch 字段类型在 ClickHouse 中的等价类型 (如有) ，并提供相关背景信息，为迁移提供参考。如果没有等价类型，则会在注释中给出替代方案或说明。

| **Elasticsearch 类型** | **ClickHouse 等价类型** | **说明** |
| - | - | - |
| `boolean` | [`UInt8`](https://clickhouse.com/docs/zh/reference/data-types/int-uint)  或 [`Bool`](https://clickhouse.com/docs/zh/reference/data-types/boolean) | 在较新版本中，ClickHouse 支持将 `Boolean` 作为 `UInt8` 的别名。 |
| `keyword` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string) | 用于精确匹配过滤、分组和排序。 |
| `text` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string) | ClickHouse 的全文搜索能力有限；分词需要借助自定义逻辑实现，例如将 `tokens` 等函数与数组函数结合使用。 |
| `long` | [`Int64`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 64 位有符号整数。 |
| `integer` | [`Int32`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 32 位有符号整数。 |
| `short` | [`Int16`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 16 位有符号整数。 |
| `byte` | [`Int8`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 8 位有符号整数。 |
| `unsigned_long` | [`UInt64`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 64 位无符号整数。 |
| `double` | [`Float64`](https://clickhouse.com/docs/zh/reference/data-types/float) | 64 位浮点数。 |
| `float` | [`Float32`](https://clickhouse.com/docs/zh/reference/data-types/float) | 32 位浮点数。 |
| `half_float` | [`Float32`](https://clickhouse.com/docs/zh/reference/data-types/float) 或 [`BFloat16`](https://clickhouse.com/docs/zh/reference/data-types/float) | 最接近的等价类型。ClickHouse 没有 16 位浮点类型，但提供 `BFloat16`，它不同于 Half-float IEE-754：half-float 以较小的范围换取更高的精度，而 bfloat16 则牺牲精度以获得更宽的范围，因此更适合机器学习工作负载。 |
| `scaled_float` | [`Decimal(x, y)`](https://clickhouse.com/docs/zh/reference/data-types/decimal) | 用于存储定点数值。 |
| `date` | [`DateTime`](https://clickhouse.com/docs/zh/reference/data-types/datetime) | 等价的日期类型，精度为秒级。 |
| `date_nanos` | [`DateTime64`](https://clickhouse.com/docs/zh/reference/data-types/datetime64) | ClickHouse 通过 `DateTime64(9)` 支持纳秒级精度。 |
| `binary` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string), [`FixedString(N)`](https://clickhouse.com/docs/zh/reference/data-types/fixedstring) | 二进制字段需要进行 base64 解码。 |
| `ip` | [`IPv4`](https://clickhouse.com/docs/zh/reference/data-types/ipv4), [`IPv6`](https://clickhouse.com/docs/zh/reference/data-types/ipv6) | 提供原生 `IPv4` 和 `IPv6` 类型。 |
| `object` | [`Nested`](https://clickhouse.com/docs/zh/reference/data-types/nested-data-structures/index), [`Map`](https://clickhouse.com/docs/zh/reference/data-types/map), [`Tuple`](https://clickhouse.com/docs/zh/reference/data-types/tuple), [`JSON`](https://clickhouse.com/docs/zh/reference/data-types/newjson) | ClickHouse 可以使用 [`Nested`](https://clickhouse.com/docs/zh/reference/data-types/nested-data-structures/index) 或 [`JSON`](https://clickhouse.com/docs/zh/reference/data-types/newjson) 对类 JSON 对象进行建模。 |
| `flattened` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string) | Elasticsearch 中的 flattened 类型将整个 JSON 对象存储为单个字段，无需完整映射即可灵活、无模式地访问嵌套键。在 ClickHouse 中，可以使用 String 类型实现类似功能，但需要借助物化视图进行处理。 |
| `nested` | [`Nested`](https://clickhouse.com/docs/zh/reference/data-types/nested-data-structures/index) | 在使用 `flatten_nested=0` 的前提下，ClickHouse 的 `Nested` 列可为分组子字段提供类似的语义。 |
| `join` | NA | 没有直接的父子关系概念。由于 ClickHouse 支持跨表 join，因此无需此类型。 |
| `alias` | [`Alias`](https://clickhouse.com/docs/zh/reference/statements/create/table#alias) 列修饰符 | [支持](https://clickhouse.com/docs/zh/reference/statements/create/table.md#alias)通过字段修饰符定义别名。可以对这些别名应用函数，例如 `size String ALIAS formatReadableSize(size_bytes)` |
| `range` 类型 (`*_range`) | [`Tuple(start, end)`](https://clickhouse.com/docs/zh/reference/data-types/tuple) 或 [`Array(T)`](https://clickhouse.com/docs/zh/reference/data-types/array) | ClickHouse 没有原生的范围类型，但数值范围和日期范围可以使用 [`Tuple(start, end)`](https://clickhouse.com/docs/zh/reference/data-types/tuple) 或 [`Array`](https://clickhouse.com/docs/zh/reference/data-types/array) 结构表示。对于 IP 范围 (`ip_range`) ，可将 CIDR 值存储为 `String`，并使用 `isIPAddressInRange()` 等函数进行判断。此外，也可以考虑使用基于 `ip_trie` 的查找字典来实现高效过滤。 |
| `aggregate_metric_double` | [`AggregateFunction(...)`](https://clickhouse.com/docs/zh/reference/data-types/aggregatefunction) 和 [`SimpleAggregateFunction(...)`](https://clickhouse.com/docs/zh/reference/data-types/simpleaggregatefunction) | 使用聚合函数状态和物化视图对预聚合指标进行建模。所有聚合函数都支持聚合状态。 |
| `histogram` | [`Tuple(Array(Float64), Array(UInt64))`](https://clickhouse.com/docs/zh/reference/data-types/tuple) | 使用数组或自定义 schema 手动表示桶及其计数。 |
| `annotated-text` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string) | 没有对实体感知搜索或注解的内置支持。 |
| `completion`, `search_as_you_type` | NA | 没有原生的自动补全或建议引擎，但可以使用 `String` 和[搜索函数](https://clickhouse.com/docs/zh/reference/functions/regular-functions/string-search-functions.md)实现类似功能。 |
| `semantic_text` | NA | 没有原生语义搜索，需要生成嵌入向量并使用向量搜索。 |
| `token_count` | [`Int32`](https://clickhouse.com/docs/zh/reference/data-types/int-uint) | 在摄取时手动计算标记数量，例如结合 Materialized 列使用 `length(tokens())` 函数 |
| `dense_vector` | [`Array(Float32)`](https://clickhouse.com/docs/zh/reference/data-types/array) | 使用数组存储嵌入向量 |
| `sparse_vector` | [`Map(UInt32, Float32)`](https://clickhouse.com/docs/zh/reference/data-types/map) | 使用 Map 模拟稀疏向量。没有原生的稀疏向量支持。 |
| `rank_feature` / `rank_features` | [`Float32`](https://clickhouse.com/docs/zh/reference/data-types/float), [`Array(Float32)`](https://clickhouse.com/docs/zh/reference/data-types/array) | 不支持原生的查询时加权，但可以在评分逻辑中手动实现。 |
| `geo_point` | [`Tuple(Float64, Float64)`](https://clickhouse.com/docs/zh/reference/data-types/tuple) 或 [`Point`](https://clickhouse.com/docs/zh/reference/data-types/geo#point) | 使用 (纬度, 经度) 元组。ClickHouse 也提供 [`Point`](https://clickhouse.com/docs/zh/reference/data-types/geo#point) 类型。 |
| `geo_shape`, `shape` | [`Ring`](https://clickhouse.com/docs/zh/reference/data-types/geo#ring), [`LineString`](https://clickhouse.com/docs/zh/reference/data-types/geo#linestring), [`MultiLineString`](https://clickhouse.com/docs/zh/reference/data-types/geo#multilinestring), [`Polygon`](https://clickhouse.com/docs/zh/reference/data-types/geo#polygon), [`MultiPolygon`](https://clickhouse.com/docs/zh/reference/data-types/geo#multipolygon) | 原生支持地理形状和空间索引。 |
| `percolator` | NA | 没有为查询建立索引的概念。请改用标准 SQL + 增量物化视图。 |
| `version` | [`String`](https://clickhouse.com/docs/zh/reference/data-types/string) | ClickHouse 没有原生的版本类型。请将版本存储为字符串，如需进行语义比较，可使用自定义 UDFs 函数实现。如果需要范围查询，可考虑将其规范化为数值格式。 |

### 说明

* **数组**：在 Elasticsearch 中，所有字段都原生支持数组。在 ClickHouse 中，数组必须显式定义 (例如 `Array(String)`) ，其优势在于可以访问和查询特定位置的元素，例如 `an_array[1]`。
* **多字段 (Multi-fields) **：Elasticsearch 允许[以多种方式索引同一字段](https://www.elastic.co/docs/reference/elasticsearch/mapping-reference/multi-fields#_multi_fields_with_multiple_analyzers) (例如同时作为 `text` 和 `keyword`) 。在 ClickHouse 中，这种模式需要通过单独的列或视图来实现。
* **Map 和 JSON 类型** - 在 ClickHouse 中，[`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 类型常用于建模 `resourceAttributes` 和 `logAttributes` 等动态键值结构。该类型允许在运行时添加任意键，从而实现灵活的无 schema 摄取，其理念与 Elasticsearch 中的 JSON 对象类似。但该类型也存在以下重要限制：

  * **值类型必须统一**：ClickHouse 的 [`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 列必须使用一致的值类型 (例如 `Map(String, String)`) 。若不进行类型强制转换，则不支持混合类型的值。
  * **性能开销**：访问 [`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 中的任意键都需要将整个 Map 加载到内存中，可能影响性能。
  * **不支持子列**：与 JSON 不同，[`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 中的键并不以真正的子列形式存储，这限制了 ClickHouse 高效索引、压缩和查询的能力。

  ClickHouse 的 [`JSON`](https://clickhouse.com/docs/zh/reference/data-types/newjson) 类型可替代 [`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 来存储动态属性，并解决了上述多项限制：

  * **真正的列式存储**：每个 JSON 路径都作为子列存储，从而支持高效的压缩、过滤和向量化查询执行。
  * **支持混合类型**：不同的数据类型 (例如整数、字符串、数组) 可以在同一路径下共存，无需类型强制转换或类型统一。
  * **文件系统可扩展性**：针对动态键 (`max_dynamic_paths`) 和类型 (`max_dynamic_types`) 的内部限制可防止磁盘上的列文件数量激增，即使键集合基数很高也不例外。
  * **紧凑存储**：null 和缺失值采用稀疏方式存储，避免不必要的开销。

    [`JSON`](https://clickhouse.com/docs/zh/reference/data-types/newjson) 类型尤其适合可观测性工作负载，既具备无 schema 摄取的灵活性，又拥有 ClickHouse 原生类型的性能和可扩展性，是在动态属性字段中替代 [`Map`](https://clickhouse.com/docs/zh/reference/data-types/map) 的理想选择。

    有关 JSON 类型的更多信息，建议参阅 [JSON 指南](https://clickhouse.com/docs/zh/guides/clickhouse/data-formats/json/intro.md)以及[“How we built a new powerful JSON data type for ClickHouse”](https://clickhouse.com/blog/a-new-powerful-json-data-type-for-clickhouse)。
