search properly aliasing now
This commit is contained in:
@@ -18,11 +18,17 @@ pub enum ConstraintMode {
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SearchConstraint {
|
||||
pub column: String,
|
||||
pub targets: Vec<SearchConstraintTarget>,
|
||||
pub query: String,
|
||||
pub mode: ConstraintMode,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SearchConstraintTarget {
|
||||
pub table_name: Option<String>,
|
||||
pub column: String,
|
||||
}
|
||||
|
||||
pub fn build_master_query(
|
||||
index: &Index,
|
||||
fields: &SchemaFields,
|
||||
@@ -34,14 +40,7 @@ pub fn build_master_query(
|
||||
let mut has_search_clause = false;
|
||||
|
||||
for constraint in must {
|
||||
let predicate = match constraint.mode {
|
||||
ConstraintMode::Exact => {
|
||||
exact_predicate(fields, &constraint.column, &constraint.query)?
|
||||
}
|
||||
ConstraintMode::Fuzzy => {
|
||||
fuzzy_predicate_scoped(fields, &constraint.column, &constraint.query)?
|
||||
}
|
||||
};
|
||||
let predicate = constraint_predicate(fields, constraint)?;
|
||||
clauses.push((Occur::Must, predicate));
|
||||
has_search_clause = true;
|
||||
}
|
||||
@@ -68,6 +67,47 @@ pub fn build_master_query(
|
||||
Ok(Box::new(BooleanQuery::new(clauses)))
|
||||
}
|
||||
|
||||
fn constraint_predicate(
|
||||
fields: &SchemaFields,
|
||||
constraint: &SearchConstraint,
|
||||
) -> Result<Box<dyn Query>, Status> {
|
||||
let mut alternatives = Vec::new();
|
||||
|
||||
for target in &constraint.targets {
|
||||
let column_predicate = match constraint.mode {
|
||||
ConstraintMode::Exact => exact_predicate(fields, &target.column, &constraint.query)?,
|
||||
ConstraintMode::Fuzzy => {
|
||||
fuzzy_predicate_scoped(fields, &target.column, &constraint.query)?
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(table_name) = &target.table_name {
|
||||
let table_term = Term::from_field_text(fields.table_name, table_name);
|
||||
alternatives.push((
|
||||
Occur::Should,
|
||||
Box::new(BooleanQuery::new(vec![
|
||||
(
|
||||
Occur::Must,
|
||||
Box::new(TermQuery::new(table_term, IndexRecordOption::Basic))
|
||||
as Box<dyn Query>,
|
||||
),
|
||||
(Occur::Must, column_predicate),
|
||||
])) as Box<dyn Query>,
|
||||
));
|
||||
} else {
|
||||
alternatives.push((Occur::Should, column_predicate));
|
||||
}
|
||||
}
|
||||
|
||||
if alternatives.is_empty() {
|
||||
return Err(Status::invalid_argument(
|
||||
"constraint has no searchable column targets",
|
||||
));
|
||||
}
|
||||
|
||||
Ok(Box::new(BooleanQuery::new(alternatives)))
|
||||
}
|
||||
|
||||
fn exact_predicate(
|
||||
fields: &SchemaFields,
|
||||
column: &str,
|
||||
|
||||
Reference in New Issue
Block a user