use crate::ast::Expr;
use crate::BinaryOp;
use relate_names::SmallName;
#[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 },
IdInSet { call: SmallName, rhs: Expr },
}
impl SearchFilterPred {
pub fn property_name(&self) -> Option<&str> {
self.parts().property
}
pub fn rhs(&self) -> Option<&Expr> {
self.parts().rhs
}
fn consumed_call(&self) -> Option<&SmallName> {
self.parts().call
}
pub fn is_entity_filter(&self) -> bool {
self.parts().entity_filter
}
fn parts(&self) -> FilterPredParts<'_> {
match self {
SearchFilterPred::Comparison { property, rhs, .. } => FilterPredParts {
property: Some(property),
rhs: Some(rhs),
call: None,
entity_filter: false,
},
SearchFilterPred::Exists { property } | SearchFilterPred::NotExists { property } => {
FilterPredParts {
property: Some(property),
rhs: None,
call: None,
entity_filter: false,
}
}
SearchFilterPred::InSet { property, rhs } => FilterPredParts {
property: Some(property),
rhs: Some(rhs),
call: None,
entity_filter: false,
},
SearchFilterPred::EntityInSet { rhs } => FilterPredParts {
property: None,
rhs: Some(rhs),
call: None,
entity_filter: true,
},
SearchFilterPred::IdInSet { call, rhs } => FilterPredParts {
property: None,
rhs: Some(rhs),
call: Some(call),
entity_filter: true,
},
}
}
}
struct FilterPredParts<'a> {
property: Option<&'a str>,
rhs: Option<&'a Expr>,
call: Option<&'a SmallName>,
entity_filter: bool,
}
#[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();
self.for_each_slot(&mut |slot| match slot {
SearchSlot::Expr(expr) => out.push(expr),
SearchSlot::Pred(pred) => {
if let Some(rhs) = pred.rhs() {
out.push(rhs);
}
}
});
out
}
pub fn consumed_function_calls(&self) -> Vec<&SmallName> {
let mut out = Vec::new();
self.for_each_slot(&mut |slot| {
if let SearchSlot::Pred(pred) = slot {
out.extend(pred.consumed_call());
}
});
out
}
fn for_each_slot<'a>(&'a self, f: &mut impl FnMut(SearchSlot<'a>)) {
match self {
SearchSpec::Index(index) => {
f(SearchSlot::Expr(&index.for_expr));
if let Some(expr) = &index.analyzer {
f(SearchSlot::Expr(expr));
}
for pred in &index.filters {
f(SearchSlot::Pred(pred));
}
if let Some(expr) = &index.offset_expr {
f(SearchSlot::Expr(expr));
}
f(SearchSlot::Expr(&index.limit_expr));
}
SearchSpec::Combine(combine) => {
for source in &combine.sources {
source.spec.for_each_slot(f);
if let Some(expr) = &source.weight_expr {
f(SearchSlot::Expr(expr));
}
}
if let Some(expr) = &combine.constant_expr {
f(SearchSlot::Expr(expr));
}
if let Some(expr) = &combine.limit_expr {
f(SearchSlot::Expr(expr));
}
}
}
}
}
enum SearchSlot<'a> {
Expr(&'a Expr),
Pred(&'a SearchFilterPred),
}
impl From<relate_parser::SearchKind> for SearchKind {
fn from(kind: relate_parser::SearchKind) -> Self {
match kind {
relate_parser::SearchKind::Vector => SearchKind::Vector,
relate_parser::SearchKind::Fulltext => SearchKind::Fulltext,
}
}
}