use std::borrow::Cow;
use serde::Serialize;
mod bool;
mod function_score;
mod match_phrase;
mod match_phrase_prefix;
mod match_query;
mod range;
mod regexp;
mod term;
mod terms;
mod wildcard;
pub use bool::*;
pub use function_score::*;
pub use match_phrase::*;
pub use match_phrase_prefix::*;
pub use match_query::*;
pub use range::*;
pub use regexp::*;
use serde_json::Value;
pub use term::*;
pub use terms::*;
pub use wildcard::*;
use crate::ToOpenSearchJson;
#[derive(Debug, Clone, Serialize)]
#[serde(tag = "type", content = "params")]
pub enum QueryType<'a> {
Bool(BoolQuery<'a>),
FunctionScore(FunctionScoreQuery<'a>),
MatchPhrase(MatchPhraseQuery<'a>),
MatchPhrasePrefix(MatchPhrasePrefixQuery<'a>),
Match(MatchQuery<'a>),
Range(RangeQuery<'a>),
Regexp(RegexpQuery<'a>),
Term(TermQuery<'a>),
Terms(TermsQuery<'a>),
WildCard(WildcardQuery<'a>),
}
impl<'a> ToOpenSearchJson for QueryType<'a> {
fn to_json(&self) -> Value {
match self {
QueryType::Bool(bool_query) => bool_query.to_json(),
QueryType::FunctionScore(function_score) => function_score.to_json(),
QueryType::MatchPhrase(match_phrase) => match_phrase.to_json(),
QueryType::MatchPhrasePrefix(match_phrase_prefix) => match_phrase_prefix.to_json(),
QueryType::Match(match_query) => match_query.to_json(),
QueryType::Term(term) => term.to_json(),
QueryType::Terms(terms) => terms.to_json(),
QueryType::Range(range) => range.to_json(),
QueryType::WildCard(wildcard_query) => wildcard_query.to_json(),
QueryType::Regexp(regexp_query) => regexp_query.to_json(),
}
}
}
impl<'a> QueryType<'a> {
pub fn term<T: Into<Value>>(field: impl Into<Cow<'a, str>>, value: T) -> Self {
QueryType::Term(TermQuery::new(field, value))
}
pub fn terms<T: Into<Value>>(
field: impl Into<Cow<'a, str>>,
values: impl IntoIterator<Item = T>,
) -> Self {
QueryType::Terms(TermsQuery::new(field, values))
}
pub fn wildcard(
field: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
case_insensitive: bool,
) -> Self {
QueryType::WildCard(WildcardQuery::new(field, value, case_insensitive))
}
pub fn regexp(field: impl Into<Cow<'a, str>>, value: impl Into<Cow<'a, str>>) -> Self {
QueryType::Regexp(RegexpQuery::new(field, value))
}
pub fn match_phrase(field: impl Into<Cow<'a, str>>, query: impl Into<Cow<'a, str>>) -> Self {
QueryType::MatchPhrase(MatchPhraseQuery::new(field, query))
}
pub fn match_phrase_prefix(
field: impl Into<Cow<'a, str>>,
query: impl Into<Cow<'a, str>>,
) -> Self {
QueryType::MatchPhrasePrefix(MatchPhrasePrefixQuery::new(field, query))
}
pub fn bool_query() -> BoolQueryBuilder<'a> {
BoolQueryBuilder::new()
}
pub fn range(field: impl Into<Cow<'a, str>>) -> RangeQueryBuilder<'a> {
RangeQueryBuilder::new(field)
}
pub fn function_score() -> FunctionScoreQueryBuilder<'a> {
FunctionScoreQueryBuilder::new()
}
}