1use async_trait::async_trait;
19use serde::Deserialize;
20use serde_json::json;
21
22use crate::{Document, SearchResult, VectorStore, VectorStoreError};
23
24#[derive(Debug, Clone)]
26pub struct Neo4jConfig {
27 pub uri: String,
29 pub username: String,
31 pub password: String,
33 pub database: String,
35 pub node_label: String,
37 pub index_name: String,
39 pub embedding_property: String,
41 pub content_property: String,
43 pub metadata_property: String,
45 pub id_property: String,
47}
48
49impl Neo4jConfig {
50 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 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 pub fn with_database(mut self, database: impl Into<String>) -> Self {
94 self.database = database.into();
95 self
96 }
97
98 pub fn with_node_label(mut self, label: impl Into<String>) -> Self {
100 self.node_label = label.into();
101 self
102 }
103
104 pub fn with_embedding_property(mut self, prop: impl Into<String>) -> Self {
106 self.embedding_property = prop.into();
107 self
108 }
109
110 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
134pub 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 pub fn new(config: Neo4jConfig) -> Self {
154 Self {
155 config,
156 client: reqwest::Client::new(),
157 }
158 }
159
160 pub fn from_env_result() -> Result<Self, String> {
162 Ok(Self::new(Neo4jConfig::from_env_result()?))
163 }
164
165 fn tx_url(&self) -> String {
167 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 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 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
241fn 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#[derive(Debug, Deserialize)]
276struct Neo4jResponse {
277 results: Vec<Neo4jResult>,
278 errors: Option<Vec<Neo4jError>>,
279}
280
281#[derive(Debug, Deserialize)]
282struct Neo4jResult {
283 data: Vec<Neo4jRow>,
284}
285
286#[derive(Debug, Deserialize)]
287struct Neo4jRow {
288 row: Vec<serde_json::Value>,
289}
290
291#[derive(Debug, Deserialize)]
292struct Neo4jError {
293 message: String,
294}
295
296#[async_trait]
297impl VectorStore for Neo4jVectorStore {
298 async fn add_documents(
299 &self,
300 documents: Vec<Document>,
301 embeddings: Vec<Vec<f32>>,
302 ) -> Result<Vec<String>, VectorStoreError> {
303 if documents.len() != embeddings.len() {
304 return Err(VectorStoreError::EmbeddingError(
305 "Number of documents and embeddings must match".to_string(),
306 ));
307 }
308
309 let ids: Vec<String> = documents
310 .iter()
311 .map(|doc| {
312 doc.id
313 .clone()
314 .unwrap_or_else(|| uuid::Uuid::new_v4().to_string())
315 })
316 .collect();
317
318 let rows: Vec<serde_json::Value> = documents
320 .into_iter()
321 .zip(embeddings)
322 .zip(ids.iter())
323 .map(|((doc, vec), id)| {
324 let metadata: serde_json::Value = doc
325 .metadata
326 .iter()
327 .map(|(k, v)| (k.clone(), json!(v)))
328 .collect();
329 json!({
330 "id": id,
331 "content": doc.content,
332 "embedding": vec,
333 "metadata": metadata,
334 })
335 })
336 .collect();
337
338 let query = format!(
339 "UNWIND $rows AS row \
340 MERGE (n:{label} {{{id_prop}: row.id}}) \
341 SET n.{content_prop} = row.content, \
342 n.{embedding_prop} = row.embedding, \
343 n.{metadata_prop} = row.metadata",
344 label = self.config.node_label,
345 id_prop = self.config.id_property,
346 content_prop = self.config.content_property,
347 embedding_prop = self.config.embedding_property,
348 metadata_prop = self.config.metadata_property,
349 );
350
351 self.run_query(&query, json!({ "rows": rows })).await?;
352
353 Ok(ids)
354 }
355
356 async fn similarity_search(
357 &self,
358 query_embedding: &[f32],
359 k: usize,
360 ) -> Result<Vec<SearchResult>, VectorStoreError> {
361 let query = format!(
363 "CALL db.index.vector.queryNodes($index_name, $k, $query_vector) \
364 YIELD node, score \
365 RETURN node.{id_prop} AS id, \
366 node.{content_prop} AS content, \
367 node.{metadata_prop} AS metadata, \
368 score \
369 ORDER BY score DESC",
370 id_prop = self.config.id_property,
371 content_prop = self.config.content_property,
372 metadata_prop = self.config.metadata_property,
373 );
374
375 let params = json!({
376 "index_name": self.config.index_name,
377 "k": k,
378 "query_vector": query_embedding,
379 });
380
381 let response = self.run_query(&query, params).await?;
382
383 let result = response.results.first();
384 let Some(neo4j_result) = result else {
385 return Ok(Vec::new());
386 };
387
388 let mut search_results = Vec::new();
389 for row in &neo4j_result.data {
390 if row.row.len() >= 4 {
391 let id = row.row[0].as_str().unwrap_or_default().to_string();
392 let content = row.row[1].as_str().unwrap_or_default().to_string();
393 let score = row.row[3].as_f64().unwrap_or(0.0) as f32;
394
395 let mut doc = Document::new(content).with_id(id);
396
397 if let Some(meta_obj) = row.row[2].as_object() {
399 for (key, value) in meta_obj {
400 if let Some(s) = value.as_str() {
401 doc = doc.with_metadata(key, s);
402 } else {
403 doc = doc.with_metadata(key, value.to_string());
404 }
405 }
406 }
407
408 search_results.push(SearchResult {
409 document: doc,
410 score,
411 });
412 }
413 }
414
415 Ok(search_results)
416 }
417
418 async fn get_document(&self, id: &str) -> Result<Option<Document>, VectorStoreError> {
419 let query = format!(
420 "MATCH (n:{label} {{{id_prop}: $id}}) \
421 RETURN n.{content_prop} AS content, n.{metadata_prop} AS metadata",
422 label = self.config.node_label,
423 id_prop = self.config.id_property,
424 content_prop = self.config.content_property,
425 metadata_prop = self.config.metadata_property,
426 );
427
428 let response = self.run_query(&query, json!({ "id": id })).await?;
429
430 let result = response.results.first();
431 let Some(neo4j_result) = result else {
432 return Ok(None);
433 };
434
435 let row = neo4j_result.data.first();
436 let Some(row) = row else {
437 return Ok(None);
438 };
439
440 if row.row.is_empty() {
441 return Ok(None);
442 }
443
444 let content = row.row[0].as_str().unwrap_or_default().to_string();
445 let mut doc = Document::new(content).with_id(id);
446
447 if row.row.len() > 1 {
448 if let Some(meta_obj) = row.row[1].as_object() {
449 for (key, value) in meta_obj {
450 if let Some(s) = value.as_str() {
451 doc = doc.with_metadata(key, s);
452 } else {
453 doc = doc.with_metadata(key, value.to_string());
454 }
455 }
456 }
457 }
458
459 Ok(Some(doc))
460 }
461
462 async fn get_embedding(&self, id: &str) -> Result<Option<Vec<f32>>, VectorStoreError> {
463 let query = format!(
464 "MATCH (n:{label} {{{id_prop}: $id}}) \
465 RETURN n.{embedding_prop} AS embedding",
466 label = self.config.node_label,
467 id_prop = self.config.id_property,
468 embedding_prop = self.config.embedding_property,
469 );
470
471 let response = self.run_query(&query, json!({ "id": id })).await?;
472
473 let result = response.results.first();
474 let Some(neo4j_result) = result else {
475 return Ok(None);
476 };
477
478 let row = neo4j_result.data.first();
479 let Some(row) = row else {
480 return Ok(None);
481 };
482
483 if row.row.is_empty() {
484 return Ok(None);
485 }
486
487 let embedding: Vec<f32> = row.row[0]
488 .as_array()
489 .map(|arr| {
490 arr.iter()
491 .filter_map(|v| v.as_f64().map(|f| f as f32))
492 .collect()
493 })
494 .unwrap_or_default();
495
496 if embedding.is_empty() {
497 Ok(None)
498 } else {
499 Ok(Some(embedding))
500 }
501 }
502
503 async fn delete_document(&self, id: &str) -> Result<(), VectorStoreError> {
504 let query = format!(
505 "MATCH (n:{label} {{{id_prop}: $id}}) \
506 DETACH DELETE n",
507 label = self.config.node_label,
508 id_prop = self.config.id_property,
509 );
510
511 self.run_query(&query, json!({ "id": id })).await?;
512 Ok(())
513 }
514
515 async fn count(&self) -> usize {
516 let query = format!(
517 "MATCH (n:{label}) RETURN count(n) AS cnt",
518 label = self.config.node_label,
519 );
520
521 let result = self.run_query(&query, json!({})).await;
522 match result {
523 Ok(response) => {
524 if let Some(neo4j_result) = response.results.first() {
525 if let Some(row) = neo4j_result.data.first() {
526 if let Some(cnt) = row.row.first() {
527 return cnt.as_u64().unwrap_or(0) as usize;
528 }
529 }
530 }
531 0
532 }
533 Err(_) => 0,
534 }
535 }
536
537 async fn clear(&self) -> Result<(), VectorStoreError> {
538 let query = format!(
539 "MATCH (n:{label}) \
540 DETACH DELETE n",
541 label = self.config.node_label,
542 );
543
544 self.run_query(&query, json!({})).await?;
545 Ok(())
546 }
547}
548
549#[cfg(test)]
550mod tests {
551 use super::*;
552
553 #[test]
554 fn test_config_new() {
555 let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "my_index");
556 assert_eq!(config.uri, "bolt://localhost:7687");
557 assert_eq!(config.username, "neo4j");
558 assert_eq!(config.password, "pass");
559 assert_eq!(config.index_name, "my_index");
560 assert_eq!(config.database, "neo4j");
561 }
562
563 #[test]
564 fn test_config_builder() {
565 let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx")
566 .with_database("mydb")
567 .with_node_label("Chunk")
568 .with_embedding_property("vec")
569 .with_content_property("text");
570 assert_eq!(config.database, "mydb");
571 assert_eq!(config.node_label, "Chunk");
572 assert_eq!(config.embedding_property, "vec");
573 assert_eq!(config.content_property, "text");
574 }
575
576 #[test]
577 fn test_config_default() {
578 let config = Neo4jConfig::default();
579 assert_eq!(config.uri, "bolt://localhost:7687");
580 assert_eq!(config.node_label, "Document");
581 assert_eq!(config.embedding_property, "embedding");
582 }
583
584 #[test]
585 fn test_tx_url_bolt() {
586 let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
587 let store = Neo4jVectorStore::new(config);
588 assert_eq!(store.tx_url(), "http://localhost:7687/db/neo4j/tx/commit");
589 }
590
591 #[test]
592 fn test_tx_url_neo4j_scheme() {
593 let config = Neo4jConfig::new("neo4j://host:7687", "neo4j", "pass", "idx");
594 let store = Neo4jVectorStore::new(config);
595 assert_eq!(store.tx_url(), "http://host:7687/db/neo4j/tx/commit");
596 }
597
598 #[test]
599 fn test_tx_url_bolt_s() {
600 let config = Neo4jConfig::new("bolt+s://host:7687", "neo4j", "pass", "idx");
601 let store = Neo4jVectorStore::new(config);
602 assert_eq!(store.tx_url(), "https://host:7687/db/neo4j/tx/commit");
603 }
604
605 #[test]
606 fn test_tx_url_custom_database() {
607 let config =
608 Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx").with_database("mydb");
609 let store = Neo4jVectorStore::new(config);
610 assert_eq!(store.tx_url(), "http://localhost:7687/db/mydb/tx/commit");
611 }
612
613 #[test]
614 fn test_base64_encode() {
615 let encoded = base64_encode("neo4j:password".to_string());
617 assert_eq!(encoded, "bmVvNGo6cGFzc3dvcmQ=");
618 }
619
620 #[test]
621 fn test_base64_encode_empty() {
622 let encoded = base64_encode(String::new());
623 assert_eq!(encoded, "");
624 }
625
626 #[test]
627 fn test_store_new() {
628 let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
629 let _store = Neo4jVectorStore::new(config);
630 }
631
632 #[test]
633 fn test_store_debug() {
634 let config = Neo4jConfig::new("bolt://localhost:7687", "neo4j", "pass", "idx");
635 let store = Neo4jVectorStore::new(config);
636 let debug_str = format!("{:?}", store);
637 assert!(debug_str.contains("Neo4jVectorStore"));
638 assert!(debug_str.contains("idx"));
639 }
640}