#查询 #DSL #Elasticsearch #表达式 #布尔 #转换 #短语

elastic_query

将布尔表达式转换为 Elasticsearch DSL

8 个不稳定版本 (3 个破坏性更新)

0.4.4 2019年2月4日
0.4.3 2019年2月1日
0.4.1 2019年1月30日
0.3.0 2019年1月29日
0.1.0 2019年1月29日

#1423数据库接口

每月下载量:31

MIT 许可证

18KB
278

概述

将布尔表达式转换为 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"    |
                                          |                            }                       |
                                          |                        }                           |
                                          |                    }]                              |
                                          |                }                                   |
                                          |            }]                                      |
                                          |        }                                           |
                                          |    }                                               |
                                          |}                                                   |
                                          +----------------------------------------------------+

示例

添加

[dependencies]
elastic_query = "0.4.4"

到你的 Cargo.toml,然后按照以下方式使用

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);
}

语法

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" }

依赖

~2.5–3.5MB
~72K SLoC