From d3cd5ea68924430c0bd0a10f0059dd6c4bd2cf4e Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Mon, 3 Mar 2025 14:45:57 +0100 Subject: [PATCH] Check if the geo fields changed additionally to the other faceted fields when reindexing facets --- .../milli/src/update/new/document_change.rs | 26 ++++++++++++++++++- .../new/extract/faceted/extract_facets.rs | 7 +++-- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/milli/src/update/new/document_change.rs b/crates/milli/src/update/new/document_change.rs index 38369a4d7..8a8ac4bb3 100644 --- a/crates/milli/src/update/new/document_change.rs +++ b/crates/milli/src/update/new/document_change.rs @@ -1,5 +1,6 @@ use bumpalo::Bump; use heed::RoTxn; +use serde_json::Value; use super::document::{ Document as _, DocumentFromDb, DocumentFromVersions, MergedDocument, Versions, @@ -10,7 +11,7 @@ use super::vector_document::{ use crate::attribute_patterns::PatternMatch; use crate::documents::FieldIdMapper; use crate::vector::EmbeddingConfigs; -use crate::{DocumentId, Index, Result}; +use crate::{DocumentId, Index, InternalError, Result}; pub enum DocumentChange<'doc> { Deletion(Deletion<'doc>), @@ -243,6 +244,29 @@ impl<'doc> Update<'doc> { Ok(has_deleted_fields) } + /// Returns `true` if the geo fields have changed. + pub fn has_changed_for_geo_fields<'t, Mapper: FieldIdMapper>( + &self, + rtxn: &'t RoTxn, + index: &'t Index, + mapper: &'t Mapper, + ) -> Result { + let current = self.current(rtxn, index, mapper)?; + let current_geo = current.geo_field()?; + let updated_geo = self.only_changed_fields().geo_field()?; + match (current_geo, updated_geo) { + (Some(current_geo), Some(updated_geo)) => { + let current: Value = + serde_json::from_str(current_geo.get()).map_err(InternalError::SerdeJson)?; + let updated: Value = + serde_json::from_str(updated_geo.get()).map_err(InternalError::SerdeJson)?; + Ok(current != updated) + } + (None, None) => Ok(false), + _ => Ok(true), + } + } + pub fn only_changed_vectors( &self, doc_alloc: &'doc Bump, diff --git a/crates/milli/src/update/new/extract/faceted/extract_facets.rs b/crates/milli/src/update/new/extract/faceted/extract_facets.rs index b3aa8f984..1b08307a2 100644 --- a/crates/milli/src/update/new/extract/faceted/extract_facets.rs +++ b/crates/milli/src/update/new/extract/faceted/extract_facets.rs @@ -117,7 +117,7 @@ impl FacetedDocidsExtractor { }, ), DocumentChange::Update(inner) => { - if !inner.has_changed_for_fields( + let has_changed = inner.has_changed_for_fields( &mut |field_name| { match_faceted_field( field_name, @@ -130,7 +130,10 @@ impl FacetedDocidsExtractor { rtxn, index, context.db_fields_ids_map, - )? { + )?; + let has_changed_for_geo_fields = + inner.has_changed_for_geo_fields(rtxn, index, context.db_fields_ids_map)?; + if !has_changed && !has_changed_for_geo_fields { return Ok(()); }