lc-vector-stores 0.12.0

Vector store implementations for langchainrust — InMemory, File, Qdrant, MongoDB, Redis, SQLite, ChromaDB, Pinecone, PGVector
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
// lc-vector-stores/src/neo4j.rs
//! Neo4j vector store implementation.
//!
//! Uses Neo4j's vector index feature (available since Neo4j 5.11) for
//! similarity search via the Cypher API over HTTP.
//!
//! # Example
//!
//! ```rust,ignore
//! use lc_vector_stores::neo4j::{Neo4jVectorStore, Neo4jConfig};
//!
//! let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "password", "my_index");
//! let store = Neo4jVectorStore::new(config);
//! store.add_documents(docs, embeddings).await?;
//! let results = store.similarity_search(&query_embedding, 5).await?;
//! ```

use async_trait::async_trait;
use serde::Deserialize;
use serde_json::json;

use crate::{Document, SearchResult, VectorStore, VectorStoreError};

/// Neo4j vector store configuration.
#[derive(Debug, Clone)]
pub struct Neo4jConfig {
    /// Neo4j URI (e.g., "bolt://localhost:7687" or "neo4j://localhost:7687").
    pub uri: String,
    /// Username.
    pub username: String,
    /// Password.
    pub password: String,
    /// Database name (default: "neo4j").
    pub database: String,
    /// Node label for vector documents (default: "Document").
    pub node_label: String,
    /// Vector index name.
    pub index_name: String,
    /// Embedding property name on the node (default: "embedding").
    pub embedding_property: String,
    /// Content property name on the node (default: "content").
    pub content_property: String,
    /// Metadata property name on the node (default: "metadata").
    pub metadata_property: String,
    /// ID property name on the node (default: "id").
    pub id_property: String,
}

impl Neo4jConfig {
    /// Creates a new Neo4jConfig.
    pub fn new(
        uri: impl Into<String>,
        username: impl Into<String>,
        password: impl Into<String>,
        index_name: impl Into<String>,
    ) -> Self {
        Self {
            uri: uri.into(),
            username: username.into(),
            password: password.into(),
            database: "neo4j".to_string(),
            node_label: "Document".to_string(),
            index_name: index_name.into(),
            embedding_property: "embedding".to_string(),
            content_property: "content".to_string(),
            metadata_property: "metadata".to_string(),
            id_property: "id".to_string(),
        }
    }

    /// Creates config from environment variables.
    pub fn from_env_result() -> Result<Self, String> {
        let uri = std::env::var("NEO4J_URI")
            .map_err(|_| "NEO4J_URI environment variable not set".to_string())?;
        let username = std::env::var("NEO4J_USERNAME")
            .map_err(|_| "NEO4J_USERNAME environment variable not set".to_string())?;
        let password = std::env::var("NEO4J_PASSWORD")
            .map_err(|_| "NEO4J_PASSWORD environment variable not set".to_string())?;
        let index_name = std::env::var("NEO4J_VECTOR_INDEX_NAME")
            .map_err(|_| "NEO4J_VECTOR_INDEX_NAME environment variable not set".to_string())?;
        let database = std::env::var("NEO4J_DATABASE").unwrap_or_else(|_| "neo4j".to_string());
        Ok(Self {
            uri,
            username,
            password,
            database,
            index_name,
            ..Default::default()
        })
    }

    /// Sets the database name.
    pub fn with_database(mut self, database: impl Into<String>) -> Self {
        self.database = database.into();
        self
    }

    /// Sets the node label.
    pub fn with_node_label(mut self, label: impl Into<String>) -> Self {
        self.node_label = label.into();
        self
    }

    /// Sets the embedding property name.
    pub fn with_embedding_property(mut self, prop: impl Into<String>) -> Self {
        self.embedding_property = prop.into();
        self
    }

    /// Sets the content property name.
    pub fn with_content_property(mut self, prop: impl Into<String>) -> Self {
        self.content_property = prop.into();
        self
    }
}

impl Default for Neo4jConfig {
    fn default() -> Self {
        Self {
            uri: "bolt://localhost:7687".to_string(),
            username: "neo4j".to_string(),
            password: String::new(),
            database: "neo4j".to_string(),
            node_label: "Document".to_string(),
            index_name: "vector_index".to_string(),
            embedding_property: "embedding".to_string(),
            content_property: "content".to_string(),
            metadata_property: "metadata".to_string(),
            id_property: "id".to_string(),
        }
    }
}

/// Neo4j vector store.
///
/// Communicates with Neo4j via the HTTP transaction API.
pub struct Neo4jVectorStore {
    config: Neo4jConfig,
    client: reqwest::Client,
}

impl std::fmt::Debug for Neo4jVectorStore {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Neo4jVectorStore")
            .field("uri", &self.config.uri)
            .field("index", &self.config.index_name)
            .finish()
    }
}

impl Neo4jVectorStore {
    /// Creates a new Neo4jVectorStore with the given configuration.
    pub fn new(config: Neo4jConfig) -> Self {
        Self {
            config,
            client: reqwest::Client::new(),
        }
    }

    /// Creates from environment variables.
    pub fn from_env_result() -> Result<Self, String> {
        Ok(Self::new(Neo4jConfig::from_env_result()?))
    }

    /// Builds the HTTP API URL for the transaction endpoint.
    fn tx_url(&self) -> String {
        // Convert bolt:// or neo4j:// to http:// for the REST API
        let http_uri = self
            .config
            .uri
            .replace("bolt://", "http://")
            .replace("neo4j://", "http://")
            .replace("bolt+s://", "https://")
            .replace("neo4j+s://", "https://");
        format!(
            "{}/db/{}/tx/commit",
            http_uri.trim_end_matches('/'),
            self.config.database
        )
    }

    /// Executes a Cypher query via the HTTP transaction API.
    async fn run_query(
        &self,
        query: &str,
        params: serde_json::Value,
    ) -> Result<Neo4jResponse, VectorStoreError> {
        let body = json!({
            "statements": [{
                "statement": query,
                "parameters": params,
            }]
        });

        let response = self
            .client
            .post(self.tx_url())
            .header("Content-Type", "application/json")
            .header(
                "Authorization",
                format!(
                    "Basic {}",
                    base64_encode(format!(
                        "{}:{}",
                        self.config.username, self.config.password
                    ))
                ),
            )
            .json(&body)
            .send()
            .await
            .map_err(|e| VectorStoreError::ConnectionError(e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let error_text = response.text().await.unwrap_or_default();
            return Err(VectorStoreError::ConnectionError(format!(
                "HTTP {}: {}",
                status, error_text
            )));
        }

        let neo4j_response: Neo4jResponse = response
            .json()
            .await
            .map_err(|e| VectorStoreError::StorageError(e.to_string()))?;

        // Check for Neo4j-level errors
        if let Some(errors) = &neo4j_response.errors {
            if !errors.is_empty() {
                let msg = errors
                    .iter()
                    .map(|e| e.message.clone())
                    .collect::<Vec<_>>()
                    .join("; ");
                return Err(VectorStoreError::StorageError(msg));
            }
        }

        Ok(neo4j_response)
    }
}

/// Base64 encoding helper (no external dependency needed).
fn base64_encode(input: String) -> String {
    const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    let bytes = input.as_bytes();
    let mut result = String::new();
    let mut i = 0;
    while i < bytes.len() {
        let b0 = bytes[i];
        let b1 = if i + 1 < bytes.len() { bytes[i + 1] } else { 0 };
        let b2 = if i + 2 < bytes.len() { bytes[i + 2] } else { 0 };

        result.push(CHARSET[((b0 >> 2) & 0x3F) as usize] as char);
        result.push(CHARSET[(((b0 << 4) | (b1 >> 4)) & 0x3F) as usize] as char);
        result.push(if i + 1 < bytes.len() {
            CHARSET[(((b1 << 2) | (b2 >> 6)) & 0x3F) as usize] as char
        } else {
            '='
        });
        result.push(if i + 2 < bytes.len() {
            CHARSET[(b2 & 0x3F) as usize] as char
        } else {
            '='
        });

        i += 3;
    }
    result
}

// ---------------------------------------------------------------------------
// Neo4j HTTP API response types
// ---------------------------------------------------------------------------

/// Neo4j transaction commit response.
#[derive(Debug, Deserialize)]
struct Neo4jResponse {
    results: Vec<Neo4jResult>,
    errors: Option<Vec<Neo4jError>>,
}

#[derive(Debug, Deserialize)]
struct Neo4jResult {
    #[allow(dead_code)]
    columns: Vec<String>,
    data: Vec<Neo4jRow>,
}

#[derive(Debug, Deserialize)]
struct Neo4jRow {
    row: Vec<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct Neo4jError {
    message: String,
}

#[async_trait]
impl VectorStore for Neo4jVectorStore {
    async fn add_documents(
        &self,
        documents: Vec<Document>,
        embeddings: Vec<Vec<f32>>,
    ) -> Result<Vec<String>, VectorStoreError> {
        if documents.len() != embeddings.len() {
            return Err(VectorStoreError::EmbeddingError(
                "Number of documents and embeddings must match".to_string(),
            ));
        }

        let ids: Vec<String> = documents
            .iter()
            .map(|doc| doc.id.clone().unwrap_or_else(|| uuid::Uuid::new_v4().to_string()))
            .collect();

        // Build UNWIND Cypher for batch insert
        let rows: Vec<serde_json::Value> = documents
            .into_iter()
            .zip(embeddings)
            .zip(ids.iter())
            .map(|((doc, vec), id)| {
                let metadata: serde_json::Value = doc
                    .metadata
                    .iter()
                    .map(|(k, v)| (k.clone(), json!(v)))
                    .collect();
                json!({
                    "id": id,
                    "content": doc.content,
                    "embedding": vec,
                    "metadata": metadata,
                })
            })
            .collect();

        let query = format!(
            "UNWIND $rows AS row \
             MERGE (n:{label} {{{id_prop}: row.id}}) \
             SET n.{content_prop} = row.content, \
                 n.{embedding_prop} = row.embedding, \
                 n.{metadata_prop} = row.metadata",
            label = self.config.node_label,
            id_prop = self.config.id_property,
            content_prop = self.config.content_property,
            embedding_prop = self.config.embedding_property,
            metadata_prop = self.config.metadata_property,
        );

        self.run_query(&query, json!({ "rows": rows }))
            .await?;

        Ok(ids)
    }

    async fn similarity_search(
        &self,
        query_embedding: &[f32],
        k: usize,
    ) -> Result<Vec<SearchResult>, VectorStoreError> {
        // Use Neo4j's db.index.vector.queryNodes procedure
        let query = format!(
            "CALL db.index.vector.queryNodes($index_name, $k, $query_vector) \
             YIELD node, score \
             RETURN node.{id_prop} AS id, \
                    node.{content_prop} AS content, \
                    node.{metadata_prop} AS metadata, \
                    score \
             ORDER BY score DESC",
            id_prop = self.config.id_property,
            content_prop = self.config.content_property,
            metadata_prop = self.config.metadata_property,
        );

        let params = json!({
            "index_name": self.config.index_name,
            "k": k,
            "query_vector": query_embedding,
        });

        let response = self.run_query(&query, params).await?;

        let result = response.results.first();
        let Some(neo4j_result) = result else {
            return Ok(Vec::new());
        };

        let mut search_results = Vec::new();
        for row in &neo4j_result.data {
            if row.row.len() >= 4 {
                let id = row.row[0].as_str().unwrap_or_default().to_string();
                let content = row.row[1].as_str().unwrap_or_default().to_string();
                let score = row.row[3].as_f64().unwrap_or(0.0) as f32;

                let mut doc = Document::new(content).with_id(id);

                // Parse metadata from JSON object
                if let Some(meta_obj) = row.row[2].as_object() {
                    for (key, value) in meta_obj {
                        if let Some(s) = value.as_str() {
                            doc = doc.with_metadata(key, s);
                        } else {
                            doc = doc.with_metadata(key, value.to_string());
                        }
                    }
                }

                search_results.push(SearchResult {
                    document: doc,
                    score,
                });
            }
        }

        Ok(search_results)
    }

    async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError> {
        let query = format!(
            "MATCH (n:{label} {{{id_prop}: $id}}) \
             RETURN n.{content_prop} AS content, n.{metadata_prop} AS metadata",
            label = self.config.node_label,
            id_prop = self.config.id_property,
            content_prop = self.config.content_property,
            metadata_prop = self.config.metadata_property,
        );

        let response = self.run_query(&query, json!({ "id": id })).await?;

        let result = response.results.first();
        let Some(neo4j_result) = result else {
            return Ok(None);
        };

        let row = neo4j_result.data.first();
        let Some(row) = row else {
            return Ok(None);
        };

        if row.row.is_empty() {
            return Ok(None);
        }

        let content = row.row[0].as_str().unwrap_or_default().to_string();
        let mut doc = Document::new(content).with_id(id);

        if row.row.len() > 1 {
            if let Some(meta_obj) = row.row[1].as_object() {
                for (key, value) in meta_obj {
                    if let Some(s) = value.as_str() {
                        doc = doc.with_metadata(key, s);
                    } else {
                        doc = doc.with_metadata(key, value.to_string());
                    }
                }
            }
        }

        Ok(Some(doc))
    }

    async fn get_embedding(&self, id: &str) -> Result<Option<Vec<f32>>, VectorStoreError> {
        let query = format!(
            "MATCH (n:{label} {{{id_prop}: $id}}) \
             RETURN n.{embedding_prop} AS embedding",
            label = self.config.node_label,
            id_prop = self.config.id_property,
            embedding_prop = self.config.embedding_property,
        );

        let response = self.run_query(&query, json!({ "id": id })).await?;

        let result = response.results.first();
        let Some(neo4j_result) = result else {
            return Ok(None);
        };

        let row = neo4j_result.data.first();
        let Some(row) = row else {
            return Ok(None);
        };

        if row.row.is_empty() {
            return Ok(None);
        }

        let embedding: Vec<f32> = row.row[0]
            .as_array()
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_f64().map(|f| f as f32))
                    .collect()
            })
            .unwrap_or_default();

        if embedding.is_empty() {
            Ok(None)
        } else {
            Ok(Some(embedding))
        }
    }

    async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError> {
        let query = format!(
            "MATCH (n:{label} {{{id_prop}: $id}}) \
             DETACH DELETE n",
            label = self.config.node_label,
            id_prop = self.config.id_property,
        );

        self.run_query(&query, json!({ "id": id })).await?;
        Ok(())
    }

    async fn count(&self) -> usize {
        let query = format!(
            "MATCH (n:{label}) RETURN count(n) AS cnt",
            label = self.config.node_label,
        );

        let result = self.run_query(&query, json!({})).await;
        match result {
            Ok(response) => {
                if let Some(neo4j_result) = response.results.first() {
                    if let Some(row) = neo4j_result.data.first() {
                        if let Some(cnt) = row.row.first() {
                            return cnt.as_u64().unwrap_or(0) as usize;
                        }
                    }
                }
                0
            }
            Err(_) => 0,
        }
    }

    async fn clear(&self) -> Result<(), VectorStoreError> {
        let query = format!(
            "MATCH (n:{label}) \
             DETACH DELETE n",
            label = self.config.node_label,
        );

        self.run_query(&query, json!({})).await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_new() {
        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "my_index");
        assert_eq!(config.uri, "bolt://localhost:7687");
        assert_eq!(config.username, "neo4j");
        assert_eq!(config.password, "pass");
        assert_eq!(config.index_name, "my_index");
        assert_eq!(config.database, "neo4j");
    }

    #[test]
    fn test_config_builder() {
        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx")
            .with_database("mydb")
            .with_node_label("Chunk")
            .with_embedding_property("vec")
            .with_content_property("text");
        assert_eq!(config.database, "mydb");
        assert_eq!(config.node_label, "Chunk");
        assert_eq!(config.embedding_property, "vec");
        assert_eq!(config.content_property, "text");
    }

    #[test]
    fn test_config_default() {
        let config = Neo4jConfig::default();
        assert_eq!(config.uri, "bolt://localhost:7687");
        assert_eq!(config.node_label, "Document");
        assert_eq!(config.embedding_property, "embedding");
    }

    #[test]
    fn test_tx_url_bolt() {
        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
        let store = Neo4jVectorStore::new(config);
        assert_eq!(
            store.tx_url(),
            "http://localhost:7687/db/neo4j/tx/commit"
        );
    }

    #[test]
    fn test_tx_url_neo4j_scheme() {
        let config = Neo4jConfig::new("neo4j://host:7687", "neo4j", "pass", "idx");
        let store = Neo4jVectorStore::new(config);
        assert_eq!(
            store.tx_url(),
            "http://host:7687/db/neo4j/tx/commit"
        );
    }

    #[test]
    fn test_tx_url_bolt_s() {
        let config = Neo4jConfig::new("bolt+s://host:7687", "neo4j", "pass", "idx");
        let store = Neo4jVectorStore::new(config);
        assert_eq!(
            store.tx_url(),
            "https://host:7687/db/neo4j/tx/commit"
        );
    }

    #[test]
    fn test_tx_url_custom_database() {
        let config =
            Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx").with_database("mydb");
        let store = Neo4jVectorStore::new(config);
        assert_eq!(
            store.tx_url(),
            "http://localhost:7687/db/mydb/tx/commit"
        );
    }

    #[test]
    fn test_base64_encode() {
        // "neo4j:password" in base64
        let encoded = base64_encode("neo4j:password".to_string());
        assert_eq!(encoded, "bmVvNGo6cGFzc3dvcmQ=");
    }

    #[test]
    fn test_base64_encode_empty() {
        let encoded = base64_encode(String::new());
        assert_eq!(encoded, "");
    }

    #[test]
    fn test_store_new() {
        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
        let _store = Neo4jVectorStore::new(config);
    }

    #[test]
    fn test_store_debug() {
        let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
        let store = Neo4jVectorStore::new(config);
        let debug_str = format!("{:?}", store);
        assert!(debug_str.contains("Neo4jVectorStore"));
        assert!(debug_str.contains("idx"));
    }
}