Normalize the vectors during indexation and search

This commit is contained in:
Kerollmops 2023-06-20 11:45:29 +02:00 committed by Clément Renault
parent 321ec5f3fa
commit ab9f2269aa
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
4 changed files with 21 additions and 5 deletions

View file

@ -285,6 +285,18 @@ pub fn normalize_facet(original: &str) -> String {
CompatibilityDecompositionNormalizer.normalize_str(original.trim()).to_lowercase()
}
/// Normalize a vector by dividing the dimensions by the lenght of it.
pub fn normalize_vector(mut vector: Vec<f32>) -> Vec<f32> {
let squared: f32 = vector.iter().map(|x| x * x).sum();
let length = squared.sqrt();
if length <= f32::EPSILON {
vector
} else {
vector.iter_mut().for_each(|x| *x = *x / length);
vector
}
}
#[cfg(test)]
mod tests {
use serde_json::json;