use crate::error::Error;
use crate::models::commit_type::CommitType;
use crate::models::context::SolrServerContext;
use crate::models::response::SolrResponse;
use crate::queries::request_builder::SolrRequestBuilder;
use serde::{Deserialize, Serialize};
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Debug)]
pub struct UpdateQuery {
handler: String,
commit_type: CommitType,
}
impl From<&UpdateQuery> for UpdateQuery {
fn from(query: &UpdateQuery) -> Self {
query.clone()
}
}
impl AsRef<UpdateQuery> for UpdateQuery {
fn as_ref(&self) -> &Self {
self
}
}
impl UpdateQuery {
pub fn new() -> Self {
UpdateQuery {
handler: "update".to_string(),
commit_type: CommitType::Hard,
}
}
pub fn handler<S: Into<String>>(mut self, handler: S) -> Self {
self.handler = handler.into();
self
}
pub fn commit_type(mut self, commit_type: CommitType) -> Self {
self.commit_type = commit_type;
self
}
pub async fn execute<C: AsRef<SolrServerContext>, D: Serialize, S: AsRef<str>>(
&self,
context: C,
collection: S,
data: &[D],
) -> Result<SolrResponse, Error> {
let mut query_params = vec![("overwrite", "true")];
match self.commit_type {
CommitType::Hard => query_params.push(("commit", "true")),
CommitType::Soft => query_params.push(("softCommit", "true")),
}
SolrRequestBuilder::new(
context.as_ref(),
format!("/solr/{}/{}", collection.as_ref(), self.handler.as_str()).as_str(),
)
.with_query_params(query_params.as_ref())
.send_post_with_json(data)
.await
}
}
#[derive(Clone, Default, Serialize, Deserialize, PartialEq, Debug)]
pub struct DeleteQuery {
handler: String,
commit_type: CommitType,
ids: Option<Vec<String>>,
queries: Option<Vec<String>>,
}
impl From<&DeleteQuery> for DeleteQuery {
fn from(query: &DeleteQuery) -> Self {
query.clone()
}
}
impl AsRef<DeleteQuery> for DeleteQuery {
fn as_ref(&self) -> &DeleteQuery {
self
}
}
impl DeleteQuery {
pub fn new() -> Self {
DeleteQuery {
handler: "update".to_string(),
commit_type: CommitType::Hard,
ids: None,
queries: None,
}
}
pub fn handler<S: Into<String>>(mut self, handler: S) -> Self {
self.handler = handler.into();
self
}
pub fn commit_type(mut self, commit_type: CommitType) -> Self {
self.commit_type = commit_type;
self
}
pub fn ids<S: Into<String>, V: IntoIterator<Item = S>, O: Into<Option<V>>>(
mut self,
ids: O,
) -> Self {
self.ids = ids
.into()
.map(|x| x.into_iter().map(|x| x.into()).collect());
self
}
pub fn queries<S: Into<String>, V: IntoIterator<Item = S>, O: Into<Option<V>>>(
mut self,
queries: O,
) -> Self {
self.queries = queries
.into()
.map(|x| x.into_iter().map(|x| x.into()).collect());
self
}
pub async fn execute<C: AsRef<SolrServerContext>, S: AsRef<str>>(
&self,
context: C,
collection: S,
) -> Result<SolrResponse, Error> {
let ids = self.ids.as_ref().map(|ids| {
ids.iter()
.map(|id| format!("<id>{}</id>", id))
.collect::<Vec<String>>()
.join("")
});
let queries = self.queries.as_ref().map(|queries| {
queries
.iter()
.map(|query| format!("<query>{}</query>", query))
.collect::<Vec<String>>()
.join("")
});
let mut query_params = vec![("overwrite", "true")];
match self.commit_type {
CommitType::Hard => query_params.push(("commit", "true")),
CommitType::Soft => query_params.push(("softCommit", "true")),
}
SolrRequestBuilder::new(
context.as_ref(),
format!("/solr/{}/{}", &collection.as_ref(), &self.handler).as_str(),
)
.with_query_params(query_params.as_ref())
.with_headers(vec![("Content-Type", "application/xml")])
.send_post_with_body(format!(
"<delete>{}{}</delete>",
ids.unwrap_or_default(),
queries.unwrap_or_default()
))
.await
}
}
#[cfg(feature = "blocking")]
use crate::runtime::RUNTIME;
#[cfg(feature = "blocking")]
impl UpdateQuery {
pub fn execute_blocking<D: Serialize, C: AsRef<SolrServerContext>, S: AsRef<str>>(
&self,
context: C,
collection: S,
data: &[D],
) -> Result<SolrResponse, Error> {
RUNTIME
.handle()
.block_on(self.execute(context, collection, data))
}
}
#[cfg(feature = "blocking")]
impl DeleteQuery {
pub fn execute_blocking<C: AsRef<SolrServerContext>, S: AsRef<str>>(
&self,
context: C,
collection: S,
) -> Result<SolrResponse, Error> {
RUNTIME.handle().block_on(self.execute(context, collection))
}
}