Skip to main content

selene_graph/vector_search/
score_shared.rs

1use selene_core::{CancellationChecker, DbString, NodeId, VectorMetric, VectorValue};
2
3use crate::error::GraphResult;
4use crate::shared::SharedGraph;
5
6use super::{
7    VectorCandidateSet, VectorNeighborDirection, VectorNeighborSearchOptions, VectorNodeSearchHit,
8    VectorSearchError,
9};
10
11impl SharedGraph {
12    /// Score explicit node candidates in the current snapshot.
13    ///
14    /// This loads one immutable snapshot and delegates to
15    /// [`crate::SeleneGraph::score_vector_nodes`].
16    pub fn score_vector_nodes(
17        &self,
18        property: &DbString,
19        query: &VectorValue,
20        candidates: &[NodeId],
21        metric: VectorMetric,
22        k: usize,
23    ) -> GraphResult<Vec<VectorNodeSearchHit>> {
24        self.read()
25            .score_vector_nodes(property, query, candidates, metric, k)
26    }
27
28    /// Lock-free read snapshot wrapper for
29    /// [`crate::SeleneGraph::score_vector_nodes_checked`].
30    pub fn score_vector_nodes_checked(
31        &self,
32        property: &DbString,
33        query: &VectorValue,
34        candidates: &[NodeId],
35        metric: VectorMetric,
36        k: usize,
37        checker: CancellationChecker<'_>,
38    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
39        self.read()
40            .score_vector_nodes_checked(property, query, candidates, metric, k, checker)
41    }
42
43    /// Score one explicit node candidate set per query in the current snapshot.
44    ///
45    /// This loads one immutable snapshot and delegates to
46    /// [`crate::SeleneGraph::score_vector_nodes_batch`].
47    pub fn score_vector_nodes_batch<C>(
48        &self,
49        property: &DbString,
50        queries: &[VectorValue],
51        candidate_sets: &[C],
52        metric: VectorMetric,
53        k: usize,
54    ) -> GraphResult<Vec<Vec<VectorNodeSearchHit>>>
55    where
56        C: AsRef<[NodeId]>,
57    {
58        self.read()
59            .score_vector_nodes_batch(property, queries, candidate_sets, metric, k)
60    }
61
62    /// Lock-free read snapshot wrapper for
63    /// [`crate::SeleneGraph::score_vector_nodes_batch_checked`].
64    pub fn score_vector_nodes_batch_checked<C>(
65        &self,
66        property: &DbString,
67        queries: &[VectorValue],
68        candidate_sets: &[C],
69        metric: VectorMetric,
70        k: usize,
71        checker: CancellationChecker<'_>,
72    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError>
73    where
74        C: AsRef<[NodeId]>,
75    {
76        self.read().score_vector_nodes_batch_checked(
77            property,
78            queries,
79            candidate_sets,
80            metric,
81            k,
82            checker,
83        )
84    }
85
86    /// Score one canonical candidate set against one query in the current snapshot.
87    ///
88    /// This loads one immutable snapshot and delegates to
89    /// [`crate::SeleneGraph::score_vector_candidate_set`].
90    pub fn score_vector_candidate_set(
91        &self,
92        property: &DbString,
93        query: &VectorValue,
94        candidates: &VectorCandidateSet,
95        metric: VectorMetric,
96        k: usize,
97    ) -> GraphResult<Vec<VectorNodeSearchHit>> {
98        self.read()
99            .score_vector_candidate_set(property, query, candidates, metric, k)
100    }
101
102    /// Lock-free read snapshot wrapper for
103    /// [`crate::SeleneGraph::score_vector_candidate_set_checked`].
104    pub fn score_vector_candidate_set_checked(
105        &self,
106        property: &DbString,
107        query: &VectorValue,
108        candidates: &VectorCandidateSet,
109        metric: VectorMetric,
110        k: usize,
111        checker: CancellationChecker<'_>,
112    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
113        self.read()
114            .score_vector_candidate_set_checked(property, query, candidates, metric, k, checker)
115    }
116
117    /// Score one canonical candidate set per query in the current snapshot.
118    ///
119    /// This loads one immutable snapshot and delegates to
120    /// [`crate::SeleneGraph::score_vector_candidate_sets_batch`].
121    pub fn score_vector_candidate_sets_batch(
122        &self,
123        property: &DbString,
124        queries: &[VectorValue],
125        candidate_sets: &[VectorCandidateSet],
126        metric: VectorMetric,
127        k: usize,
128    ) -> GraphResult<Vec<Vec<VectorNodeSearchHit>>> {
129        self.read()
130            .score_vector_candidate_sets_batch(property, queries, candidate_sets, metric, k)
131    }
132
133    /// Lock-free read snapshot wrapper for
134    /// [`crate::SeleneGraph::score_vector_candidate_sets_batch_checked`].
135    pub fn score_vector_candidate_sets_batch_checked(
136        &self,
137        property: &DbString,
138        queries: &[VectorValue],
139        candidate_sets: &[VectorCandidateSet],
140        metric: VectorMetric,
141        k: usize,
142        checker: CancellationChecker<'_>,
143    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
144        self.read().score_vector_candidate_sets_batch_checked(
145            property,
146            queries,
147            candidate_sets,
148            metric,
149            k,
150            checker,
151        )
152    }
153
154    /// Score vector-valued neighbors reached from one anchor in the current snapshot.
155    pub fn score_vector_neighbors(
156        &self,
157        property: &DbString,
158        query: &VectorValue,
159        anchor: NodeId,
160        options: VectorNeighborSearchOptions<'_>,
161    ) -> GraphResult<Vec<VectorNodeSearchHit>> {
162        self.read()
163            .score_vector_neighbors(property, query, anchor, options)
164    }
165
166    /// Lock-free read snapshot wrapper for
167    /// [`crate::SeleneGraph::score_vector_neighbors_checked`].
168    pub fn score_vector_neighbors_checked(
169        &self,
170        property: &DbString,
171        query: &VectorValue,
172        anchor: NodeId,
173        options: VectorNeighborSearchOptions<'_>,
174        checker: CancellationChecker<'_>,
175    ) -> Result<Vec<VectorNodeSearchHit>, VectorSearchError> {
176        self.read()
177            .score_vector_neighbors_checked(property, query, anchor, options, checker)
178    }
179
180    /// Score one anchor's vector-valued neighbors for each query in the current snapshot.
181    pub fn score_vector_neighbors_batch(
182        &self,
183        property: &DbString,
184        queries: &[VectorValue],
185        anchors: &[NodeId],
186        options: VectorNeighborSearchOptions<'_>,
187    ) -> GraphResult<Vec<Vec<VectorNodeSearchHit>>> {
188        self.read()
189            .score_vector_neighbors_batch(property, queries, anchors, options)
190    }
191
192    /// Lock-free read snapshot wrapper for
193    /// [`crate::SeleneGraph::score_vector_neighbors_batch_checked`].
194    pub fn score_vector_neighbors_batch_checked(
195        &self,
196        property: &DbString,
197        queries: &[VectorValue],
198        anchors: &[NodeId],
199        options: VectorNeighborSearchOptions<'_>,
200        checker: CancellationChecker<'_>,
201    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
202        self.read()
203            .score_vector_neighbors_batch_checked(property, queries, anchors, options, checker)
204    }
205
206    /// Expand one canonical root set per query, then score it in the current snapshot.
207    ///
208    /// This loads one immutable snapshot and delegates to
209    /// [`crate::SeleneGraph::score_vector_expanded_candidate_sets_batch`].
210    pub fn score_vector_expanded_candidate_sets_batch(
211        &self,
212        property: &DbString,
213        queries: &[VectorValue],
214        root_sets: &[VectorCandidateSet],
215        options: VectorNeighborSearchOptions<'_>,
216    ) -> GraphResult<Vec<Vec<VectorNodeSearchHit>>> {
217        self.read()
218            .score_vector_expanded_candidate_sets_batch(property, queries, root_sets, options)
219    }
220
221    /// Lock-free read snapshot wrapper for
222    /// [`crate::SeleneGraph::score_vector_expanded_candidate_sets_batch_checked`].
223    pub fn score_vector_expanded_candidate_sets_batch_checked(
224        &self,
225        property: &DbString,
226        queries: &[VectorValue],
227        root_sets: &[VectorCandidateSet],
228        options: VectorNeighborSearchOptions<'_>,
229        checker: CancellationChecker<'_>,
230    ) -> Result<Vec<Vec<VectorNodeSearchHit>>, VectorSearchError> {
231        self.read()
232            .score_vector_expanded_candidate_sets_batch_checked(
233                property, queries, root_sets, options, checker,
234            )
235    }
236
237    /// Return canonical vector-score candidates reached from one graph anchor
238    /// in the current snapshot.
239    #[must_use]
240    pub fn vector_neighbor_candidates(
241        &self,
242        anchor: NodeId,
243        edge_label: &DbString,
244        direction: VectorNeighborDirection,
245    ) -> VectorCandidateSet {
246        self.read()
247            .vector_neighbor_candidates(anchor, edge_label, direction)
248    }
249
250    /// Expand canonical candidates through one labeled graph hop in the current snapshot.
251    #[must_use]
252    pub fn expand_vector_candidate_set(
253        &self,
254        roots: &VectorCandidateSet,
255        edge_label: &DbString,
256        direction: VectorNeighborDirection,
257    ) -> VectorCandidateSet {
258        self.read()
259            .expand_vector_candidate_set(roots, edge_label, direction)
260    }
261
262    /// Lock-free read snapshot wrapper for
263    /// [`crate::SeleneGraph::expand_vector_candidate_set_checked`].
264    pub fn expand_vector_candidate_set_checked(
265        &self,
266        roots: &VectorCandidateSet,
267        edge_label: &DbString,
268        direction: VectorNeighborDirection,
269        checker: CancellationChecker<'_>,
270    ) -> Result<VectorCandidateSet, VectorSearchError> {
271        self.read()
272            .expand_vector_candidate_set_checked(roots, edge_label, direction, checker)
273    }
274
275    /// Expand one canonical root set per query through one graph hop in the
276    /// current snapshot.
277    pub fn expand_vector_candidate_sets_batch_checked(
278        &self,
279        root_sets: &[VectorCandidateSet],
280        edge_label: &DbString,
281        direction: VectorNeighborDirection,
282        k: usize,
283        checker: CancellationChecker<'_>,
284    ) -> Result<Vec<VectorCandidateSet>, VectorSearchError> {
285        self.read().expand_vector_candidate_sets_batch_checked(
286            root_sets, edge_label, direction, k, checker,
287        )
288    }
289}