use crate::ast::Expr;
use crate::BinaryOp;
#[derive(Debug, Clone, PartialEq)]
pub enum SearchSpec {
Index(Box<SearchIndexSpec>),
Combine(SearchCombineSpec),
}
#[derive(Debug, Clone, PartialEq)]
pub struct SearchIndexSpec {
pub kind: SearchKind,
pub index_name: String,
pub for_expr: Expr,
pub analyzer: Option<Box<Expr>>,
pub filters: Vec<SearchFilterPred>,
pub offset_expr: Option<Box<Expr>>,
pub limit_expr: Expr,
}
#[derive(Debug, Clone, PartialEq)]
pub enum SearchFilterPred {
Comparison {
property: String,
op: SearchOp,
rhs: Expr,
},
Exists { property: String },
NotExists { property: String },
InSet { property: String, rhs: Expr },
EntityInSet { rhs: Expr },
}
impl SearchFilterPred {
pub fn property_name(&self) -> Option<&str> {
match self {
SearchFilterPred::Comparison { property, .. }
| SearchFilterPred::Exists { property }
| SearchFilterPred::NotExists { property }
| SearchFilterPred::InSet { property, .. } => Some(property),
SearchFilterPred::EntityInSet { .. } => None,
}
}
pub fn rhs(&self) -> Option<&Expr> {
match self {
SearchFilterPred::Comparison { rhs, .. }
| SearchFilterPred::InSet { rhs, .. }
| SearchFilterPred::EntityInSet { rhs } => Some(rhs),
SearchFilterPred::Exists { .. } | SearchFilterPred::NotExists { .. } => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchOp {
Eq,
Lt,
Lte,
Gt,
Gte,
}
impl SearchOp {
pub fn flip(self) -> Self {
match self {
SearchOp::Eq => SearchOp::Eq,
SearchOp::Lt => SearchOp::Gt,
SearchOp::Gt => SearchOp::Lt,
SearchOp::Lte => SearchOp::Gte,
SearchOp::Gte => SearchOp::Lte,
}
}
pub fn from_binary(op: BinaryOp) -> Option<Self> {
Some(match op {
BinaryOp::Eq => SearchOp::Eq,
BinaryOp::Lt => SearchOp::Lt,
BinaryOp::Lte => SearchOp::Lte,
BinaryOp::Gt => SearchOp::Gt,
BinaryOp::Gte => SearchOp::Gte,
_ => return None,
})
}
}
impl From<SearchOp> for BinaryOp {
fn from(op: SearchOp) -> Self {
match op {
SearchOp::Eq => BinaryOp::Eq,
SearchOp::Lt => BinaryOp::Lt,
SearchOp::Lte => BinaryOp::Lte,
SearchOp::Gt => BinaryOp::Gt,
SearchOp::Gte => BinaryOp::Gte,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchKind {
Vector,
Fulltext,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SearchCombineSpec {
pub sources: Vec<SearchWeightedSource>,
pub constant_expr: Option<Box<Expr>>,
pub limit_expr: Option<Box<Expr>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SearchWeightedSource {
pub spec: SearchSpec,
pub weight_expr: Option<Box<Expr>>,
}
impl SearchSpec {
pub fn exprs(&self) -> Vec<&Expr> {
let mut out = Vec::new();
push_spec_exprs(self, &mut out);
out
}
}
fn push_spec_exprs<'a>(spec: &'a SearchSpec, out: &mut Vec<&'a Expr>) {
match spec {
SearchSpec::Index(index) => {
out.push(&index.for_expr);
if let Some(expr) = &index.analyzer {
out.push(expr);
}
for pred in &index.filters {
if let Some(rhs) = pred.rhs() {
out.push(rhs);
}
}
if let Some(expr) = &index.offset_expr {
out.push(expr);
}
out.push(&index.limit_expr);
}
SearchSpec::Combine(combine) => {
for source in &combine.sources {
push_spec_exprs(&source.spec, out);
if let Some(expr) = &source.weight_expr {
out.push(expr);
}
}
if let Some(expr) = &combine.constant_expr {
out.push(expr);
}
if let Some(expr) = &combine.limit_expr {
out.push(expr);
}
}
}
}