Convert Bool Expression to Elasticsearch DSL.
+----------------------------------------------------+
|{ |
| "query": { |
| "bool": { |
| "must": [{ |
| "match": { |
| "a": { |
| "query": "1", |
| "type": "phrase" |
| } |
| } |
| }, { |
| "bool": { |
| "must": [{ |
| "match": { |
| "b": { |
+-----------------------------+ | "query": "2", |
|a = 1 and (b = 2 and (c = 3))|---------->| "type": "phrase" |
+-----------------------------+ | } |
| } |
| }, { |
| "match": { |
| "c": { |
| "query": "3", |
| "type": "phrase" |
| } |
| } |
| }] |
| } |
| }] |
| } |
| } |
|} |
+----------------------------------------------------+
Example:
Add:
toml
[dependencies]
elastic_query = "0.4.1"
To your Cargo.toml
, then use as follows:
```rust extern crate elastic_query;
fn main() { let result = elasticquery::convert("a = 1 and b in (1,2,3)".tostring(), 0, 100, vec![], vec![]).unwrap(); println!("{}", result); }
```
Grammar:
```peg bool_expr = { SOI ~ expr ~ EOI }
expr = { andexpr | orexpr | parenbool | compexpr }
andexpr = { (parenbool | comp_expr) ~ "and" ~ (expr) }
orexpr = { (parenbool | comp_expr) ~ "or" ~ (expr) }
paren_bool = { "(" ~ expr ~ ")" }
comp_expr = { field ~ op ~ value }
field = @{ (ASCIIALPHA ~ ASCIIALPHANUMERIC*) } op = { eq | neq | opin | opnotin | gt | gte | lt | lte | like | notlike } eq = { "=" } neq = { "!=" | "<>"} opin = { "in" } opnotin= { "not" ~ "in"} gt = { ">" } gte = { ">=" } lt = { "<" } lte = { "<=" } like = { "like" } notlike = { "not" ~ "like" }
value = { stringliteral | numliteral | "(" ~ stringliteral ~("," ~ stringliteral)* ~ ")" | "(" ~ numliteral ~("," ~ numliteral)* ~ ")" }
numliteral = @{ "-"? ~ ("0" | ASCIINONZERODIGIT ~ ASCIIDIGIT) ~ ("." ~ ASCII_DIGIT)? ~ (^"e" ~ ("+" | "-")? ~ ASCII_DIGIT+)? }
stringliteral = ${ "\"" ~ string ~ "\"" } string = @{ char* } char = { !("\"" | "\") ~ ANY | "\" ~ ("\"" | "\" | "/" | "b" | "f" | "n" | "r" | "t") | "\" ~ ("u" ~ ASCIIHEX_DIGIT{4}) }
WHITESPACE = _{ " " | "\n" | "\r" } ```