> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-opensw-1782332329-96d87c7.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenGauss VectorStore integration

> Integrate with the OpenGauss VectorStore using LangChain Python.

This notebook covers how to get started with the openGauss VectorStore. [openGauss](https://opengauss.org/en/) is a high-performance relational database with native vector storage and retrieval capabilities. This integration enables ACID-compliant vector operations within LangChain applications, combining traditional SQL functionality with modern AI-driven similarity search.
vector store.

## Setup

### Launch openGauss Container

```bash theme={null}
docker run --name opengauss \
  -d \
  -e GS_PASSWORD='MyStrongPass@123' \
  -p 8888:5432 \
  opengauss/opengauss-server:latest
```

### Install langchain-opengauss

```bash theme={null}
pip install langchain-opengauss
```

**System Requirements**:

* openGauss ≥ 7.0.0
* Python ≥ 3.8
* psycopg2-binary

### Credentials

Using your openGauss Credentials

## Initialization

<EmbeddingTabs />

```python theme={null}
from langchain_opengauss import OpenGauss, OpenGaussSettings

# Configure with schema validation
config = OpenGaussSettings(
    table_name="test_langchain",
    embedding_dimension=384,
    index_type="HNSW",
    distance_strategy="COSINE",
)
vector_store = OpenGauss(embedding=embeddings, config=config)
```

## Manage vector store

### Add items to vector store

```python theme={null}
from langchain_core.documents import Document

document_1 = Document(page_content="foo", metadata={"source": "https://example.com"})

document_2 = Document(page_content="bar", metadata={"source": "https://example.com"})

document_3 = Document(page_content="baz", metadata={"source": "https://example.com"})

documents = [document_1, document_2, document_3]

vector_store.add_documents(documents=documents, ids=["1", "2", "3"])
```

### Update items in vector store

```python theme={null}
updated_document = Document(
    page_content="qux", metadata={"source": "https://another-example.com"}
)

# If the id is already exist, will update the document
vector_store.add_documents(document_id="1", document=updated_document)
```

### Delete items from vector store

```python theme={null}
vector_store.delete(ids=["3"])
```

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

* TODO: Edit and then run code cell to generate output

```python theme={null}
results = vector_store.similarity_search(
    query="thud", k=1, filter={"source": "https://another-example.com"}
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
```

If you want to execute a similarity search and receive the corresponding scores you can run:

```python theme={null}
results = vector_store.similarity_search_with_score(
    query="thud", k=1, filter={"source": "https://example.com"}
)
for doc, score in results:
    print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
```

### Query by turning into retriever

You can also transform the vector store into a retriever for easier usage in your chains.

* TODO: Edit and then run code cell to generate output

```python theme={null}
retriever = vector_store.as_retriever(search_type="mmr", search_kwargs={"k": 1})
retriever.invoke("thud")
```

## Usage for retrieval-augmented generation

For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:

* [Retrieval docs](/oss/python/langchain/retrieval)
* [Build a RAG app with LangChain](/oss/python/langchain/rag)
* [Agentic RAG](/oss/python/langgraph/agentic-rag)

## Configuration

### Connection settings

| Parameter             | Default                 | Description                                                                                                                                                                                                                                                         |
| --------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `host`                | localhost               | Database server address                                                                                                                                                                                                                                             |
| `port`                | 8888                    | Database connection port                                                                                                                                                                                                                                            |
| `user`                | gaussdb                 | Database username                                                                                                                                                                                                                                                   |
| `password`            | -                       | Complex password string                                                                                                                                                                                                                                             |
| `database`            | postgres                | Default database name                                                                                                                                                                                                                                               |
| `min_connections`     | 1                       | Connection pool minimum size                                                                                                                                                                                                                                        |
| `max_connections`     | 5                       | Connection pool maximum size                                                                                                                                                                                                                                        |
| `table_name`          | langchain\_docs         | Name of the table for storing vector data and metadata                                                                                                                                                                                                              |
| `index_type`          | IndexType.HNSW          | Vector index algorithm type. Options: HNSW or IVFFLAT\nDefault is HNSW.                                                                                                                                                                                             |
| `vector_type`         | VectorType.vector       | Type of vector representation to use. Default is Vector.                                                                                                                                                                                                            |
| `distance_strategy`   | DistanceStrategy.COSINE | Vector similarity metric to use for retrieval. Options: euclidean (L2 distance), cosine (angular distance, ideal for text embeddings), manhattan (L1 distance for sparse data), negative\_inner\_product (dot product for normalized vectors).\n Default is cosine. |
| `embedding_dimension` | 1536                    | Dimensionality of the vector embeddings.                                                                                                                                                                                                                            |

### Supported combinations

| Vector Type | Dimensions | Index Types  | Supported Distance Strategies          |
| ----------- | ---------- | ------------ | -------------------------------------- |
| vector      | ≤2000      | HNSW/IVFFLAT | COSINE/EUCLIDEAN/MANHATTAN/INNER\_PROD |

## Performance optimization

### Index tuning guidelines

**HNSW Parameters**:

* `m`: 16-100 (balance between recall and memory)
* `ef_construction`: 64-1000 (must be > 2\*m)

**IVFFLAT Recommendations**:

```python theme={null}
import math

lists = min(
    int(math.sqrt(total_rows)) if total_rows > 1e6 else int(total_rows / 1000),
    2000,  # openGauss maximum
)
```

### Connection pooling

```python theme={null}
OpenGaussSettings(min_connections=3, max_connections=20)
```

## Limitations

* `bit` and `sparsevec` vector types currently in development
* Maximum vector dimensions: 2000 for `vector` type

***

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/vectorstores/opengauss.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
