Skip to main content
ClickHouse is the fastest and most resource efficient open-source database for real-time apps and analytics with full SQL support and a wide range of functions to assist users in writing analytical queries. Lately added data structures and distance search functions (like L2Distance) as well as approximate nearest neighbor search indexes enable ClickHouse to be used as a high performance and scalable vector database to store and search vectors with SQL.
This notebook shows how to use functionality related to the ClickHouse vector store.

Setup

First set up a local clickhouse server with docker:
You’ll need to install langchain-community and clickhouse-connect to use this integration

Credentials

There are no credentials for this notebook, just make sure you have installed the packages as shown above. If you want to get best in-class automated tracing of your model calls you can also set your LangSmith API key by uncommenting below:

Instantiation

Manage vector store

Once you have created your vector store, we can interact with it by adding and deleting different items.

Add items to vector store

We can add items to our vector store by using the add_documents function.

Delete items from vector store

We can delete items from our vector store by ID by using the delete function.

Query vector store

Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent.

Query directly

Performing a simple similarity search can be done as follows:

Similarity search with score

You can also search with score:

Filtering

You can have direct access to ClickHouse SQL where statement. You can write WHERE clause following standard SQL. NOTE: Please be aware of SQL injection, this interface must not be directly called by end-user. If you custimized your column_map under your setting, you search with filter like this:

Other search methods

There are a variety of other search methods that are not covered in this notebook, such as MMR search or searching by vector. For a full list of the search abilities available for Clickhouse vector store check out the API reference.

Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains. Here is how to transform your vector store into a retriever and then invoke the retreiever with a simple query and filter.

Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections: For more, check out a complete RAG template using Astra DB here.

API reference

For detailed documentation of all Clickhouse features and configurations head to the API reference.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.