Build elasticsearch query with C-style boolean expression.
see ./query_builder_test.go
The v1.0.0+ version is broken due to a bad commit :(, use v2 instead.
import "github.com/r4ve1/esqb/v2"
- Use the expression to be parsed and a query factory to instanciate a
queryBuilder
(ThequeryBuilder
will be instanciated only if the expression can be parsed without any problems) - The query factory is a 2-level map, which maps field-alias & comparator combinations to
queryGenerator
(a closure function). WhenqueryGenerator
is called, it will return a sub-query for the certain field with the given value. - Call the
Build()
function to finally build the query
When an expression is given, it will:
- Scan the expression and extract all tokens from it.
- Use shunting-yard algorithm to convert these tokens to a suffix expression.
- Parse the expression and return the final query
- If the operator is a comparing operator and one of the operands is a field-alias, calls the
queryGenerator
to build a query. - If the operator is a logical operator and both of the operands are
elastic.Query
, useelastic.BoolQuery
to group the sub queries.
- If the operator is a comparing operator and one of the operands is a field-alias, calls the
- Lexer from govaluate
- Shunting-yard algorithm from go-shunting-yard