Accept the null JSON value as the value of _vectors

This commit is contained in:
Kerollmops 2023-08-14 16:03:55 +02:00
parent ef3d098b4d
commit c53841e166
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
3 changed files with 16 additions and 13 deletions

View File

@ -666,6 +666,7 @@ fn compute_semantic_score(query: &[f32], vectors: Value) -> milli::Result<Option
.map_err(InternalError::SerdeJson)?; .map_err(InternalError::SerdeJson)?;
Ok(vectors Ok(vectors
.into_iter() .into_iter()
.flatten()
.map(|v| OrderedFloat(dot_product_similarity(query, &v))) .map(|v| OrderedFloat(dot_product_similarity(query, &v)))
.max() .max()
.map(OrderedFloat::into_inner)) .map(OrderedFloat::into_inner))

View File

@ -293,15 +293,15 @@ pub fn normalize_facet(original: &str) -> String {
#[derive(serde::Serialize, serde::Deserialize, Debug)] #[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(transparent)] #[serde(transparent)]
pub struct VectorOrArrayOfVectors { pub struct VectorOrArrayOfVectors {
#[serde(with = "either::serde_untagged")] #[serde(with = "either::serde_untagged_optional")]
inner: either::Either<Vec<f32>, Vec<Vec<f32>>>, inner: Option<either::Either<Vec<f32>, Vec<Vec<f32>>>>,
} }
impl VectorOrArrayOfVectors { impl VectorOrArrayOfVectors {
pub fn into_array_of_vectors(self) -> Vec<Vec<f32>> { pub fn into_array_of_vectors(self) -> Option<Vec<Vec<f32>>> {
match self.inner { match self.inner? {
either::Either::Left(vector) => vec![vector], either::Either::Left(vector) => Some(vec![vector]),
either::Either::Right(vectors) => vectors, either::Either::Right(vectors) => Some(vectors),
} }
} }
} }

View File

@ -33,7 +33,7 @@ pub fn extract_vector_points<R: io::Read + io::Seek>(
// lazily get it when needed // lazily get it when needed
let document_id = || -> Value { let document_id = || -> Value {
let document_id = obkv.get(primary_key_id).unwrap(); let document_id = obkv.get(primary_key_id).unwrap();
serde_json::from_slice(document_id).unwrap() from_slice(document_id).unwrap()
}; };
// first we retrieve the _vectors field // first we retrieve the _vectors field
@ -50,12 +50,14 @@ pub fn extract_vector_points<R: io::Read + io::Seek>(
} }
}; };
for (i, vector) in vectors.into_iter().enumerate().take(u16::MAX as usize) { if let Some(vectors) = vectors {
let index = u16::try_from(i).unwrap(); for (i, vector) in vectors.into_iter().enumerate().take(u16::MAX as usize) {
let mut key = docid_bytes.to_vec(); let index = u16::try_from(i).unwrap();
key.extend_from_slice(&index.to_be_bytes()); let mut key = docid_bytes.to_vec();
let bytes = cast_slice(&vector); key.extend_from_slice(&index.to_be_bytes());
writer.insert(key, bytes)?; let bytes = cast_slice(&vector);
writer.insert(key, bytes)?;
}
} }
} }
// else => the `_vectors` object was `null`, there is nothing to do // else => the `_vectors` object was `null`, there is nothing to do