Fix primary key fid order

This commit is contained in:
Louis Dureuil 2024-11-08 00:05:12 +01:00
parent 4706a0eb49
commit 8a314ab81d
No known key found for this signature in database
4 changed files with 21 additions and 6 deletions

1
Cargo.lock generated
View File

@ -2623,6 +2623,7 @@ dependencies = [
"meilisearch-types", "meilisearch-types",
"memmap2", "memmap2",
"page_size", "page_size",
"raw-collections",
"rayon", "rayon",
"roaring", "roaring",
"serde", "serde",

View File

@ -22,6 +22,7 @@ flate2 = "1.0.30"
meilisearch-auth = { path = "../meilisearch-auth" } meilisearch-auth = { path = "../meilisearch-auth" }
meilisearch-types = { path = "../meilisearch-types" } meilisearch-types = { path = "../meilisearch-types" }
page_size = "0.6.0" page_size = "0.6.0"
raw-collections = { git = "https://github.com/dureuill/raw-collections.git", version = "0.1.0" }
rayon = "1.10.0" rayon = "1.10.0"
roaring = { version = "0.10.6", features = ["serde"] } roaring = { version = "0.10.6", features = ["serde"] }
serde = { version = "1.0.204", features = ["derive"] } serde = { version = "1.0.204", features = ["derive"] }

View File

@ -43,6 +43,7 @@ use meilisearch_types::milli::{self, Filter};
use meilisearch_types::settings::{apply_settings_to_builder, Settings, Unchecked}; use meilisearch_types::settings::{apply_settings_to_builder, Settings, Unchecked};
use meilisearch_types::tasks::{Details, IndexSwap, Kind, KindWithContent, Status, Task}; use meilisearch_types::tasks::{Details, IndexSwap, Kind, KindWithContent, Status, Task};
use meilisearch_types::{compression, Index, VERSION_FILE_NAME}; use meilisearch_types::{compression, Index, VERSION_FILE_NAME};
use raw_collections::RawMap;
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
use time::macros::format_description; use time::macros::format_description;
use time::OffsetDateTime; use time::OffsetDateTime;
@ -1318,7 +1319,12 @@ impl IndexScheduler {
index, index,
&mut new_fields_ids_map, &mut new_fields_ids_map,
primary_key.as_deref(), primary_key.as_deref(),
first_document.as_ref(), first_document
.map(|raw| RawMap::from_raw_value(raw, &indexer_alloc))
.transpose()
.map_err(|error| {
milli::Error::UserError(milli::UserError::SerdeJson(error))
})?,
)? )?
.map_err(milli::Error::from)?; .map_err(milli::Error::from)?;

View File

@ -12,6 +12,7 @@ use heed::{RoTxn, RwTxn};
use itertools::{merge_join_by, EitherOrBoth}; use itertools::{merge_join_by, EitherOrBoth};
pub use partial_dump::PartialDump; pub use partial_dump::PartialDump;
use rand::SeedableRng as _; use rand::SeedableRng as _;
use raw_collections::RawMap;
use rayon::ThreadPool; use rayon::ThreadPool;
use time::OffsetDateTime; use time::OffsetDateTime;
pub use update_by_function::UpdateByFunction; pub use update_by_function::UpdateByFunction;
@ -24,7 +25,7 @@ use super::word_fst_builder::{PrefixData, PrefixDelta, WordFstBuilder};
use super::words_prefix_docids::{ use super::words_prefix_docids::{
compute_word_prefix_docids, compute_word_prefix_fid_docids, compute_word_prefix_position_docids, compute_word_prefix_docids, compute_word_prefix_fid_docids, compute_word_prefix_position_docids,
}; };
use super::{StdResult, TopLevelMap}; use super::StdResult;
use crate::documents::{PrimaryKey, DEFAULT_PRIMARY_KEY}; use crate::documents::{PrimaryKey, DEFAULT_PRIMARY_KEY};
use crate::facet::FacetType; use crate::facet::FacetType;
use crate::fields_ids_map::metadata::{FieldIdMapWithMetadata, MetadataBuilder}; use crate::fields_ids_map::metadata::{FieldIdMapWithMetadata, MetadataBuilder};
@ -733,7 +734,7 @@ pub fn retrieve_or_guess_primary_key<'a>(
index: &Index, index: &Index,
new_fields_ids_map: &mut FieldsIdsMap, new_fields_ids_map: &mut FieldsIdsMap,
primary_key_from_op: Option<&'a str>, primary_key_from_op: Option<&'a str>,
first_document: Option<&'a TopLevelMap<'a>>, first_document: Option<RawMap<'a>>,
) -> Result<StdResult<(PrimaryKey<'a>, bool), UserError>> { ) -> Result<StdResult<(PrimaryKey<'a>, bool), UserError>> {
// make sure that we have a declared primary key, either fetching it from the index or attempting to guess it. // make sure that we have a declared primary key, either fetching it from the index or attempting to guess it.
@ -769,12 +770,18 @@ pub fn retrieve_or_guess_primary_key<'a>(
None => return Ok(Err(UserError::NoPrimaryKeyCandidateFound)), None => return Ok(Err(UserError::NoPrimaryKeyCandidateFound)),
}; };
let mut guesses: Vec<&str> = first_document let guesses: Result<Vec<&str>> = first_document
.keys() .keys()
.map(AsRef::as_ref) .filter_map(|name| {
.filter(|name| name.to_lowercase().ends_with(DEFAULT_PRIMARY_KEY)) let Some(_) = new_fields_ids_map.insert(name) else {
return Some(Err(UserError::AttributeLimitReached.into()));
};
name.to_lowercase().ends_with(DEFAULT_PRIMARY_KEY).then_some(Ok(name))
})
.collect(); .collect();
let mut guesses = guesses?;
// sort the keys in lexicographical order, so that fields are always in the same order. // sort the keys in lexicographical order, so that fields are always in the same order.
guesses.sort_unstable(); guesses.sort_unstable();