Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
[feat] elasticsearch url builder
Browse files Browse the repository at this point in the history
  • Loading branch information
franke1276 committed Jan 28, 2019
1 parent 29ead83 commit dae335c
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/elasticsearch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::encode_url_component;
use super::serde_field_value;
use super::{IntoClientRequest, Problem};
use actix_web::client::{ClientRequest, ClientRequestBuilder};
Expand Down Expand Up @@ -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),)
}
}
39 changes: 38 additions & 1 deletion src/elasticsearch_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::elasticsearch::{Query, QueryRequest, SortOrder};
use super::elasticsearch::{ElasticsearchUrlBuilder, Query, QueryRequest, SortOrder};
use serde_json;
use spectral::prelude::*;

Expand Down Expand Up @@ -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());
}

0 comments on commit dae335c

Please sign in to comment.