!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())

[Skip to main content](#__docusaurus_skipToContent_fallback)

LangChain v0.2 is coming soon! Preview the new docs [here](/v0.2/docs/introduction/).

[

![🦜️🔗 Langchain](/v0.1/img/brand/wordmark.png)![🦜️🔗 Langchain](/v0.1/img/brand/wordmark-dark.png)

](/v0.1/)[Docs](/v0.1/docs/get_started/introduction/)[Use cases](/v0.1/docs/use_cases/)[Integrations](/v0.1/docs/integrations/platforms/)[API Reference](https://api.js.langchain.com)

[More](#)

*   [People](/v0.1/docs/people/)
*   [Community](/v0.1/docs/community/)
*   [Tutorials](/v0.1/docs/additional_resources/tutorials/)
*   [Contributing](/v0.1/docs/contributing/)

[v0.1](#)

*   [v0.2](https://js.langchain.com/v0.2/docs/introduction)
*   [v0.1](/v0.1/docs/get_started/introduction/)

[🦜🔗](#)

*   [LangSmith](https://smith.langchain.com)
*   [LangSmith Docs](https://docs.smith.langchain.com)
*   [LangChain Hub](https://smith.langchain.com/hub)
*   [LangServe](https://github.com/langchain-ai/langserve)
*   [Python Docs](https://python.langchain.com/)

[Chat](https://chatjs.langchain.com)[](https://github.com/langchain-ai/langchainjs)

Search

*   [Providers](/v0.1/docs/integrations/platforms/)
    
    *   [Providers](/v0.1/docs/integrations/platforms/)
    *   [Anthropic](/v0.1/docs/integrations/platforms/anthropic/)
    *   [AWS](/v0.1/docs/integrations/platforms/aws/)
    *   [Google](/v0.1/docs/integrations/platforms/google/)
    *   [Microsoft](/v0.1/docs/integrations/platforms/microsoft/)
    *   [OpenAI](/v0.1/docs/integrations/platforms/openai/)
*   [Components](/v0.1/docs/integrations/components/)
    
    *   [LLMs](/v0.1/docs/integrations/llms/)
        
    *   [Chat models](/v0.1/docs/integrations/chat/)
        
    *   [Document loaders](/v0.1/docs/integrations/document_loaders/)
        
    *   [Document transformers](/v0.1/docs/integrations/document_transformers/)
        
    *   [Document compressors](/v0.1/docs/integrations/document_compressors/)
        
    *   [Text embedding models](/v0.1/docs/integrations/text_embedding/)
        
    *   [Vector stores](/v0.1/docs/integrations/vectorstores/)
        
        *   [Memory](/v0.1/docs/integrations/vectorstores/memory/)
        *   [AnalyticDB](/v0.1/docs/integrations/vectorstores/analyticdb/)
        *   [Astra DB](/v0.1/docs/integrations/vectorstores/astradb/)
        *   [Azure AI Search](/v0.1/docs/integrations/vectorstores/azure_aisearch/)
        *   [Azure Cosmos DB](/v0.1/docs/integrations/vectorstores/azure_cosmosdb/)
        *   [Cassandra](/v0.1/docs/integrations/vectorstores/cassandra/)
        *   [Chroma](/v0.1/docs/integrations/vectorstores/chroma/)
        *   [ClickHouse](/v0.1/docs/integrations/vectorstores/clickhouse/)
        *   [CloseVector](/v0.1/docs/integrations/vectorstores/closevector/)
        *   [Cloudflare Vectorize](/v0.1/docs/integrations/vectorstores/cloudflare_vectorize/)
        *   [Convex](/v0.1/docs/integrations/vectorstores/convex/)
        *   [Couchbase](/v0.1/docs/integrations/vectorstores/couchbase/)
        *   [Elasticsearch](/v0.1/docs/integrations/vectorstores/elasticsearch/)
        *   [Faiss](/v0.1/docs/integrations/vectorstores/faiss/)
        *   [Google Vertex AI Matching Engine](/v0.1/docs/integrations/vectorstores/googlevertexai/)
        *   [SAP HANA Cloud Vector Engine](/v0.1/docs/integrations/vectorstores/hanavector/)
        *   [HNSWLib](/v0.1/docs/integrations/vectorstores/hnswlib/)
        *   [LanceDB](/v0.1/docs/integrations/vectorstores/lancedb/)
        *   [Milvus](/v0.1/docs/integrations/vectorstores/milvus/)
        *   [Momento Vector Index (MVI)](/v0.1/docs/integrations/vectorstores/momento_vector_index/)
        *   [MongoDB Atlas](/v0.1/docs/integrations/vectorstores/mongodb_atlas/)
        *   [MyScale](/v0.1/docs/integrations/vectorstores/myscale/)
        *   [Neo4j Vector Index](/v0.1/docs/integrations/vectorstores/neo4jvector/)
        *   [Neon Postgres](/v0.1/docs/integrations/vectorstores/neon/)
        *   [OpenSearch](/v0.1/docs/integrations/vectorstores/opensearch/)
        *   [PGVector](/v0.1/docs/integrations/vectorstores/pgvector/)
        *   [Pinecone](/v0.1/docs/integrations/vectorstores/pinecone/)
        *   [Prisma](/v0.1/docs/integrations/vectorstores/prisma/)
        *   [Qdrant](/v0.1/docs/integrations/vectorstores/qdrant/)
        *   [Redis](/v0.1/docs/integrations/vectorstores/redis/)
        *   [Rockset](/v0.1/docs/integrations/vectorstores/rockset/)
        *   [SingleStore](/v0.1/docs/integrations/vectorstores/singlestore/)
        *   [Supabase](/v0.1/docs/integrations/vectorstores/supabase/)
        *   [Tigris](/v0.1/docs/integrations/vectorstores/tigris/)
        *   [Turbopuffer](/v0.1/docs/integrations/vectorstores/turbopuffer/)
        *   [TypeORM](/v0.1/docs/integrations/vectorstores/typeorm/)
        *   [Typesense](/v0.1/docs/integrations/vectorstores/typesense/)
        *   [Upstash Vector](/v0.1/docs/integrations/vectorstores/upstash/)
        *   [USearch](/v0.1/docs/integrations/vectorstores/usearch/)
        *   [Vectara](/v0.1/docs/integrations/vectorstores/vectara/)
        *   [Vercel Postgres](/v0.1/docs/integrations/vectorstores/vercel_postgres/)
        *   [Voy](/v0.1/docs/integrations/vectorstores/voy/)
        *   [Weaviate](/v0.1/docs/integrations/vectorstores/weaviate/)
        *   [Xata](/v0.1/docs/integrations/vectorstores/xata/)
        *   [Zep](/v0.1/docs/integrations/vectorstores/zep/)
    *   [Retrievers](/v0.1/docs/integrations/retrievers/)
        
    *   [Tools](/v0.1/docs/integrations/tools/)
        
    *   [Agents and toolkits](/v0.1/docs/integrations/toolkits/)
        
    *   [Chat Memory](/v0.1/docs/integrations/chat_memory/)
        
    *   [Stores](/v0.1/docs/integrations/stores/)
        

*   [](/v0.1/)
*   [Components](/v0.1/docs/integrations/components/)
*   [Vector stores](/v0.1/docs/integrations/vectorstores/)
*   Cassandra

On this page

Cassandra
=========

Compatibility

Only available on Node.js.

[Apache Cassandra®](https://cassandra.apache.org/_/index.html) is a NoSQL, row-oriented, highly scalable and highly available database.

The [latest version](https://cwiki.apache.org/confluence/display/CASSANDRA/CEP-30%3A+Approximate+Nearest+Neighbor(ANN)+Vector+Search+via+Storage-Attached+Indexes) of Apache Cassandra natively supports Vector Similarity Search.

Setup[​](#setup "Direct link to Setup")
---------------------------------------

First, install the Cassandra Node.js driver:

tip

See [this section for general instructions on installing integration packages](/v0.1/docs/get_started/installation/#installing-integration-packages).

*   npm
*   Yarn
*   pnpm

    npm install cassandra-driver @langchain/community @langchain/openai

    yarn add cassandra-driver @langchain/community @langchain/openai

    pnpm add cassandra-driver @langchain/community @langchain/openai

Depending on your database providers, the specifics of how to connect to the database will vary. We will create a document `configConnection` which will be used as part of the vector store configuration.

### Apache Cassandra®[​](#apache-cassandra "Direct link to Apache Cassandra®")

Vector search is supported in [Apache Cassandra® 5.0](https://cassandra.apache.org/_/Apache-Cassandra-5.0-Moving-Toward-an-AI-Driven-Future.html) and above. You can use a standard connection document, for example:

    const configConnection = {  contactPoints: ['h1', 'h2'],  localDataCenter: 'datacenter1',  credentials: {    username: <...> as string,    password: <...> as string,  },};

### Astra DB[​](#astra-db "Direct link to Astra DB")

Astra DB is a cloud-native Cassandra-as-a-Service platform.

1.  Create an [Astra DB account](https://astra.datastax.com/register).
2.  Create a [vector enabled database](https://astra.datastax.com/createDatabase).
3.  Create a [token](https://docs.datastax.com/en/astra/docs/manage-application-tokens.html) for your database.

    const configConnection = {  serviceProviderArgs: {    astra: {      token: <...> as string,      endpoint: <...> as string,    },  },};

Instead of `endpoint:`, you many provide property `datacenterID:` and optionally `regionName:`.

Indexing docs[​](#indexing-docs "Direct link to Indexing docs")
---------------------------------------------------------------

    import { CassandraStore } from "langchain/vectorstores/cassandra";import { OpenAIEmbeddings } from "@langchain/openai";// The configConnection document is defined aboveconst config = {  ...configConnection,  keyspace: "test",  dimensions: 1536,  table: "test",  indices: [{ name: "name", value: "(name)" }],  primaryKey: {    name: "id",    type: "int",  },  metadataColumns: [    {      name: "name",      type: "text",    },  ],};const vectorStore = await CassandraStore.fromTexts(  ["I am blue", "Green yellow purple", "Hello there hello"],  [    { id: 2, name: "2" },    { id: 1, name: "1" },    { id: 3, name: "3" },  ],  new OpenAIEmbeddings(),  cassandraConfig);

Querying docs[​](#querying-docs "Direct link to Querying docs")
---------------------------------------------------------------

    const results = await vectorStore.similaritySearch("Green yellow purple", 1);

or filtered query:

    const results = await vectorStore.similaritySearch("B", 1, { name: "Bubba" });

Vector Types[​](#vector-types "Direct link to Vector Types")
------------------------------------------------------------

Cassandra supports `cosine` (the default), `dot_product`, and `euclidean` similarity search; this is defined when the vector store is first created, and specifed in the constructor parameter `vectorType`, for example:

      ...,  vectorType: "dot_product",  ...

Indices[​](#indices "Direct link to Indices")
---------------------------------------------

With Version 5, Cassandra introduced Storage Attached Indexes, or SAIs. These allow `WHERE` filtering without specifying the partition key, and allow for additional operator types such as non-equalities. You can define these with the `indices` parameter, which accepts zero or more dictionaries each containing `name` and `value` entries.

Indices are optional, though required if using filtered queries on non-partition columns.

*   The `name` entry is part of the object name; on a table named `test_table` an index with `name: "some_column"` would be `idx_test_table_some_column`.
*   The `value` entry is the column on which the index is created, surrounded by `(` and `)`. With the above column `some_column` it would be specified as `value: "(some_column)"`.
*   An optional `options` entry is a map passed to the `WITH OPTIONS =` clause of the `CREATE CUSTOM INDEX` statement. The specific entries on this map are index type specific.

      indices: [{ name: "some_column", value: "(some_column)" }],

Advanced Filtering[​](#advanced-filtering "Direct link to Advanced Filtering")
------------------------------------------------------------------------------

By default, filters are applied with an equality `=`. For those fields that have an `indices` entry, you may provide an `operator` with a string of a value supported by the index; in this case, you specify one or more filters, as either a singleton or in a list (which will be `AND`\-ed together). For example:

       { name: "create_datetime", operator: ">", value: some_datetime_variable }

or

    [  { userid: userid_variable },  { name: "create_datetime", operator: ">", value: some_date_variable },];

`value` can be a single value or an array. If it is not an array, or there is only one element in `value`, the resulting query will be along the lines of `${name} ${operator} ?` with `value` bound to the `?`.

If there is more than one element in the `value` array, the number of unquoted `?` in `name` are counted and subtracted from the length of `value`, and this number of `?` is put on the right side of the operator; if there are more than one `?` then they will be encapsulated in `(` and `)`, e.g. `(?, ?, ?)`.

This faciliates bind values on the left of the operator, which is useful for some functions; for example a geo-distance filter:

    {  name: "GEO_DISTANCE(coord, ?)",  operator: "<",  value: [new Float32Array([53.3730617,-6.3000515]), 10000],},

Data Partitioning and Composite Keys[​](#data-partitioning-and-composite-keys "Direct link to Data Partitioning and Composite Keys")
------------------------------------------------------------------------------------------------------------------------------------

In some systems, you may wish to partition the data for various reasons, perhaps by user or by session. Data in Cassandra is always partitioned; by default this library will partition by the first primary key field. You may specify multiple columns which comprise the primary (unique) key of a record, and optionally indicate those fields which should be part of the partition key. For example, the vector store could be partitioned by both `userid` and `collectionid`, with additional fields `docid` and `docpart` making an individual entry unique:

      ...,  primaryKey: [    {name: "userid", type: "text", partition: true},    {name: "collectionid", type: "text", partition: true},    {name: "docid", type: "text"},    {name: "docpart", type: "int"},  ],  ...

When searching, you may include partition keys on the filter without defining `indices` for these columns; you do not need to specify all partition keys, but must specify those in the key first. In the above example, you could specify a filter of `{userid: userid_variable}` and `{userid: userid_variable, collectionid: collectionid_variable}`, but if you wanted to specify a filter of only `{collectionid: collectionid_variable}` you would have to include `collectionid` on the `indices` list.

Additional Configuration Options[​](#additional-configuration-options "Direct link to Additional Configuration Options")
------------------------------------------------------------------------------------------------------------------------

In the configuration document, further optional parameters are provided; their defaults are:

      ...,  maxConcurrency: 25,  batchSize: 1,  withClause: "",  ...

Parameter

Usage

`maxConcurrency`

How many concurrent requests will be sent to Cassandra at a given time.

`batchSize`

How many documents will be sent on a single request to Cassandra. When using a value > 1, you should ensure your batch size will not exceed the Cassandra parameter `batch_size_fail_threshold_in_kb`. Batches are unlogged.

`withClause`

Cassandra tables may be created with an optional `WITH` clause; this is generally not needed but provided for completeness.

* * *

#### Help us out by providing feedback on this documentation page:

[

Previous

Azure Cosmos DB

](/v0.1/docs/integrations/vectorstores/azure_cosmosdb/)[

Next

Chroma

](/v0.1/docs/integrations/vectorstores/chroma/)

*   [Setup](#setup)
    *   [Apache Cassandra®](#apache-cassandra)
    *   [Astra DB](#astra-db)
*   [Indexing docs](#indexing-docs)
*   [Querying docs](#querying-docs)
*   [Vector Types](#vector-types)
*   [Indices](#indices)
*   [Advanced Filtering](#advanced-filtering)
*   [Data Partitioning and Composite Keys](#data-partitioning-and-composite-keys)
*   [Additional Configuration Options](#additional-configuration-options)

Community

*   [Discord](https://discord.gg/cU2adEyC7w)
*   [Twitter](https://twitter.com/LangChainAI)

GitHub

*   [Python](https://github.com/langchain-ai/langchain)
*   [JS/TS](https://github.com/langchain-ai/langchainjs)

More

*   [Homepage](https://langchain.com)
*   [Blog](https://blog.langchain.dev)

Copyright © 2024 LangChain, Inc.