From a7f4a641760b8b67a1f4b3975c68ca93b1c03e3e Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Wed, 23 Jan 2019 13:48:42 +0100 Subject: [PATCH] [feat] bool query --- src/elasticsearch.rs | 16 ++++++++++++++++ src/elasticsearch_test.rs | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/elasticsearch.rs b/src/elasticsearch.rs index 416f2e6..29abd79 100644 --- a/src/elasticsearch.rs +++ b/src/elasticsearch.rs @@ -114,6 +114,22 @@ impl Query { pub fn prefix, V: Into>(field: F, value: V) -> Query { Query::Prefix(field.into(), value.into()) } + + pub fn bool_query( + must: Vec, + filter: Vec, + must_not: Vec, + should: Vec, + minimum_should_match: Option, + ) -> Query { + Query::Bool(BoolQuery { + must, + filter, + must_not, + should, + minimum_should_match, + }) + } } #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)] diff --git a/src/elasticsearch_test.rs b/src/elasticsearch_test.rs index afe0290..7fbc425 100644 --- a/src/elasticsearch_test.rs +++ b/src/elasticsearch_test.rs @@ -14,6 +14,25 @@ fn test_term_query() { assert_that(&actual).is_equal_to(query); } +#[test] +fn test_bool_query() { + let query = QueryRequest::new().with_query(Query::bool_query( + vec![Query::term("fieldname", "fieldvalue")], + vec![], + vec![], + vec![], + None, + )); + + let json = serde_json::to_string(&query).unwrap(); + + assert_that(&json.as_str()).is_equal_to(r#"{"query":{"bool":{"must":[{"term":{"fieldname":"fieldvalue"}}],"filter":[],"must_not":[],"should":[],"minimum_should_match":null}}}"#); + + let actual = serde_json::from_str::(&json).unwrap(); + + assert_that(&actual).is_equal_to(query); +} + #[test] fn test_prefix_query() { let query = QueryRequest::new().with_query(Query::prefix("bla", "blub"));