# RedisVectorStore

> **Class** in `langchain_redis`

📖 [View in docs](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore)

Redis vector store integration.

## Signature

```python
RedisVectorStore(
    self,
    embeddings: Embeddings,
    config: Optional[RedisConfig] = None,
    ttl: Optional[int] = None,
    **kwargs: Any = {},
)
```

## Description

**Setup:**

Install ``langchain-redis`` and running the Redis docker container.

.. code-block:: bash

    pip install -qU langchain-redis
    docker run -p 6379:6379 redis/redis-stack-server:latest

Key init args — indexing params:
    index_name: str
        Name of the index to create.
    embedding: Embeddings
        Embedding function to use.
    distance_metric: str
        Distance metric to use for similarity search. Default is "COSINE".
    indexing_algorithm: str
        Indexing algorithm to use. Default is "FLAT".
    vector_datatype: str
        Data type of the vector. Default is "FLOAT32".

Key init args — client params:
    redis_url: Optional[str]
        URL of the Redis instance to connect to.
    redis_client: Optional[Redis]
        Pre-existing Redis connection.
    ttl: Optional[int]
        Time-to-live for the Redis keys.

**Instantiate:**

.. code-block:: python

from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings

vector_store = RedisVectorStore(
    index_name="langchain-demo",
    embedding=OpenAIEmbeddings(),
    redis_url="redis://localhost:6379",
)

You can also connect to an existing Redis instance by passing in a
pre-existing Redis connection via the redis_client argument.

**Instantiate from existing connection:**

.. code-block:: python

from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
from redis import Redis

redis_client = Redis.from_url("redis://localhost:6379")

store = RedisVectorStore(
    embedding=OpenAIEmbeddings(),
    index_name="langchain-demo",
    redis_client=redis_client
)

**Add Documents:**

.. code-block:: python

from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="bar", metadata={"foo": "baz"})
document_3 = Document(page_content="to be deleted")

documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)

**Delete Documents:**

.. code-block:: python

vector_store.delete(ids=["3"])

**Search:**

.. code-block:: python

    results = vector_store.similarity_search(query="foo", k=1)
    for doc in results:
        print(f"* {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * foo [{'baz': 'bar'}]

**Search with filter:**

.. code-block:: python

    from redisvl.query.filter import Tag

    results = vector_store.similarity_search(
        query="foo",
        k=1,
        filter=Tag("baz") == "bar"
    )
    for doc in results:
        print(f"* {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * foo [{'baz': 'bar'}]

**Search with score:**

.. code-block:: python

    results = vector_store.similarity_search_with_score(query="foo", k=1)
    for doc, score in results:
        print(f"* [SIM={score:.3f}] {doc.page_content} [{doc.metadata}]")

.. code-block:: python

    * [SIM=0.916] foo [{'baz': 'bar'}]

**Use as Retriever:**

.. code-block:: python

    retriever = vector_store.as_retriever(
        search_type="mmr",
        search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
    )
    retriever.get_relevant_documents("foo")

.. code-block:: python

    [Document(page_content='foo', metadata={'baz': 'bar'})]

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `embeddings` | `Embeddings` | Yes | The Embeddings instance used for this store. |
| `config` | `Optional[RedisConfig]` | No | Optional RedisConfig object. If not provided, a new one will be created from kwargs. (default: `None`) |
| `ttl` | `Optional[int]` | No | Optional time-to-live for Redis keys. (default: `None`) |
| `**kwargs` | `Any` | No | Additional keyword arguments for RedisConfig if config is not provided. (default: `{}`) |

## Extends

- `VectorStore`

## Constructors

```python
__init__(
    self,
    embeddings: Embeddings,
    config: Optional[RedisConfig] = None,
    ttl: Optional[int] = None,
    **kwargs: Any = {},
)
```

| Name | Type |
|------|------|
| `embeddings` | `Embeddings` |
| `config` | `Optional[RedisConfig]` |
| `ttl` | `Optional[int]` |


## Properties

- `config`
- `ttl`
- `index`
- `embeddings`
- `key_prefix`

## Methods

- [`add_texts()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/add_texts)
- [`from_texts()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/from_texts)
- [`from_documents()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/from_documents)
- [`from_existing_index()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/from_existing_index)
- [`delete()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/delete)
- [`similarity_search_by_vector()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/similarity_search_by_vector)
- [`similarity_search()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/similarity_search)
- [`convert_vector()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/convert_vector)
- [`similarity_search_with_score_by_vector()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/similarity_search_with_score_by_vector)
- [`similarity_search_with_score()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/similarity_search_with_score)
- [`max_marginal_relevance_search_by_vector()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/max_marginal_relevance_search_by_vector)
- [`max_marginal_relevance_search()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/max_marginal_relevance_search)
- [`get_by_ids()`](https://reference.langchain.com/python/langchain-redis/vectorstores/RedisVectorStore/get_by_ids)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-redis/blob/ae0db7690f041437d662d0810523cda4cb7f5174/libs/redis/langchain_redis/vectorstores.py#L140)