Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/daemon/src/handler/archive_ask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,7 @@ async fn retrieve_candidates(
let lexical_ids: Vec<mxr_core::MessageId> = if want_lexical {
let query = lexical_query_with_filters(question, filters);
let page = if has_structured_filters(filters) {
let ast = mxr_search::parse_query(&query).map_err(|e| e.to_string())?;
let schema = mxr_search::MxrSchema::build();
let query = mxr_search::QueryBuilder::new(&schema).build(&ast);
let query = mxr_search::build_query(&query).map_err(|e| e.to_string())?;
state
.search
.search_ast(query, pool_size, 0, SortOrder::Relevance)
Expand Down
5 changes: 2 additions & 3 deletions crates/daemon/src/handler/mutations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2444,10 +2444,9 @@ async fn select_sender_footprint(
let mut envelopes = Vec::new();
let mut offset = 0usize;
const PAGE_SIZE: usize = 500;
let ast = mxr_search::parse_query(&query).map_err(|e| e.to_string())?;
let schema = mxr_search::MxrSchema::build();
let compiled = mxr_search::CompiledQuery::parse(&query).map_err(|e| e.to_string())?;
loop {
let query_ast = mxr_search::QueryBuilder::new(&schema).build(&ast);
let query_ast = compiled.build();
let page = state
.search
.search_ast(
Expand Down
2 changes: 1 addition & 1 deletion crates/search/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use mail_query::{
};

pub use index::{SearchIndex, SearchPage, SearchResult};
pub use query_builder::QueryBuilder;
pub use query_builder::{build_query, CompiledQuery, QueryBuilder};
pub use schema::MxrSchema;
pub use service::{SearchIndexEntry, SearchServiceHandle, SearchUpdateBatch};

Expand Down
64 changes: 62 additions & 2 deletions crates/search/src/query_builder.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::schema::MxrSchema;
use crate::{
DateBound, DateValue, FilterKind, QueryField, QueryNode, RelativeUnit, SizeOp,
FILTER_OWED_REPLY, FILTER_REPLY_LATER,
parse_query, DateBound, DateValue, FilterKind, ParseError, QueryField, QueryNode, RelativeUnit,
SizeOp, FILTER_OWED_REPLY, FILTER_REPLY_LATER,
};
use chrono::{Datelike, Local, NaiveDate};
use std::ops::Bound;
Expand Down Expand Up @@ -589,6 +589,38 @@ fn normalize_message_id(value: &str) -> String {
.to_ascii_lowercase()
}

/// A parsed query plus the schema needed to compile it, so callers
/// don't repeat the `parse_query` + `MxrSchema::build` + `QueryBuilder`
/// dance at every search site. Holds the schema so it isn't rebuilt per
/// call in a paging loop — Tantivy queries are consumed by `search_ast`,
/// so each page needs a fresh `build()`.
pub struct CompiledQuery {
schema: MxrSchema,
ast: QueryNode,
}

impl CompiledQuery {
/// Parse a query string and capture the schema once.
pub fn parse(query: &str) -> Result<Self, ParseError> {
Ok(Self {
schema: MxrSchema::build(),
ast: parse_query(query)?,
})
}

/// Produce a fresh Tantivy query for one search call.
pub fn build(&self) -> Box<dyn Query> {
QueryBuilder::new(&self.schema).build(&self.ast)
}
}

/// Parse and compile a query string into a Tantivy query in one call.
/// For a paging loop, use [`CompiledQuery::parse`] once and `build()`
/// per page instead so the schema isn't rebuilt each iteration.
pub fn build_query(query: &str) -> Result<Box<dyn Query>, ParseError> {
Ok(CompiledQuery::parse(query)?.build())
}

#[cfg(test)]
mod tests {
#![expect(
Expand All @@ -601,6 +633,34 @@ mod tests {
use crate::test_fixtures::TestEnvelopeBuilder;
use crate::{parse_query, ParseError};
use mxr_core::types::*;

#[test]
fn build_query_matches_the_manual_parse_schema_build_path() {
for query in [
"from:alice@example.com",
"is:unread subject:report",
"has:link-heavy",
] {
let schema = MxrSchema::build();
let manual = QueryBuilder::new(&schema).build(&parse_query(query).unwrap());
let helper = build_query(query).unwrap();
assert_eq!(
format!("{manual:?}"),
format!("{helper:?}"),
"build_query must match the manual path for {query:?}"
);
// CompiledQuery builds an equivalent query and can do so
// repeatedly (each call yields a fresh boxed query).
let compiled = CompiledQuery::parse(query).unwrap();
assert_eq!(format!("{:?}", compiled.build()), format!("{manual:?}"));
assert_eq!(format!("{:?}", compiled.build()), format!("{manual:?}"));
}
}

#[test]
fn build_query_propagates_parse_errors() {
assert!(build_query("subject:(unbalanced").is_err());
}
use proptest::prelude::*;

fn make_test_envelope(
Expand Down
Loading