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

Commit

Permalink
[feat] bool query
Browse files Browse the repository at this point in the history
  • Loading branch information
franke1276 committed Jan 23, 2019
1 parent b0f3a6c commit a7f4a64
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/elasticsearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ impl Query {
pub fn prefix<F: Into<String>, V: Into<Value>>(field: F, value: V) -> Query {
Query::Prefix(field.into(), value.into())
}

pub fn bool_query(
must: Vec<Query>,
filter: Vec<Query>,
must_not: Vec<Query>,
should: Vec<Query>,
minimum_should_match: Option<i32>,
) -> Query {
Query::Bool(BoolQuery {
must,
filter,
must_not,
should,
minimum_should_match,
})
}
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
Expand Down
19 changes: 19 additions & 0 deletions src/elasticsearch_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<QueryRequest>(&json).unwrap();

assert_that(&actual).is_equal_to(query);
}

#[test]
fn test_prefix_query() {
let query = QueryRequest::new().with_query(Query::prefix("bla", "blub"));
Expand Down

0 comments on commit a7f4a64

Please sign in to comment.