Skip to main content
Allows SELECT queries on documents of an index in Elasticsearch. Each document is returned as one row: its _source is exposed as a single column of the JSON type, and the document id and index name are available as virtual columns. The function is read-only.

Syntax

The Elasticsearch integration is experimental. Enable it with the enable_elasticsearch setting:

Arguments

The url, index, user and password arguments can also be given in the key = value form; positional arguments fill these slots in this order, and specifying an argument both positionally and as a key (or the same key twice) is an error. The following arguments can be specified in the key = value form (or as keys of a named collection): In addition, the table function accepts: The read settings keep_alive and page_size can be passed in a trailing SETTINGS clause; see the Elasticsearch table engine for their description.

Authentication

At most one authentication method can be provided:
  1. Basic authentication. Pass user and password.
  2. Bearer token. Pass bearer_token.
  3. API key. Pass api_key.
Without any of them the request is sent anonymously, which only works when security is disabled in Elasticsearch. Store credentials in a named collection to avoid specifying them in each query. A permanent table created from a named collection (with the Elasticsearch table engine or CREATE TABLE ... AS elasticsearch(...)) is registered as a dependency of the collection, so DROP NAMED COLLECTION is blocked while the table exists.

Virtual columns

  • _id — The document id. Type: String.
  • _index — The name of the index the document was read from. Type: LowCardinality(String).
When a query reads only virtual columns, the document _source is not requested from Elasticsearch. An index that does not store _source (the _source field is disabled in its mapping) can be read only through the virtual columns: a query that reads the JSON column from such an index fails with an error instead of returning empty documents.

Examples

Count the documents of an index:
Read fields of the documents as subcolumns of the JSON column:
Filter on the Elasticsearch side with a Query DSL object and tune the page size:
Use a named collection:

Limitations

  • The function is read-only; INSERT is not supported.
  • Predicates are not pushed down. A WHERE condition is applied in ClickHouse after the documents have been downloaded; use the query argument to filter on the Elasticsearch side.
  • The query is sent with the size of a page. A query whose result depends on size, such as a knn query without k, returns a set of documents that depends on page_size. Specify such parameters explicitly.
  • Each page is a separate search request, and Elasticsearch evaluates date math such as now-1h in the query anew for each request. The point in time freezes the documents but not the clock, so a time-dependent query can silently skip documents near the moving boundary. Use absolute dates in the query.
  • Each read opens a point in time, pages through the results with search_after sorted by _shard_doc, and closes the point in time at the end. A page request that fails on any shard, or that times out in Elasticsearch, raises an error instead of returning partial results.
  • Elasticsearch 7.12 or later is required: the function relies on the point in time API and on the _shard_doc sort field, so older versions and OpenSearch reject the first request.
  • page_size cannot exceed the index.max_result_window index setting of Elasticsearch (10000 by default).