use std::sync::Arc;
use std::sync::OnceLock;
use arrow_schema::DataType;
use datafusion::execution::SendableRecordBatchStream;
use datafusion::logical_expr::ColumnarValue;
use uni_plugin::traits::procedure::{
NamedArgType, ProcedureContext, ProcedureMode, ProcedurePlugin, ProcedureSignature,
};
use uni_plugin::traits::scalar::ArgType;
use uni_plugin::{FnError, PluginError, PluginRegistrar, QName, SideEffects};
use crate::procedures_plugin::vector::{fts_query_yields, run_search_procedure};
use crate::query::df_graph::search_procedures::run_fts_query;
fn signature() -> &'static ProcedureSignature {
static SIG: OnceLock<ProcedureSignature> = OnceLock::new();
SIG.get_or_init(|| ProcedureSignature {
args: vec![
NamedArgType {
name: smol_str::SmolStr::new("label"),
ty: ArgType::Primitive(DataType::Utf8),
default: None,
doc: "Vertex label to search.".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("property"),
ty: ArgType::Primitive(DataType::Utf8),
default: None,
doc: "FTS property name on the label.".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("search_term"),
ty: ArgType::Primitive(DataType::Utf8),
default: None,
doc: "Free-text search term.".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("k"),
ty: ArgType::Primitive(DataType::Int64),
default: None,
doc: "Number of top hits to return.".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("filter"),
ty: ArgType::Primitive(DataType::Utf8),
default: None,
doc: "Optional pushdown filter expression.".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("threshold"),
ty: ArgType::Primitive(DataType::Float64),
default: None,
doc: "Optional minimum score threshold (post-filter).".to_owned(),
},
NamedArgType {
name: smol_str::SmolStr::new("options"),
ty: ArgType::CypherValue,
default: None,
doc: "Optional reranker / extra options map.".to_owned(),
},
],
yields: fts_query_yields(),
mode: ProcedureMode::Read,
side_effects: SideEffects::ReadOnly,
retry_contract: None,
batch_input: None,
docs: "BM25 full-text search over an FTS index with optional cross-encoder rerank."
.to_owned(),
})
}
#[derive(Debug)]
struct FtsQueryProc;
impl ProcedurePlugin for FtsQueryProc {
fn signature(&self) -> &ProcedureSignature {
signature()
}
fn invoke(
&self,
ctx: ProcedureContext<'_>,
args: &[ColumnarValue],
) -> Result<SendableRecordBatchStream, FnError> {
run_search_procedure(
"uni.fts.query",
&ctx,
args,
signature(),
|host, uni_args, yield_items, output_schema| async move {
let target_properties = host.target_properties().clone();
run_fts_query(
&host,
&uni_args,
&yield_items,
&target_properties,
&output_schema,
)
.await
},
)
}
}
pub fn register_into(r: &mut PluginRegistrar<'_>) -> Result<(), PluginError> {
r.procedure(
QName::new("uni", "fts.query"),
signature().clone(),
Arc::new(FtsQueryProc),
)?;
Ok(())
}