Skip to main content

lc_vector_stores/
neo4j.rs

1// lc-vector-stores/src/neo4j.rs
2//! Neo4j vector store implementation.
3//!
4//! Uses Neo4j's vector index feature (available since Neo4j 5.11) for
5//! similarity search via the Cypher API over HTTP.
6//!
7//! # Example
8//!
9//! ```rust,ignore
10//! use lc_vector_stores::neo4j::{Neo4jVectorStore, Neo4jConfig};
11//!
12//! let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "password", "my_index");
13//! let store = Neo4jVectorStore::new(config);
14//! store.add_documents(docs, embeddings).await?;
15//! let results = store.similarity_search(&query_embedding, 5).await?;
16//! ```
17
18use async_trait::async_trait;
19use serde::Deserialize;
20use serde_json::json;
21
22use crate::{Document, SearchResult, VectorStore, VectorStoreError};
23
24/// Neo4j vector store configuration.
25#[derive(Debug, Clone)]
26pub struct Neo4jConfig {
27    /// Neo4j URI (e.g., "bolt://localhost:7687" or "neo4j://localhost:7687").
28    pub uri: String,
29    /// Username.
30    pub username: String,
31    /// Password.
32    pub password: String,
33    /// Database name (default: "neo4j").
34    pub database: String,
35    /// Node label for vector documents (default: "Document").
36    pub node_label: String,
37    /// Vector index name.
38    pub index_name: String,
39    /// Embedding property name on the node (default: "embedding").
40    pub embedding_property: String,
41    /// Content property name on the node (default: "content").
42    pub content_property: String,
43    /// Metadata property name on the node (default: "metadata").
44    pub metadata_property: String,
45    /// ID property name on the node (default: "id").
46    pub id_property: String,
47}
48
49impl Neo4jConfig {
50    /// Creates a new Neo4jConfig.
51    pub fn new(
52        uri: impl Into<String>,
53        username: impl Into<String>,
54        password: impl Into<String>,
55        index_name: impl Into<String>,
56    ) -> Self {
57        Self {
58            uri: uri.into(),
59            username: username.into(),
60            password: password.into(),
61            database: "neo4j".to_string(),
62            node_label: "Document".to_string(),
63            index_name: index_name.into(),
64            embedding_property: "embedding".to_string(),
65            content_property: "content".to_string(),
66            metadata_property: "metadata".to_string(),
67            id_property: "id".to_string(),
68        }
69    }
70
71    /// Creates config from environment variables.
72    pub fn from_env_result() -> Result<Self, String> {
73        let uri = std::env::var("NEO4J_URI")
74            .map_err(|_| "NEO4J_URI environment variable not set".to_string())?;
75        let username = std::env::var("NEO4J_USERNAME")
76            .map_err(|_| "NEO4J_USERNAME environment variable not set".to_string())?;
77        let password = std::env::var("NEO4J_PASSWORD")
78            .map_err(|_| "NEO4J_PASSWORD environment variable not set".to_string())?;
79        let index_name = std::env::var("NEO4J_VECTOR_INDEX_NAME")
80            .map_err(|_| "NEO4J_VECTOR_INDEX_NAME environment variable not set".to_string())?;
81        let database = std::env::var("NEO4J_DATABASE").unwrap_or_else(|_| "neo4j".to_string());
82        Ok(Self {
83            uri,
84            username,
85            password,
86            database,
87            index_name,
88            ..Default::default()
89        })
90    }
91
92    /// Sets the database name.
93    pub fn with_database(mut self, database: impl Into<String>) -> Self {
94        self.database = database.into();
95        self
96    }
97
98    /// Sets the node label.
99    pub fn with_node_label(mut self, label: impl Into<String>) -> Self {
100        self.node_label = label.into();
101        self
102    }
103
104    /// Sets the embedding property name.
105    pub fn with_embedding_property(mut self, prop: impl Into<String>) -> Self {
106        self.embedding_property = prop.into();
107        self
108    }
109
110    /// Sets the content property name.
111    pub fn with_content_property(mut self, prop: impl Into<String>) -> Self {
112        self.content_property = prop.into();
113        self
114    }
115}
116
117impl Default for Neo4jConfig {
118    fn default() -> Self {
119        Self {
120            uri: "bolt://localhost:7687".to_string(),
121            username: "neo4j".to_string(),
122            password: String::new(),
123            database: "neo4j".to_string(),
124            node_label: "Document".to_string(),
125            index_name: "vector_index".to_string(),
126            embedding_property: "embedding".to_string(),
127            content_property: "content".to_string(),
128            metadata_property: "metadata".to_string(),
129            id_property: "id".to_string(),
130        }
131    }
132}
133
134/// Neo4j vector store.
135///
136/// Communicates with Neo4j via the HTTP transaction API.
137pub struct Neo4jVectorStore {
138    config: Neo4jConfig,
139    client: reqwest::Client,
140}
141
142impl std::fmt::Debug for Neo4jVectorStore {
143    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144        f.debug_struct("Neo4jVectorStore")
145            .field("uri", &self.config.uri)
146            .field("index", &self.config.index_name)
147            .finish()
148    }
149}
150
151impl Neo4jVectorStore {
152    /// Creates a new Neo4jVectorStore with the given configuration.
153    pub fn new(config: Neo4jConfig) -> Self {
154        Self {
155            config,
156            client: reqwest::Client::new(),
157        }
158    }
159
160    /// Creates from environment variables.
161    pub fn from_env_result() -> Result<Self, String> {
162        Ok(Self::new(Neo4jConfig::from_env_result()?))
163    }
164
165    /// Builds the HTTP API URL for the transaction endpoint.
166    fn tx_url(&self) -> String {
167        // Convert bolt:// or neo4j:// to http:// for the REST API
168        let http_uri = self
169            .config
170            .uri
171            .replace("bolt://", "http://")
172            .replace("neo4j://", "http://")
173            .replace("bolt+s://", "https://")
174            .replace("neo4j+s://", "https://");
175        format!(
176            "{}/db/{}/tx/commit",
177            http_uri.trim_end_matches('/'),
178            self.config.database
179        )
180    }
181
182    /// Executes a Cypher query via the HTTP transaction API.
183    async fn run_query(
184        &self,
185        query: &str,
186        params: serde_json::Value,
187    ) -> Result<Neo4jResponse, VectorStoreError> {
188        let body = json!({
189            "statements": [{
190                "statement": query,
191                "parameters": params,
192            }]
193        });
194
195        let response = self
196            .client
197            .post(self.tx_url())
198            .header("Content-Type", "application/json")
199            .header(
200                "Authorization",
201                format!(
202                    "Basic {}",
203                    base64_encode(format!("{}:{}", self.config.username, self.config.password))
204                ),
205            )
206            .json(&body)
207            .send()
208            .await
209            .map_err(|e| VectorStoreError::ConnectionError(e.to_string()))?;
210
211        let status = response.status();
212        if !status.is_success() {
213            let error_text = response.text().await.unwrap_or_default();
214            return Err(VectorStoreError::ConnectionError(format!(
215                "HTTP {}: {}",
216                status, error_text
217            )));
218        }
219
220        let neo4j_response: Neo4jResponse = response
221            .json()
222            .await
223            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;
224
225        // Check for Neo4j-level errors
226        if let Some(errors) = &neo4j_response.errors {
227            if !errors.is_empty() {
228                let msg = errors
229                    .iter()
230                    .map(|e| e.message.clone())
231                    .collect::<Vec<_>>()
232                    .join("; ");
233                return Err(VectorStoreError::StorageError(msg));
234            }
235        }
236
237        Ok(neo4j_response)
238    }
239}
240
241/// Base64 encoding helper (no external dependency needed).
242fn base64_encode(input: String) -> String {
243    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
244    let bytes = input.as_bytes();
245    let mut result = String::new();
246    let mut i = 0;
247    while i < bytes.len() {
248        let b0 = bytes[i];
249        let b1 = if i + 1 < bytes.len() { bytes[i + 1] } else { 0 };
250        let b2 = if i + 2 < bytes.len() { bytes[i + 2] } else { 0 };
251
252        result.push(CHARSET[((b0 >> 2) & 0x3F) as usize] as char);
253        result.push(CHARSET[(((b0 << 4) | (b1 >> 4)) & 0x3F) as usize] as char);
254        result.push(if i + 1 < bytes.len() {
255            CHARSET[(((b1 << 2) | (b2 >> 6)) & 0x3F) as usize] as char
256        } else {
257            '='
258        });
259        result.push(if i + 2 < bytes.len() {
260            CHARSET[(b2 & 0x3F) as usize] as char
261        } else {
262            '='
263        });
264
265        i += 3;
266    }
267    result
268}
269
270// ---------------------------------------------------------------------------
271// Neo4j HTTP API response types
272// ---------------------------------------------------------------------------
273
274/// Neo4j transaction commit response.
275#[derive(Debug, Deserialize)]
276struct Neo4jResponse {
277    results: Vec<Neo4jResult>,
278    errors: Option<Vec<Neo4jError>>,
279}
280
281#[derive(Debug, Deserialize)]
282struct Neo4jResult {
283    #[allow(dead_code)]
284    columns: Vec<String>,
285    data: Vec<Neo4jRow>,
286}
287
288#[derive(Debug, Deserialize)]
289struct Neo4jRow {
290    row: Vec<serde_json::Value>,
291}
292
293#[derive(Debug, Deserialize)]
294struct Neo4jError {
295    message: String,
296}
297
298#[async_trait]
299impl VectorStore for Neo4jVectorStore {
300    async fn add_documents(
301        &self,
302        documents: Vec<Document>,
303        embeddings: Vec<Vec<f32>>,
304    ) -> Result<Vec<String>, VectorStoreError> {
305        if documents.len() != embeddings.len() {
306            return Err(VectorStoreError::EmbeddingError(
307                "Number of documents and embeddings must match".to_string(),
308            ));
309        }
310
311        let ids: Vec<String> = documents
312            .iter()
313            .map(|doc| {
314                doc.id
315                    .clone()
316                    .unwrap_or_else(|| uuid::Uuid::new_v4().to_string())
317            })
318            .collect();
319
320        // Build UNWIND Cypher for batch insert
321        let rows: Vec<serde_json::Value> = documents
322            .into_iter()
323            .zip(embeddings)
324            .zip(ids.iter())
325            .map(|((doc, vec), id)| {
326                let metadata: serde_json::Value = doc
327                    .metadata
328                    .iter()
329                    .map(|(k, v)| (k.clone(), json!(v)))
330                    .collect();
331                json!({
332                    "id": id,
333                    "content": doc.content,
334                    "embedding": vec,
335                    "metadata": metadata,
336                })
337            })
338            .collect();
339
340        let query = format!(
341            "UNWIND $rows AS row \
342             MERGE (n:{label} {{{id_prop}: row.id}}) \
343             SET n.{content_prop} = row.content, \
344                 n.{embedding_prop} = row.embedding, \
345                 n.{metadata_prop} = row.metadata",
346            label = self.config.node_label,
347            id_prop = self.config.id_property,
348            content_prop = self.config.content_property,
349            embedding_prop = self.config.embedding_property,
350            metadata_prop = self.config.metadata_property,
351        );
352
353        self.run_query(&query, json!({ "rows": rows })).await?;
354
355        Ok(ids)
356    }
357
358    async fn similarity_search(
359        &self,
360        query_embedding: &[f32],
361        k: usize,
362    ) -> Result<Vec<SearchResult>, VectorStoreError> {
363        // Use Neo4j's db.index.vector.queryNodes procedure
364        let query = format!(
365            "CALL db.index.vector.queryNodes($index_name, $k, $query_vector) \
366             YIELD node, score \
367             RETURN node.{id_prop} AS id, \
368                    node.{content_prop} AS content, \
369                    node.{metadata_prop} AS metadata, \
370                    score \
371             ORDER BY score DESC",
372            id_prop = self.config.id_property,
373            content_prop = self.config.content_property,
374            metadata_prop = self.config.metadata_property,
375        );
376
377        let params = json!({
378            "index_name": self.config.index_name,
379            "k": k,
380            "query_vector": query_embedding,
381        });
382
383        let response = self.run_query(&query, params).await?;
384
385        let result = response.results.first();
386        let Some(neo4j_result) = result else {
387            return Ok(Vec::new());
388        };
389
390        let mut search_results = Vec::new();
391        for row in &neo4j_result.data {
392            if row.row.len() >= 4 {
393                let id = row.row[0].as_str().unwrap_or_default().to_string();
394                let content = row.row[1].as_str().unwrap_or_default().to_string();
395                let score = row.row[3].as_f64().unwrap_or(0.0) as f32;
396
397                let mut doc = Document::new(content).with_id(id);
398
399                // Parse metadata from JSON object
400                if let Some(meta_obj) = row.row[2].as_object() {
401                    for (key, value) in meta_obj {
402                        if let Some(s) = value.as_str() {
403                            doc = doc.with_metadata(key, s);
404                        } else {
405                            doc = doc.with_metadata(key, value.to_string());
406                        }
407                    }
408                }
409
410                search_results.push(SearchResult {
411                    document: doc,
412                    score,
413                });
414            }
415        }
416
417        Ok(search_results)
418    }
419
420    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError> {
421        let query = format!(
422            "MATCH (n:{label} {{{id_prop}: $id}}) \
423             RETURN n.{content_prop} AS content, n.{metadata_prop} AS metadata",
424            label = self.config.node_label,
425            id_prop = self.config.id_property,
426            content_prop = self.config.content_property,
427            metadata_prop = self.config.metadata_property,
428        );
429
430        let response = self.run_query(&query, json!({ "id": id })).await?;
431
432        let result = response.results.first();
433        let Some(neo4j_result) = result else {
434            return Ok(None);
435        };
436
437        let row = neo4j_result.data.first();
438        let Some(row) = row else {
439            return Ok(None);
440        };
441
442        if row.row.is_empty() {
443            return Ok(None);
444        }
445
446        let content = row.row[0].as_str().unwrap_or_default().to_string();
447        let mut doc = Document::new(content).with_id(id);
448
449        if row.row.len() > 1 {
450            if let Some(meta_obj) = row.row[1].as_object() {
451                for (key, value) in meta_obj {
452                    if let Some(s) = value.as_str() {
453                        doc = doc.with_metadata(key, s);
454                    } else {
455                        doc = doc.with_metadata(key, value.to_string());
456                    }
457                }
458            }
459        }
460
461        Ok(Some(doc))
462    }
463
464    async fn get_embedding(&self, id: &str) -> Result<Option<Vec<f32>>, VectorStoreError> {
465        let query = format!(
466            "MATCH (n:{label} {{{id_prop}: $id}}) \
467             RETURN n.{embedding_prop} AS embedding",
468            label = self.config.node_label,
469            id_prop = self.config.id_property,
470            embedding_prop = self.config.embedding_property,
471        );
472
473        let response = self.run_query(&query, json!({ "id": id })).await?;
474
475        let result = response.results.first();
476        let Some(neo4j_result) = result else {
477            return Ok(None);
478        };
479
480        let row = neo4j_result.data.first();
481        let Some(row) = row else {
482            return Ok(None);
483        };
484
485        if row.row.is_empty() {
486            return Ok(None);
487        }
488
489        let embedding: Vec<f32> = row.row[0]
490            .as_array()
491            .map(|arr| {
492                arr.iter()
493                    .filter_map(|v| v.as_f64().map(|f| f as f32))
494                    .collect()
495            })
496            .unwrap_or_default();
497
498        if embedding.is_empty() {
499            Ok(None)
500        } else {
501            Ok(Some(embedding))
502        }
503    }
504
505    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError> {
506        let query = format!(
507            "MATCH (n:{label} {{{id_prop}: $id}}) \
508             DETACH DELETE n",
509            label = self.config.node_label,
510            id_prop = self.config.id_property,
511        );
512
513        self.run_query(&query, json!({ "id": id })).await?;
514        Ok(())
515    }
516
517    async fn count(&self) -> usize {
518        let query = format!(
519            "MATCH (n:{label}) RETURN count(n) AS cnt",
520            label = self.config.node_label,
521        );
522
523        let result = self.run_query(&query, json!({})).await;
524        match result {
525            Ok(response) => {
526                if let Some(neo4j_result) = response.results.first() {
527                    if let Some(row) = neo4j_result.data.first() {
528                        if let Some(cnt) = row.row.first() {
529                            return cnt.as_u64().unwrap_or(0) as usize;
530                        }
531                    }
532                }
533                0
534            }
535            Err(_) => 0,
536        }
537    }
538
539    async fn clear(&self) -> Result<(), VectorStoreError> {
540        let query = format!(
541            "MATCH (n:{label}) \
542             DETACH DELETE n",
543            label = self.config.node_label,
544        );
545
546        self.run_query(&query, json!({})).await?;
547        Ok(())
548    }
549}
550
551#[cfg(test)]
552mod tests {
553    use super::*;
554
555    #[test]
556    fn test_config_new() {
557        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "my_index");
558        assert_eq!(config.uri, "bolt://localhost:7687");
559        assert_eq!(config.username, "neo4j");
560        assert_eq!(config.password, "pass");
561        assert_eq!(config.index_name, "my_index");
562        assert_eq!(config.database, "neo4j");
563    }
564
565    #[test]
566    fn test_config_builder() {
567        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx")
568            .with_database("mydb")
569            .with_node_label("Chunk")
570            .with_embedding_property("vec")
571            .with_content_property("text");
572        assert_eq!(config.database, "mydb");
573        assert_eq!(config.node_label, "Chunk");
574        assert_eq!(config.embedding_property, "vec");
575        assert_eq!(config.content_property, "text");
576    }
577
578    #[test]
579    fn test_config_default() {
580        let config = Neo4jConfig::default();
581        assert_eq!(config.uri, "bolt://localhost:7687");
582        assert_eq!(config.node_label, "Document");
583        assert_eq!(config.embedding_property, "embedding");
584    }
585
586    #[test]
587    fn test_tx_url_bolt() {
588        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
589        let store = Neo4jVectorStore::new(config);
590        assert_eq!(store.tx_url(), "http://localhost:7687/db/neo4j/tx/commit");
591    }
592
593    #[test]
594    fn test_tx_url_neo4j_scheme() {
595        let config = Neo4jConfig::new("neo4j://host:7687", "neo4j", "pass", "idx");
596        let store = Neo4jVectorStore::new(config);
597        assert_eq!(store.tx_url(), "http://host:7687/db/neo4j/tx/commit");
598    }
599
600    #[test]
601    fn test_tx_url_bolt_s() {
602        let config = Neo4jConfig::new("bolt+s://host:7687", "neo4j", "pass", "idx");
603        let store = Neo4jVectorStore::new(config);
604        assert_eq!(store.tx_url(), "https://host:7687/db/neo4j/tx/commit");
605    }
606
607    #[test]
608    fn test_tx_url_custom_database() {
609        let config =
610            Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx").with_database("mydb");
611        let store = Neo4jVectorStore::new(config);
612        assert_eq!(store.tx_url(), "http://localhost:7687/db/mydb/tx/commit");
613    }
614
615    #[test]
616    fn test_base64_encode() {
617        // "neo4j:password" in base64
618        let encoded = base64_encode("neo4j:password".to_string());
619        assert_eq!(encoded, "bmVvNGo6cGFzc3dvcmQ=");
620    }
621
622    #[test]
623    fn test_base64_encode_empty() {
624        let encoded = base64_encode(String::new());
625        assert_eq!(encoded, "");
626    }
627
628    #[test]
629    fn test_store_new() {
630        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
631        let _store = Neo4jVectorStore::new(config);
632    }
633
634    #[test]
635    fn test_store_debug() {
636        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
637        let store = Neo4jVectorStore::new(config);
638        let debug_str = format!("{:?}", store);
639        assert!(debug_str.contains("Neo4jVectorStore"));
640        assert!(debug_str.contains("idx"));
641    }
642}