mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
Normalize the vectors during indexation and search
This commit is contained in:
parent
321ec5f3fa
commit
ab9f2269aa
4 changed files with 21 additions and 5 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue