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
//! Vector, text, and hybrid search operations for GrafeoDB.
#[cfg(any(feature = "text-index", feature = "hybrid-search"))]
use grafeo_common::types::NodeId;
#[cfg(feature = "vector-index")]
use grafeo_common::types::Value;
#[cfg(any(feature = "text-index", feature = "hybrid-search"))]
use grafeo_common::utils::error::Error;
#[cfg(any(
feature = "vector-index",
feature = "text-index",
feature = "hybrid-search"
))]
use grafeo_common::utils::error::Result;
impl super::GrafeoDB {
/// Computes a node allowlist from property filters.
///
/// Supports equality filters (scalar values) and operator filters (Map values
/// with `$`-prefixed keys like `$gt`, `$lt`, `$in`, `$contains`).
///
/// Returns `None` if filters is `None` or empty (meaning no filtering),
/// or `Some(set)` with the intersection (possibly empty).
#[cfg(feature = "vector-index")]
fn compute_filter_allowlist(
&self,
label: &str,
filters: Option<&std::collections::HashMap<String, Value>>,
) -> Option<std::collections::HashSet<NodeId>> {
let filters = filters.filter(|f| !f.is_empty())?;
// Start with all nodes for this label
let label_nodes: std::collections::HashSet<NodeId> =
self.lpg_store().nodes_by_label(label).into_iter().collect();
let mut allowlist = label_nodes;
for (key, filter_value) in filters {
// Check if this is an operator filter (Map with $-prefixed keys)
let is_operator_filter = matches!(filter_value, Value::Map(ops) if ops.keys().any(|k| k.as_str().starts_with('$')));
if is_operator_filter {
// Operator filter: scan only the current allowlist (not all nodes).
// This is much faster when a prior filter has already narrowed the set.
let prop_key = grafeo_common::types::PropertyKey::new(key);
allowlist.retain(|&node_id| {
self.lpg_store()
.get_node_property(node_id, &prop_key)
.is_some_and(|v| grafeo_core::LpgStore::matches_filter(&v, filter_value))
});
} else {
// Equality filter: use indexed lookup when available
let matching: std::collections::HashSet<NodeId> = self
.lpg_store()
.find_nodes_by_property(key, filter_value)
.into_iter()
.collect();
allowlist = allowlist.intersection(&matching).copied().collect();
}
// Short-circuit: empty intersection means no results possible
if allowlist.is_empty() {
return Some(allowlist);
}
}
Some(allowlist)
}
/// Searches for the k nearest neighbors of a query vector.
///
/// Uses the HNSW index created by [`create_vector_index`](Self::create_vector_index).
///
/// # Arguments
///
/// * `label` - Node label that was indexed
/// * `property` - Property that was indexed
/// * `query` - Query vector (slice of floats)
/// * `k` - Number of nearest neighbors to return
/// * `ef` - Search beam width (higher = better recall, slower). Uses index default if `None`.
/// * `filters` - Optional property equality filters. Only nodes matching all
/// `(key, value)` pairs will appear in results.
///
/// # Returns
///
/// Vector of `(NodeId, distance)` pairs sorted by distance ascending.
#[cfg(feature = "vector-index")]
pub fn vector_search(
&self,
label: &str,
property: &str,
query: &[f32],
k: usize,
ef: Option<usize>,
filters: Option<&std::collections::HashMap<String, Value>>,
) -> Result<Vec<(grafeo_common::types::NodeId, f32)>> {
let index = self.lpg_store().get_vector_index(label, property).ok_or_else(|| {
grafeo_common::utils::error::Error::Internal(format!(
"No vector index found for :{label}({property}). Call create_vector_index() first."
))
})?;
let accessor =
grafeo_core::index::vector::PropertyVectorAccessor::new(&**self.lpg_store(), property);
let results = match self.compute_filter_allowlist(label, filters) {
Some(allowlist) => match ef {
Some(ef_val) => {
index.search_with_ef_and_filter(query, k, ef_val, &allowlist, &accessor)
}
None => index.search_with_filter(query, k, &allowlist, &accessor),
},
None => match ef {
Some(ef_val) => index.search_with_ef(query, k, ef_val, &accessor),
None => index.search(query, k, &accessor),
},
};
Ok(results)
}
/// Searches for nearest neighbors for multiple query vectors in parallel.
///
/// Uses rayon parallel iteration under the hood for multi-core throughput.
///
/// # Arguments
///
/// * `label` - Node label that was indexed
/// * `property` - Property that was indexed
/// * `queries` - Batch of query vectors
/// * `k` - Number of nearest neighbors per query
/// * `ef` - Search beam width (uses index default if `None`)
/// * `filters` - Optional property equality filters
#[cfg(feature = "vector-index")]
pub fn batch_vector_search(
&self,
label: &str,
property: &str,
queries: &[Vec<f32>],
k: usize,
ef: Option<usize>,
filters: Option<&std::collections::HashMap<String, Value>>,
) -> Result<Vec<Vec<(grafeo_common::types::NodeId, f32)>>> {
let index = self.lpg_store().get_vector_index(label, property).ok_or_else(|| {
grafeo_common::utils::error::Error::Internal(format!(
"No vector index found for :{label}({property}). Call create_vector_index() first."
))
})?;
let accessor =
grafeo_core::index::vector::PropertyVectorAccessor::new(&**self.lpg_store(), property);
let results = match self.compute_filter_allowlist(label, filters) {
Some(allowlist) => match ef {
Some(ef_val) => {
index.batch_search_with_ef_and_filter(queries, k, ef_val, &allowlist, &accessor)
}
None => index.batch_search_with_filter(queries, k, &allowlist, &accessor),
},
None => match ef {
Some(ef_val) => index.batch_search_with_ef(queries, k, ef_val, &accessor),
None => index.batch_search(queries, k, &accessor),
},
};
Ok(results)
}
/// Searches for diverse nearest neighbors using Maximal Marginal Relevance (MMR).
///
/// MMR balances relevance (similarity to query) with diversity (dissimilarity
/// among selected results). This is the algorithm used by LangChain's
/// `mmr_traversal_search()` for RAG applications.
///
/// # Arguments
///
/// * `label` - Node label that was indexed
/// * `property` - Property that was indexed
/// * `query` - Query vector
/// * `k` - Number of diverse results to return
/// * `fetch_k` - Number of initial candidates from HNSW (default: `4 * k`)
/// * `lambda` - Relevance vs. diversity in \[0, 1\] (default: 0.5).
/// 1.0 = pure relevance, 0.0 = pure diversity.
/// * `ef` - HNSW search beam width (uses index default if `None`)
/// * `filters` - Optional property equality filters
///
/// # Returns
///
/// `(NodeId, distance)` pairs in MMR selection order. The f32 is the original
/// distance from the query, matching [`vector_search`](Self::vector_search).
#[cfg(feature = "vector-index")]
#[allow(clippy::too_many_arguments)]
pub fn mmr_search(
&self,
label: &str,
property: &str,
query: &[f32],
k: usize,
fetch_k: Option<usize>,
lambda: Option<f32>,
ef: Option<usize>,
filters: Option<&std::collections::HashMap<String, Value>>,
) -> Result<Vec<(grafeo_common::types::NodeId, f32)>> {
use grafeo_core::index::vector::mmr_select;
let index = self.lpg_store().get_vector_index(label, property).ok_or_else(|| {
grafeo_common::utils::error::Error::Internal(format!(
"No vector index found for :{label}({property}). Call create_vector_index() first."
))
})?;
let accessor =
grafeo_core::index::vector::PropertyVectorAccessor::new(&**self.lpg_store(), property);
let fetch_k = fetch_k.unwrap_or(k.saturating_mul(4).max(k));
let lambda = lambda.unwrap_or(0.5);
// Step 1: Fetch candidates from HNSW (with optional filter)
let initial_results = match self.compute_filter_allowlist(label, filters) {
Some(allowlist) => match ef {
Some(ef_val) => {
index.search_with_ef_and_filter(query, fetch_k, ef_val, &allowlist, &accessor)
}
None => index.search_with_filter(query, fetch_k, &allowlist, &accessor),
},
None => match ef {
Some(ef_val) => index.search_with_ef(query, fetch_k, ef_val, &accessor),
None => index.search(query, fetch_k, &accessor),
},
};
if initial_results.is_empty() {
return Ok(Vec::new());
}
// Step 2: Retrieve stored vectors for MMR pairwise comparison
use grafeo_core::index::vector::VectorAccessor;
let candidates: Vec<(grafeo_common::types::NodeId, f32, std::sync::Arc<[f32]>)> =
initial_results
.into_iter()
.filter_map(|(id, dist)| accessor.get_vector(id).map(|vec| (id, dist, vec)))
.collect();
// Step 3: Build slice-based candidates for mmr_select
let candidate_refs: Vec<(grafeo_common::types::NodeId, f32, &[f32])> = candidates
.iter()
.map(|(id, dist, vec)| (*id, *dist, vec.as_ref()))
.collect();
// Step 4: Run MMR selection
let metric = index.config().metric;
Ok(mmr_select(query, &candidate_refs, k, lambda, metric))
}
/// Searches a text index using BM25 scoring.
///
/// Returns up to `k` results as `(NodeId, score)` pairs sorted by
/// descending relevance score.
///
/// # Errors
///
/// Returns an error if no text index exists for this label+property.
#[cfg(feature = "text-index")]
pub fn text_search(
&self,
label: &str,
property: &str,
query: &str,
k: usize,
) -> Result<Vec<(NodeId, f64)>> {
let index = self
.lpg_store()
.get_text_index(label, property)
.ok_or_else(|| {
Error::Internal(format!(
"No text index found for :{label}({property}). Call create_text_index() first."
))
})?;
Ok(index.read().search(query, k))
}
/// Performs hybrid search combining text (BM25) and vector similarity.
///
/// Runs both text search and vector search, then fuses results using
/// the specified method (default: Reciprocal Rank Fusion).
///
/// # Arguments
///
/// * `label` - Node label to search within
/// * `text_property` - Property indexed for text search
/// * `vector_property` - Property indexed for vector search
/// * `query_text` - Text query for BM25 search
/// * `query_vector` - Vector query for similarity search (optional)
/// * `k` - Number of results to return
/// * `fusion` - Score fusion method (default: RRF with k=60)
///
/// # Errors
///
/// Returns an error if the required indexes don't exist.
#[cfg(feature = "hybrid-search")]
#[allow(clippy::too_many_arguments)]
pub fn hybrid_search(
&self,
label: &str,
text_property: &str,
vector_property: &str,
query_text: &str,
query_vector: Option<&[f32]>,
k: usize,
fusion: Option<grafeo_core::index::text::FusionMethod>,
) -> Result<Vec<(NodeId, f64)>> {
use grafeo_core::index::text::fuse_results;
let fusion_method = fusion.unwrap_or_default();
let mut sources: Vec<Vec<(NodeId, f64)>> = Vec::new();
// Text search
if let Some(text_index) = self.lpg_store().get_text_index(label, text_property) {
let text_results = text_index.read().search(query_text, k * 2);
if !text_results.is_empty() {
sources.push(text_results);
}
}
// Vector search (if query vector provided)
if let Some(query_vec) = query_vector
&& let Some(vector_index) = self.lpg_store().get_vector_index(label, vector_property)
{
let accessor = grafeo_core::index::vector::PropertyVectorAccessor::new(
&**self.lpg_store(),
vector_property,
);
let vector_results = vector_index.search(query_vec, k * 2, &accessor);
if !vector_results.is_empty() {
sources.push(
vector_results
.into_iter()
.map(|(id, dist)| (id, f64::from(dist)))
.collect(),
);
}
}
if sources.is_empty() {
return Ok(Vec::new());
}
Ok(fuse_results(&sources, &fusion_method, k))
}
}