diff --git a/src/elasticsearch.rs b/src/elasticsearch.rs index 464d1c3..e4ff049 100644 --- a/src/elasticsearch.rs +++ b/src/elasticsearch.rs @@ -1,3 +1,4 @@ +use super::encode_url_component; use super::serde_field_value; use super::{IntoClientRequest, Problem}; use actix_web::client::{ClientRequest, ClientRequestBuilder}; @@ -386,3 +387,48 @@ where .map_err(Problem::from) } } + +pub struct ElasticsearchUrlBuilder { + elasticsearch_base_url: String, + index_name: String, +} + +impl ElasticsearchUrlBuilder { + pub fn new(elasticsearch_base_url: String, index_name: String) -> ElasticsearchUrlBuilder { + ElasticsearchUrlBuilder { + elasticsearch_base_url, + index_name, + } + } + + pub fn index(&self) -> String { + format!("{}/{}", self.elasticsearch_base_url, self.index_name) + } + + pub fn mapping(&self) -> String { + format!("{}/_mapping/_doc", self.index()) + } + + pub fn search(&self) -> String { + format!("{}/_search", self.index()) + } + + pub fn mget(&self) -> String { + format!("{}/_doc/_mget", self.index()) + } + + pub fn create(&self, id: String) -> String { + format!( + "{}/_doc/{}/_create?refresh=true", + self.index(), + encode_url_component(id), + ) + } + + pub fn update(&self, id: String) -> String { + format!("{}/_doc/{}/_update", self.index(), encode_url_component(id),) + } + pub fn delete(&self, id: String) -> String { + format!("{}/_doc/{}", self.index(), encode_url_component(id),) + } +} diff --git a/src/elasticsearch_test.rs b/src/elasticsearch_test.rs index 7fbc425..e8ced96 100644 --- a/src/elasticsearch_test.rs +++ b/src/elasticsearch_test.rs @@ -1,4 +1,4 @@ -use super::elasticsearch::{Query, QueryRequest, SortOrder}; +use super::elasticsearch::{ElasticsearchUrlBuilder, Query, QueryRequest, SortOrder}; use serde_json; use spectral::prelude::*; @@ -58,3 +58,40 @@ fn test_sort_without_query() { assert_that(&actual).is_equal_to(query); } + +#[test] +fn test_build_url_create() { + let url_builder = ElasticsearchUrlBuilder::new("http://server".to_string(), "INDEX_NAME".to_string()); + + assert_that(&url_builder.create("die id".to_string())) + .is_equal_to("http://server/INDEX_NAME/_doc/die+id/_create?refresh=true".to_string()); +} + +#[test] +fn test_build_url_search() { + let url_builder = ElasticsearchUrlBuilder::new("http://server".to_string(), "INDEX_NAME".to_string()); + + assert_that(&url_builder.search()).is_equal_to("http://server/INDEX_NAME/_search".to_string()); +} + +#[test] +fn test_build_url_mget() { + let url_builder = ElasticsearchUrlBuilder::new("http://server".to_string(), "INDEX_NAME".to_string()); + + assert_that(&url_builder.mget()).is_equal_to("http://server/INDEX_NAME/_doc/_mget".to_string()); +} + +#[test] +fn test_build_url_mapping() { + let url_builder = ElasticsearchUrlBuilder::new("http://server".to_string(), "INDEX_NAME".to_string()); + + assert_that(&url_builder.mapping()).is_equal_to("http://server/INDEX_NAME/_mapping/_doc".to_string()); +} + +#[test] +fn test_build_url_update() { + let url_builder = ElasticsearchUrlBuilder::new("http://server".to_string(), "INDEX_NAME".to_string()); + + assert_that(&url_builder.update("die id".to_string())) + .is_equal_to("http://server/INDEX_NAME/_doc/die+id/_update".to_string()); +}