Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cch123/elastic-rs

comvert bool expression to elasticsearch DSL in rust
https://github.com/cch123/elastic-rs

boolean-expression dsl elastic elasticsearch rust sql

Last synced: 2 months ago
JSON representation

comvert bool expression to elasticsearch DSL in rust

Awesome Lists containing this project

README

        

# Overview

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.4"
```

To your `Cargo.toml`, then use as follows:

```rust
extern crate elastic_query;

fn main() {
let result = elastic_query::convert("a = 1 and b in (1,2,3)".to_string(), 0, 100, vec![], vec![]).unwrap();
println!("{}", result);
}

```

Grammar:

```peg
bool_expr = { SOI ~ expr ~ EOI }

expr = {
(paren_bool | comp_expr) ~ ( (and_op|or_op)~ (paren_bool| comp_expr))*
}

and_op = { "and" }
or_op = { "or" }

paren_bool = { "(" ~ expr ~ ")" }

comp_expr = { field ~ op ~ value }

field = @{ (ASCII_ALPHA ~ ASCII_ALPHANUMERIC*) }
op = { eq | neq | op_in | op_not_in | gt | gte | lt | lte | like | not_like }
eq = { "=" }
neq = { "!=" | "<>"}
op_in = { "in" }
op_not_in= { "not" ~ "in"}
gt = { ">" }
gte = { ">=" }
lt = { "<" }
lte = { "<=" }
like = { "like" }
not_like = { "not" ~ "like" }

value = {
string_literal
| num_literal
| "(" ~ string_literal ~("," ~ string_literal)* ~ ")"
| "(" ~ num_literal ~("," ~ num_literal)* ~ ")"
}

num_literal = @{
"-"?
~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*)
~ ("." ~ ASCII_DIGIT*)?
~ (^"e" ~ ("+" | "-")? ~ ASCII_DIGIT+)?
}

string_literal = ${ "\"" ~ string ~ "\"" }
string = @{ char* }
char = {
!("\"" | "\\") ~ ANY
| "\\" ~ ("\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t")
| "\\" ~ ("u" ~ ASCII_HEX_DIGIT{4})
}

WHITESPACE = _{ " " | "\n" | "\r" }
```