|
| 1 | +--- |
| 2 | +layout: integration |
| 3 | +name: FalkorDB |
| 4 | +description: Use FalkorDB as a document store with native vector search for GraphRAG workloads in Haystack |
| 5 | +authors: |
| 6 | + - name: deepset |
| 7 | + socials: |
| 8 | + github: deepset-ai |
| 9 | + twitter: deepset_ai |
| 10 | + linkedin: https://www.linkedin.com/company/deepset-ai/ |
| 11 | +pypi: https://pypi.org/project/falkordb-haystack/ |
| 12 | +repo: https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/falkordb |
| 13 | +type: Document Store |
| 14 | +report_issue: https://github.com/deepset-ai/haystack-core-integrations/issues |
| 15 | +logo: /logos/falkordb.png |
| 16 | +version: Haystack 2.0 |
| 17 | +toc: true |
| 18 | +--- |
| 19 | + |
| 20 | +### Table of Contents |
| 21 | + |
| 22 | +- [Overview](#overview) |
| 23 | +- [Installation](#installation) |
| 24 | +- [Usage](#usage) |
| 25 | + - [Writing documents](#writing-documents) |
| 26 | + - [Retrieving documents](#retrieving-documents) |
| 27 | + - [Graph queries with Cypher](#graph-queries-with-cypher) |
| 28 | +- [License](#license) |
| 29 | + |
| 30 | +## Overview |
| 31 | + |
| 32 | +An integration of [FalkorDB](https://www.falkordb.com/) with [Haystack](https://docs.haystack.deepset.ai/docs/intro) by [deepset](https://www.deepset.ai). |
| 33 | + |
| 34 | +FalkorDB is a high-performance graph database optimized for GraphRAG workloads. It stores documents as graph nodes and supports native vector search — no APOC is required. All bulk writes use `UNWIND` + `MERGE` for safe, idiomatic OpenCypher upserts. |
| 35 | + |
| 36 | +The library provides a `FalkorDBDocumentStore` that implements the Haystack [DocumentStore protocol](https://docs.haystack.deepset.ai/docs/document-store#documentstore-protocol), plus two pipeline-ready retriever components: |
| 37 | + |
| 38 | +- **FalkorDBDocumentStore** — stores Documents as labeled graph nodes in a named FalkorDB graph, with `meta` fields stored flat alongside `id` and `content`. Embeddings are indexed using FalkorDB's native vector index. |
| 39 | +- **FalkorDBEmbeddingRetriever** — a [retriever component](https://docs.haystack.deepset.ai/docs/retrievers) that queries the native vector index to find Documents by dense similarity, with support for metadata filtering. |
| 40 | +- **FalkorDBCypherRetriever** — a power-user retriever for executing arbitrary [OpenCypher](https://opencypher.org/) queries, enabling graph traversal and multi-hop queries in GraphRAG pipelines. |
| 41 | + |
| 42 | +```text |
| 43 | + +-----------------------------+ |
| 44 | + | FalkorDB Database | |
| 45 | + +-----------------------------+ |
| 46 | + | | |
| 47 | + | +----------------+ | |
| 48 | + | | Document | | |
| 49 | + write_documents | +----------------+ | |
| 50 | + +------------------------+----->| properties | | |
| 51 | + | | | | | |
| 52 | ++---------+----------+ | | embedding | | |
| 53 | +| | | +--------+-------+ | |
| 54 | +| FalkorDBDocument | | | | |
| 55 | +| Store | | |index/query | |
| 56 | ++---------+----------+ | | | |
| 57 | + | | +---------+---------+ | |
| 58 | + | | | Native Vector Idx | | |
| 59 | + +----------------------->| | | | |
| 60 | + _embedding_retrieval | | (vecf32 index) | | |
| 61 | + | +-------------------+ | |
| 62 | + | | |
| 63 | + +-----------------------------+ |
| 64 | +``` |
| 65 | + |
| 66 | +In the above diagram: |
| 67 | + |
| 68 | +- `Document` is a FalkorDB node with a configurable label (default: `"Document"`) |
| 69 | +- `properties` are Document [attributes](https://docs.haystack.deepset.ai/docs/data-classes#document) and `meta` fields stored flat on the node |
| 70 | +- `embedding` is stored as a `vecf32` vector property indexed by FalkorDB's native vector index |
| 71 | +- The native vector index enables approximate nearest neighbor search via `db.idx.vector.queryNodes` |
| 72 | + |
| 73 | +## Installation |
| 74 | + |
| 75 | +`falkordb-haystack` can be installed using pip: |
| 76 | + |
| 77 | +```bash |
| 78 | +pip install falkordb-haystack |
| 79 | +``` |
| 80 | + |
| 81 | +You will need a running FalkorDB instance. The simplest way is with Docker: |
| 82 | + |
| 83 | +```bash |
| 84 | +docker run -d -p 6379:6379 falkordb/falkordb:latest |
| 85 | +``` |
| 86 | + |
| 87 | +## Usage |
| 88 | + |
| 89 | +```python |
| 90 | +from haystack_integrations.document_stores.falkordb import FalkorDBDocumentStore |
| 91 | + |
| 92 | +document_store = FalkorDBDocumentStore( |
| 93 | + host="localhost", |
| 94 | + port=6379, |
| 95 | + embedding_dim=384, |
| 96 | + similarity="cosine", |
| 97 | +) |
| 98 | +``` |
| 99 | + |
| 100 | +### Writing documents |
| 101 | + |
| 102 | +```python |
| 103 | +from haystack import Document |
| 104 | +from haystack.document_stores.types import DuplicatePolicy |
| 105 | + |
| 106 | +documents = [ |
| 107 | + Document( |
| 108 | + content="FalkorDB is a high-performance graph database for GraphRAG.", |
| 109 | + meta={"source": "docs", "category": "database"}, |
| 110 | + ) |
| 111 | +] |
| 112 | +document_store.write_documents(documents, policy=DuplicatePolicy.OVERWRITE) |
| 113 | +``` |
| 114 | + |
| 115 | +### Retrieving documents |
| 116 | + |
| 117 | +`FalkorDBEmbeddingRetriever` can be used in a pipeline to retrieve documents by querying the native vector index with an embedded query, with optional metadata filtering: |
| 118 | + |
| 119 | +```python |
| 120 | +from haystack import Document, Pipeline |
| 121 | +from haystack.components.embedders import ( |
| 122 | + SentenceTransformersDocumentEmbedder, |
| 123 | + SentenceTransformersTextEmbedder, |
| 124 | +) |
| 125 | +from haystack_integrations.document_stores.falkordb import FalkorDBDocumentStore |
| 126 | +from haystack_integrations.components.retrievers.falkordb import FalkorDBEmbeddingRetriever |
| 127 | + |
| 128 | +document_store = FalkorDBDocumentStore( |
| 129 | + host="localhost", |
| 130 | + port=6379, |
| 131 | + embedding_dim=384, |
| 132 | + recreate_graph=True, |
| 133 | +) |
| 134 | + |
| 135 | +documents = [ |
| 136 | + Document( |
| 137 | + content="My name is Morgan and I live in Paris.", |
| 138 | + meta={"release_date": "2018-12-09"}, |
| 139 | + ) |
| 140 | +] |
| 141 | + |
| 142 | +document_embedder = SentenceTransformersDocumentEmbedder( |
| 143 | + model="sentence-transformers/all-MiniLM-L6-v2" |
| 144 | +) |
| 145 | +document_embedder.warm_up() |
| 146 | +documents_with_embeddings = document_embedder.run(documents) |
| 147 | +document_store.write_documents(documents_with_embeddings["documents"]) |
| 148 | + |
| 149 | +pipeline = Pipeline() |
| 150 | +pipeline.add_component( |
| 151 | + "text_embedder", |
| 152 | + SentenceTransformersTextEmbedder(model="sentence-transformers/all-MiniLM-L6-v2"), |
| 153 | +) |
| 154 | +pipeline.add_component( |
| 155 | + "retriever", |
| 156 | + FalkorDBEmbeddingRetriever(document_store=document_store), |
| 157 | +) |
| 158 | +pipeline.connect("text_embedder.embedding", "retriever.query_embedding") |
| 159 | + |
| 160 | +result = pipeline.run( |
| 161 | + data={ |
| 162 | + "text_embedder": {"text": "What cities do people live in?"}, |
| 163 | + "retriever": { |
| 164 | + "top_k": 5, |
| 165 | + "filters": {"field": "release_date", "operator": "==", "value": "2018-12-09"}, |
| 166 | + }, |
| 167 | + } |
| 168 | +) |
| 169 | + |
| 170 | +documents = result["retriever"]["documents"] |
| 171 | +``` |
| 172 | + |
| 173 | +### Graph queries with Cypher |
| 174 | + |
| 175 | +`FalkorDBCypherRetriever` allows you to run arbitrary OpenCypher queries against the graph, which is useful for multi-hop traversals and custom GraphRAG patterns. Use parameterized queries to avoid injection vulnerabilities: |
| 176 | + |
| 177 | +```python |
| 178 | +from haystack_integrations.document_stores.falkordb import FalkorDBDocumentStore |
| 179 | +from haystack_integrations.components.retrievers.falkordb import FalkorDBCypherRetriever |
| 180 | + |
| 181 | +document_store = FalkorDBDocumentStore(host="localhost", port=6379) |
| 182 | + |
| 183 | +retriever = FalkorDBCypherRetriever( |
| 184 | + document_store=document_store, |
| 185 | + custom_cypher_query="MATCH (d:Document {topic: $topic}) RETURN d", |
| 186 | +) |
| 187 | + |
| 188 | +result = retriever.run(parameters={"topic": "GraphRAG"}) |
| 189 | +documents = result["documents"] |
| 190 | +``` |
| 191 | + |
| 192 | +## License |
| 193 | + |
| 194 | +`falkordb-haystack` is distributed under the terms of the [Apache 2.0](https://spdx.org/licenses/Apache-2.0.html) license. |
0 commit comments