Add index name to non sortable search query

This commit is contained in:
Francesco 2024-02-12 20:23:48 +01:00
parent 15dafde21d
commit e6597cd57a
10 changed files with 42 additions and 19 deletions

View File

@ -634,6 +634,7 @@ impl SearchAggregator {
pub fn from_query(query: &SearchQuery, request: &HttpRequest) -> Self {
let SearchQuery {
q,
index_uid,
vector,
offset,
limit,

View File

@ -102,6 +102,7 @@ impl From<FacetSearchQuery> for SearchQuery {
SearchQuery {
q,
index_uid: None,
offset: DEFAULT_SEARCH_OFFSET(),
limit: DEFAULT_SEARCH_LIMIT(),
page: None,

View File

@ -128,6 +128,7 @@ impl From<SearchQueryGet> for SearchQuery {
};
Self {
index_uid: None,
q: other.q,
vector: other.vector.map(CS::into_inner),
offset: other.offset.0,
@ -203,9 +204,10 @@ pub async fn search_with_url_query(
let distribution = embed(&mut query, index_scheduler.get_ref(), &index).await?;
let search_result =
tokio::task::spawn_blocking(move || perform_search(&index, query, features, distribution))
.await?;
let search_result = tokio::task::spawn_blocking(move || {
perform_search(&index, index_uid.to_string(), query, features, distribution)
})
.await?;
if let Ok(ref search_result) = search_result {
aggregate.succeed(search_result);
}
@ -242,9 +244,10 @@ pub async fn search_with_post(
let distribution = embed(&mut query, index_scheduler.get_ref(), &index).await?;
let search_result =
tokio::task::spawn_blocking(move || perform_search(&index, query, features, distribution))
.await?;
let search_result = tokio::task::spawn_blocking(move || {
perform_search(&index, index_uid.to_string(), query, features, distribution)
})
.await?;
if let Ok(ref search_result) = search_result {
aggregate.succeed(search_result);
}

View File

@ -54,6 +54,8 @@ pub async fn multi_search_with_post(
{
debug!(on_index = query_index, parameters = ?query, "Multi-search");
let index_uid_name = &index_uid.clone();
// Check index from API key
if !index_scheduler.filters().is_index_authorized(&index_uid) {
return Err(AuthenticationError::InvalidToken).with_index(query_index);
@ -80,13 +82,13 @@ pub async fn multi_search_with_post(
.with_index(query_index)?;
let search_result = tokio::task::spawn_blocking(move || {
perform_search(&index, query, features, distribution)
perform_search(&index, index_uid.to_string().clone(), query, features, distribution)
})
.await
.with_index(query_index)?;
search_results.push(SearchResultWithIndex {
index_uid: index_uid.into_inner(),
index_uid: index_uid_name.to_string(),
result: search_result.with_index(query_index)?,
});
}

View File

@ -41,6 +41,7 @@ pub const DEFAULT_SEMANTIC_RATIO: fn() -> SemanticRatio = || SemanticRatio(0.5);
#[derive(Debug, Clone, Default, PartialEq, Deserr)]
#[deserr(error = DeserrJsonError, rename_all = camelCase, deny_unknown_fields)]
pub struct SearchQuery {
pub index_uid: Option<String>,
#[deserr(default, error = DeserrJsonError<InvalidSearchQ>)]
pub q: Option<String>,
#[deserr(default, error = DeserrJsonError<InvalidSearchVector>)]
@ -192,6 +193,7 @@ pub struct SearchQueryWithIndex {
impl SearchQueryWithIndex {
pub fn into_index_query(self) -> (IndexUid, SearchQuery) {
let index_uid_name = self.index_uid.clone().to_string().clone();
let SearchQueryWithIndex {
index_uid,
q,
@ -221,6 +223,7 @@ impl SearchQueryWithIndex {
index_uid,
SearchQuery {
q,
index_uid: Some(index_uid_name),
vector,
offset,
limit,
@ -378,12 +381,13 @@ pub fn add_search_rules(query: &mut SearchQuery, rules: IndexSearchRules) {
fn prepare_search<'t>(
index: &'t Index,
index_uid: &String,
rtxn: &'t RoTxn,
query: &'t SearchQuery,
features: RoFeatures,
distribution: Option<DistributionShift>,
) -> Result<(milli::Search<'t>, bool, usize, usize), MeilisearchHttpError> {
let mut search = index.search(rtxn);
let mut search = index.search(rtxn, index_uid.clone());
if query.vector.is_some() {
features.check_vector("Passing `vector` as a query parameter")?;
@ -486,6 +490,7 @@ fn prepare_search<'t>(
pub fn perform_search(
index: &Index,
index_uid: String,
query: SearchQuery,
features: RoFeatures,
distribution: Option<DistributionShift>,
@ -494,7 +499,7 @@ pub fn perform_search(
let rtxn = index.read_txn()?;
let (search, is_finite_pagination, max_total_hits, offset) =
prepare_search(index, &rtxn, &query, features, distribution)?;
prepare_search(index, &index_uid, &rtxn, &query, features, distribution)?;
let milli::SearchResult { documents_ids, matching_words, candidates, document_scores, .. } =
match &query.hybrid {
@ -725,7 +730,8 @@ pub fn perform_facet_search(
let before_search = Instant::now();
let rtxn = index.read_txn()?;
let (search, _, _, _) = prepare_search(index, &rtxn, &search_query, features, None)?;
let (search, _, _, _) =
prepare_search(index, &"".to_string(), &rtxn, &search_query, features, None)?;
let mut facet_search =
SearchForFacetValues::new(facet_name, search, search_query.hybrid.is_some());
if let Some(facet_query) = &facet_query {

View File

@ -122,8 +122,9 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
InvalidFilter(String),
#[error("Invalid type for filter subexpression: expected: {}, found: {1}.", .0.join(", "))]
InvalidFilterExpression(&'static [&'static str], Value),
#[error("Attribute `{}` is not sortable. {}",
#[error("Attribute `{}` of index `{}` is not sortable. {}",
.field,
.index,
match .valid_fields.is_empty() {
true => "This index does not have configured sortable attributes.".to_string(),
false => format!("Available sortable attributes are: `{}{}`.",
@ -132,7 +133,7 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
),
}
)]
InvalidSortableAttribute { field: String, valid_fields: BTreeSet<String>, hidden_fields: bool },
InvalidSortableAttribute { field: String, index: String, valid_fields: BTreeSet<String>, hidden_fields: bool },
#[error("Attribute `{}` is not facet-searchable. {}",
.field,
match .valid_fields.is_empty() {

View File

@ -1212,8 +1212,8 @@ impl Index {
FacetDistribution::new(rtxn, self)
}
pub fn search<'a>(&'a self, rtxn: &'a RoTxn) -> Search<'a> {
Search::new(rtxn, self)
pub fn search<'a>(&'a self, rtxn: &'a RoTxn, index_uid: String) -> Search<'a> {
Search::new(rtxn, self, index_uid)
}
/// Returns the index creation time.

View File

@ -115,6 +115,7 @@ impl<'a> Search<'a> {
// TODO: find classier way to achieve that than to reset vector and query params
// create separate keyword and semantic searches
let mut search = Search {
index_uid: self.index_uid.to_string().clone(),
query: self.query.clone(),
vector: self.vector.clone(),
filter: self.filter.clone(),

View File

@ -36,6 +36,7 @@ pub mod hybrid;
pub mod new;
pub struct Search<'a> {
index_uid: String,
query: Option<String>,
vector: Option<Vec<f32>>,
// this should be linked to the String in the query
@ -57,8 +58,9 @@ pub struct Search<'a> {
}
impl<'a> Search<'a> {
pub fn new(rtxn: &'a heed::RoTxn, index: &'a Index) -> Search<'a> {
pub fn new(rtxn: &'a heed::RoTxn, index: &'a Index, index_uid: String) -> Search<'a> {
Search {
index_uid,
query: None,
vector: None,
filter: None,
@ -156,7 +158,7 @@ impl<'a> Search<'a> {
pub fn execute_for_candidates(&self, has_vector_search: bool) -> Result<RoaringBitmap> {
if has_vector_search {
let ctx = SearchContext::new(self.index, self.rtxn);
let ctx = SearchContext::new(self.index, self.index_uid.to_string(), self.rtxn);
filtered_universe(&ctx, &self.filter)
} else {
Ok(self.execute()?.candidates)
@ -173,7 +175,7 @@ impl<'a> Search<'a> {
}
};
let mut ctx = SearchContext::new(self.index, self.rtxn);
let mut ctx = SearchContext::new(self.index, self.index_uid.to_string(), self.rtxn);
if let Some(searchable_attributes) = self.searchable_attributes {
ctx.searchable_attributes(searchable_attributes)?;
@ -224,6 +226,7 @@ impl<'a> Search<'a> {
impl fmt::Debug for Search<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let Search {
index_uid,
query,
vector: _,
filter,
@ -242,6 +245,7 @@ impl fmt::Debug for Search<'_> {
embedder_name,
} = self;
f.debug_struct("Search")
.field("index_uid", index_uid)
.field("query", query)
.field("vector", &"[...]")
.field("filter", filter)

View File

@ -58,6 +58,7 @@ use crate::{
/// A structure used throughout the execution of a search query.
pub struct SearchContext<'ctx> {
pub index: &'ctx Index,
pub index_uid: String,
pub txn: &'ctx RoTxn<'ctx>,
pub db_cache: DatabaseCache<'ctx>,
pub word_interner: DedupInterner<String>,
@ -68,9 +69,10 @@ pub struct SearchContext<'ctx> {
}
impl<'ctx> SearchContext<'ctx> {
pub fn new(index: &'ctx Index, txn: &'ctx RoTxn<'ctx>) -> Self {
pub fn new(index: &'ctx Index, index_uid: String, txn: &'ctx RoTxn<'ctx>) -> Self {
Self {
index,
index_uid,
txn,
db_cache: <_>::default(),
word_interner: <_>::default(),
@ -706,6 +708,7 @@ fn check_sort_criteria(ctx: &SearchContext, sort_criteria: Option<&Vec<AscDesc>>
return Err(UserError::InvalidSortableAttribute {
field: field.to_string(),
index: ctx.index_uid.clone(),
valid_fields,
hidden_fields,
}
@ -717,6 +720,7 @@ fn check_sort_criteria(ctx: &SearchContext, sort_criteria: Option<&Vec<AscDesc>>
return Err(UserError::InvalidSortableAttribute {
field: "_geo".to_string(),
index: ctx.index_uid.clone(),
valid_fields,
hidden_fields,
}