> ## 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.

# Elasticsearch 表引擎

> 该引擎支持从 Elasticsearch 索引中读取文档。

[Experimental 功能](https://clickhouse.com/docs/reference/settings/beta-and-experimental-features.md#experimental-features)

`Elasticsearch` 引擎支持从 [Elasticsearch](https://www.elastic.co/elasticsearch) 的索引中读取文档。每个文档以一行的形式返回：其 `_source` 作为单个 [JSON](https://clickhouse.com/docs/zh/reference/data-types/newjson.md) 类型的列公开，文档 id 和索引名称则以虚拟列的形式提供。

该引擎为只读引擎。文档通过 `_search` API 分页拉取，并借助[时间点](https://www.elastic.co/docs/reference/elasticsearch/rest-apis/paginate-search-results#search-after) (时间点) 机制，确保即使索引正在被修改，查询看到的也始终是该索引的一致性快照。

要求 Elasticsearch 7.12 或更高版本：该引擎依赖时间点 API 和 `_shard_doc` 排序字段。不支持 OpenSearch，因为它没有 `_shard_doc` 字段。如果服务器版本较旧，第一个请求就会被拒绝并返回错误。

## 创建表

```sql theme={null}
CREATE TABLE [IF NOT EXISTS] [db.]table_name
[(
    name1 JSON
)]
ENGINE = Elasticsearch('url', 'index'[, 'user', 'password'][, query('...')][, key = value, ...])
[SETTINGS keep_alive = 60, page_size = 10000]
```

> [!NOTE]
>
> Elasticsearch 集成目前处于实验性阶段。请通过 [`enable_elasticsearch`](https://clickhouse.com/docs/zh/reference/settings/session-settings/enable#enable_elasticsearch) 设置启用：
>
> ```sql theme={null}
> SET enable_elasticsearch = 1;
> ```

列列表是可选的。如果省略，表将只包含一个类型为 `JSON` 的列 `document`。如果指定，则必须恰好包含一列，且其类型必须为 `JSON`，可以附带类型提示和跳过路径。不支持 `MATERIALIZED`、`ALIAS` 和 `EPHEMERAL` 列，且名称 `_id` 和 `_index` 保留给虚拟列使用。

**引擎参数**

* `url` — Elasticsearch 集群的基础 URL，例如 `https://localhost:9200`。必须是 `http` 或 `https` URL，且不能包含凭证、查询字符串或片段。
* `index` — 要读取的索引。也可以是别名、数据流、逗号分隔的列表、通配符模式，或日期运算表达式 (例如 `<logs-{now/d}>`) ，只要 Elasticsearch 时间点 API 能够接受即可。该值为字面名称，不得进行 URL 编码。如果模式未匹配到任何索引，则会报错。
* `user`、`password` — 用于 HTTP 基本身份验证的可选凭证。
* `query('...')` — 可选的 JSON 格式 Elasticsearch [Query DSL](https://www.elastic.co/docs/explore-analyze/query-filter/languages/querydsl) 对象，用于筛选要读取的文档。它必须位于位置参数之后。默认值：`{"match_all": {}}`。

这些参数也可以通过[命名集合](https://clickhouse.com/docs/zh/concepts/features/configuration/server-config/named-collections.md)传递，并使用 `key = value` 覆盖其中的值。有关完整的键列表以及[身份验证方法](https://clickhouse.com/docs/zh/reference/functions/table-functions/elasticsearch.md#authentication)的说明，请参阅 [`elasticsearch` 表函数](https://clickhouse.com/docs/zh/reference/functions/table-functions/elasticsearch#arguments)。

**引擎设置**

| 设置 | 默认值 | 描述 |
| - | - | - |
| `keep_alive` | `60` | 在两次连续的分页请求之间，Elasticsearch 保持时间点处于打开状态的时长 (以秒为单位) 。 |
| `page_size` | `10000` | 每次请求从 Elasticsearch 获取的文档数量。每一页对应一个块，因此该设置同时决定了读取时的块大小，[`max_block_size`](https://clickhouse.com/docs/zh/reference/settings/session-settings/max#max_block_size) 对此不起作用。若该值超过 Elasticsearch 的索引设置 `index.max_result_window` (默认为 `10000`) ，请求将被 Elasticsearch 拒绝。 |

这些设置无法通过 `ALTER TABLE ... MODIFY SETTING` 修改。如需更改，请重新创建表。

## 虚拟列

* `_id` — 文档 ID。类型：`String`。
* `_index` — 该文档所在索引的名称。类型：`LowCardinality(String)`。

当查询仅读取虚拟列时，不会向 Elasticsearch 请求文档的 `_source`。对于不存储 `_source` 的索引 (即其映射中禁用了 `_source` 字段) ，只能通过虚拟列进行读取：若查询从此类索引中读取 `JSON` 列，将直接报错，而不会返回空文档。

## 使用示例

```sql theme={null}
CREATE TABLE logs
ENGINE = Elasticsearch('https://localhost:9200', 'logs-*', 'elastic', 'changeme')
SETTINGS page_size = 5000;

SELECT _index, _id, document.message
FROM logs
WHERE document.level = 'error'
LIMIT 10;
```

使用 Query DSL 对象在 Elasticsearch 端进行过滤：

```sql theme={null}
CREATE TABLE errors_since_october
ENGINE = Elasticsearch('https://localhost:9200', 'logs-*',
                       query('{"bool": {"filter": [{"term": {"level": "error"}}, {"range": {"@timestamp": {"gte": "2026-10-01T00:00:00Z"}}}]}}'),
                       api_key = '...');
```

请在 `query` 中使用绝对日期：`now-1h` 这类日期运算会在每次翻页时重新求值，因此可能会遗漏文档。详见 [`elasticsearch` 表函数的限制](https://clickhouse.com/docs/zh/reference/functions/table-functions/elasticsearch#limitations)。

## 相关内容

* [`elasticsearch` 表函数](https://clickhouse.com/docs/zh/reference/functions/table-functions/elasticsearch)
