MCPcopy Create free account
hub / github.com/cosdata/cosdata / finalize_ann_results

Function finalize_ann_results

src/vector_store.rs:404–445  ·  view source on GitHub ↗
(
    collection: &Collection,
    hnsw_index: &HNSWIndex,
    results: Vec<(SharedLatestNode, MetricResult)>,
    query: &[f32],
    top_k: Option<usize>,
    return_raw_text: bool,
)

Source from the content-addressed store, hash-verified

402}
403
404pub fn finalize_ann_results(
405 collection: &Collection,
406 hnsw_index: &HNSWIndex,
407 results: Vec<(SharedLatestNode, MetricResult)>,
408 query: &[f32],
409 top_k: Option<usize>,
410 return_raw_text: bool,
411) -> Result<Vec<InternalSearchResult>, WaCustomError> {
412 let filtered = remove_duplicates_and_filter(hnsw_index, results, top_k, &hnsw_index.cache);
413 let mut results = Vec::with_capacity(top_k.unwrap_or(filtered.len()));
414 let mag_query = query.iter().map(|x| x * x).sum::<f32>().sqrt();
415
416 for (internal_id, _) in filtered {
417 let raw_emb = collection
418 .get_raw_emb_by_internal_id(&internal_id)
419 .ok_or_else(|| {
420 WaCustomError::NotFound(format!("raw embedding not found for id={internal_id:?}"))
421 })?;
422 let dense_values = raw_emb.dense_values.as_ref().ok_or_else(|| {
423 WaCustomError::NotFound("dense values not found for raw embedding".to_string())
424 })?;
425 let dp = dot_product_f32(query, dense_values);
426 let mag_raw = dense_values.iter().map(|x| x * x).sum::<f32>().sqrt();
427 let cs = dp / (mag_query * mag_raw);
428 results.push((
429 internal_id,
430 Some(raw_emb.id.clone()),
431 raw_emb.document_id.clone(),
432 cs,
433 if return_raw_text {
434 raw_emb.text.clone()
435 } else {
436 None
437 },
438 ));
439 }
440 results.sort_unstable_by(|(_, _, _, a, _), (_, _, _, b, _)| b.total_cmp(a));
441 if let Some(k) = top_k {
442 results.truncate(k);
443 }
444 Ok(results)
445}
446
447/// Intermediate representation of the embedding in a form that's
448/// ready for indexing.

Callers 1

search_internalMethod · 0.85

Calls 7

dot_product_f32Function · 0.85
cloneMethod · 0.80
lenMethod · 0.45
iterMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected