2024-05-15 15:02:26 +02:00
|
|
|
//! The fieldids weights map is in charge of storing linking the searchable fields with their weights.
|
|
|
|
|
2024-05-06 14:49:45 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2024-06-05 11:21:06 +02:00
|
|
|
use crate::vector::parsed_vectors::RESERVED_VECTORS_FIELD_NAME;
|
|
|
|
use crate::{FieldId, FieldsIdsMap, Weight};
|
2024-05-06 14:49:45 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct FieldidsWeightsMap {
|
|
|
|
map: HashMap<FieldId, Weight>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FieldidsWeightsMap {
|
2024-05-15 15:02:26 +02:00
|
|
|
/// Insert a field id -> weigth into the map.
|
|
|
|
/// If the map did not have this key present, `None` is returned.
|
|
|
|
/// If the map did have this key present, the value is updated, and the old value is returned.
|
2024-05-06 14:49:45 +02:00
|
|
|
pub fn insert(&mut self, fid: FieldId, weight: Weight) -> Option<Weight> {
|
|
|
|
self.map.insert(fid, weight)
|
|
|
|
}
|
|
|
|
|
2024-05-15 17:16:10 +02:00
|
|
|
/// Create the map from the fields ids maps.
|
|
|
|
/// Should only be called in the case there are NO searchable attributes.
|
2024-05-16 01:06:33 +02:00
|
|
|
/// All the fields will be inserted in the order of the fields ids map with a weight of 0.
|
2024-05-15 17:16:10 +02:00
|
|
|
pub fn from_field_id_map_without_searchable(fid_map: &FieldsIdsMap) -> Self {
|
2024-05-21 17:08:45 +02:00
|
|
|
FieldidsWeightsMap {
|
|
|
|
map: fid_map
|
|
|
|
.iter()
|
|
|
|
.filter(|(_fid, name)| !crate::is_faceted_by(name, RESERVED_VECTORS_FIELD_NAME))
|
|
|
|
.map(|(fid, _name)| (fid, 0))
|
|
|
|
.collect(),
|
|
|
|
}
|
2024-05-15 17:16:10 +02:00
|
|
|
}
|
|
|
|
|
2024-05-15 15:02:26 +02:00
|
|
|
/// Removes a field id from the map, returning the associated weight previously in the map.
|
2024-05-06 14:49:45 +02:00
|
|
|
pub fn remove(&mut self, fid: FieldId) -> Option<Weight> {
|
|
|
|
self.map.remove(&fid)
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:02:26 +02:00
|
|
|
/// Returns weight corresponding to the key.
|
2024-05-06 14:49:45 +02:00
|
|
|
pub fn weight(&self, fid: FieldId) -> Option<Weight> {
|
|
|
|
self.map.get(&fid).copied()
|
|
|
|
}
|
|
|
|
|
2024-05-15 15:02:26 +02:00
|
|
|
/// Returns highest weight contained in the map if any.
|
2024-05-06 14:49:45 +02:00
|
|
|
pub fn max_weight(&self) -> Option<Weight> {
|
|
|
|
self.map.values().copied().max()
|
|
|
|
}
|
2024-05-07 17:56:40 +02:00
|
|
|
|
2024-05-15 15:02:26 +02:00
|
|
|
/// Return an iterator visiting all field ids in arbitrary order.
|
2024-05-14 17:20:57 +02:00
|
|
|
pub fn ids(&self) -> impl Iterator<Item = FieldId> + '_ {
|
2024-05-07 17:56:40 +02:00
|
|
|
self.map.keys().copied()
|
|
|
|
}
|
2024-05-06 14:49:45 +02:00
|
|
|
}
|