use crate::document::Document;
use serde_json::Value;
#[derive(Debug, Clone)]
pub struct Filter {
pub field: String,
pub value: Value,
}
#[derive(Debug, Clone, Default)]
pub struct Query {
pub text: Option<String>,
pub filters: Vec<Filter>,
pub limit: usize,
pub offset: usize,
}
pub const DEFAULT_LIMIT: usize = 20;
impl Query {
pub fn text(text: impl Into<String>) -> Self {
Self {
text: Some(text.into()),
limit: DEFAULT_LIMIT,
..Default::default()
}
}
pub fn all() -> Self {
Self {
limit: DEFAULT_LIMIT,
..Default::default()
}
}
pub fn filter(mut self, field: impl Into<String>, value: impl Into<Value>) -> Self {
self.filters.push(Filter {
field: field.into(),
value: value.into(),
});
self
}
pub fn limit(mut self, limit: usize) -> Self {
self.limit = limit;
self
}
pub fn offset(mut self, offset: usize) -> Self {
self.offset = offset;
self
}
pub fn effective_limit(&self) -> usize {
if self.limit == 0 {
DEFAULT_LIMIT
} else {
self.limit
}
}
}
impl From<&str> for Query {
fn from(s: &str) -> Self {
Query::text(s)
}
}
impl From<String> for Query {
fn from(s: String) -> Self {
Query::text(s)
}
}
#[derive(Debug, Clone)]
pub struct Hit {
pub id: String,
pub score: f32,
pub document: Document,
}