Documentation ¶
Overview ¶
Example (ExtractStrings) ¶
es := func(cond string) { root, err := parseCondition(cond) if err != nil { panic(err) } str, positive := extractStrings(root) fmt.Printf("----- %s -----\n", cond) fmt.Printf("strings: %+v\n", str) fmt.Printf("positive: %t\n", positive) } es(`"foo"`) es(`"foo" AND "bar"`) es(`"foo" OR "bar"`) es(`NOT "foo"`) es(`"foo" AND NOT "bar"`) es(`"foo" OR NOT "bar"`) es(`NOT ("foo" OR "bar")`) es(`NOT ("foo" AND "bar")`) es(`NOT ("foo" OR NOT "bar")`) es(`NOT ("foo" AND NOT "bar")`)
Output: ----- "foo" ----- strings: [foo] positive: true ----- "foo" AND "bar" ----- strings: [foo bar] positive: true ----- "foo" OR "bar" ----- strings: [foo bar] positive: true ----- NOT "foo" ----- strings: [foo] positive: false ----- "foo" AND NOT "bar" ----- strings: [foo bar] positive: true ----- "foo" OR NOT "bar" ----- strings: [foo bar] positive: false ----- NOT ("foo" OR "bar") ----- strings: [foo bar] positive: false ----- NOT ("foo" AND "bar") ----- strings: [foo bar] positive: false ----- NOT ("foo" OR NOT "bar") ----- strings: [foo bar] positive: true ----- NOT ("foo" AND NOT "bar") ----- strings: [foo bar] positive: false
Example (Parse) ¶
p := func(cond string) { t, err := tokenize(cond) if err != nil { panic(err) } root, err := parse(t) if err != nil { panic(err) } fmt.Printf("----- %s -----\n", cond) fmt.Println(root.String()) } p(`"foo"`) p(`"foo" AND "bar"`) p(`"foo" AND NOT "bar"`) p(`"foo" AND NOT ("bar" OR "baz")`) p(`("foo" OR "bar") AND ("bar" OR "baz") AND ("baaz" OR "qux")`)
Output: ----- "foo" ----- nodeVAL{"foo"} ----- "foo" AND "bar" ----- nodeAND{nodeVAL{"foo"},nodeVAL{"bar"}} ----- "foo" AND NOT "bar" ----- nodeAND{nodeVAL{"foo"},nodeNOT{nodeVAL{"bar"}}} ----- "foo" AND NOT ("bar" OR "baz") ----- nodeAND{nodeVAL{"foo"},nodeNOT{nodeOR{nodeVAL{"bar"},nodeVAL{"baz"}}}} ----- ("foo" OR "bar") AND ("bar" OR "baz") AND ("baaz" OR "qux") ----- nodeAND{nodeAND{nodeOR{nodeVAL{"foo"},nodeVAL{"bar"}},nodeOR{nodeVAL{"bar"},nodeVAL{"baz"}}},nodeOR{nodeVAL{"baaz"},nodeVAL{"qux"}}}
Example (Parse_multi) ¶
p := func(cond string) { t, err := tokenize(cond) if err != nil { panic(err) } root, err := parse(t) if err != nil { panic(err) } fmt.Printf("----- %s -----\n", cond) fmt.Println(root.Condition()) } p(`NOT "foo" OR NOT "bar" OR NOT "baz" OR NOT "qux"`) p(`"qux" AND "foo" OR "bar" AND "baz"`) p(`"qux" OR "foo" AND "bar" OR "baz"`)
Output: ----- NOT "foo" OR NOT "bar" OR NOT "baz" OR NOT "qux" ----- ((NOT "foo" OR NOT "bar") OR (NOT "baz" OR NOT "qux")) ----- "qux" AND "foo" OR "bar" AND "baz" ----- (("qux" AND "foo") OR ("bar" AND "baz")) ----- "qux" OR "foo" AND "bar" OR "baz" ----- (("qux" OR ("foo" AND "bar")) OR "baz")
Example (RandomCondition) ¶
rand.Seed(0) fmt.Println(randomCondition(3)) fmt.Println(randomCondition(3)) fmt.Println(randomCondition(3)) fmt.Println(randomCondition(3)) fmt.Println(randomCondition(3)) fmt.Println(randomCondition(3))
Output: "ERA9rI2cvTK4UHom"i NOT "QvymkzADm" ("HwxmE4tL20SrW"i AND (NOT "U86R7wIBbUt9RwI9U" OR "aWsz0l"i) AND "egogMR6spJPZHaPT0w4"i) "nRPswXn0i6jt3PARUDb0oU" NOT "aT3LnXs1oH7gq"i (NOT ("K4k"i OR NOT "UcTwCQmwFJPivbFQtOWS") OR NOT "8bmfKkaauLI"i)
Example (RandomString) ¶
rand.Seed(0) fmt.Println(randomString(10)) fmt.Println(randomString(10))
Output: mUNERA9rI2 cvTK4UHomc
Example (Tokenize) ¶
tk := func(cond string) { tokens, err := tokenize(cond) if err != nil { panic(err) } fmt.Printf("----- %s -----\n", cond) for _, token := range tokens { fmt.Println(token.String()) } } tk(`"foo"`) tk(`"foo" AND "bar"`) tk(`"foo" AND ("bar" OR "baz")`) tk(`"foo" AND ("bar" OR NOT "baz")`) tk(`"escaped quote: \""`)
Output: ----- "foo" ----- nodeVAL ( foo ) at pos 1 ----- "foo" AND "bar" ----- nodeVAL ( foo ) at pos 1 nodeAND ( AND ) at pos 7 nodeVAL ( bar ) at pos 11 ----- "foo" AND ("bar" OR "baz") ----- nodeVAL ( foo ) at pos 1 nodeAND ( AND ) at pos 7 LPAR ( ( ) at pos 11 nodeVAL ( bar ) at pos 12 nodeOR ( OR ) at pos 18 nodeVAL ( baz ) at pos 21 RPAR ( ) ) at pos 26 ----- "foo" AND ("bar" OR NOT "baz") ----- nodeVAL ( foo ) at pos 1 nodeAND ( AND ) at pos 7 LPAR ( ( ) at pos 11 nodeVAL ( bar ) at pos 12 nodeOR ( OR ) at pos 18 nodeNOT ( NOT ) at pos 21 nodeVAL ( baz ) at pos 25 RPAR ( ) ) at pos 30 ----- "escaped quote: \"" ----- nodeVAL ( escaped quote: " ) at pos 1
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Evalostic ¶
type Evalostic struct {
// contains filtered or unexported fields
}
Evalostic is a matcher that can apply multiple conditions on a string with some performance optimizations. The biggest optimization is that only conditions that contain at least one keyword of the string will be checked, these strings will be filtered with the Aho-Corasick algorithm. The only exception are negative conditions (see comment of: Negatives() function).
func New ¶
New builds a new Evalostic matcher that compiles all conditions to one big rule set that can be applied to strings.
func (*Evalostic) ExportElasticSearchQuery ¶
ExportElasticSearchQuery exports the compiled query into an ElasticSearch query, e.g. `"foo" OR "baz"` will be compiled to {"bool":{"should":[{"wildcard":{"raw":{"case_insensitive":false,"value":"*foo*"}}},{"wildcard":{"raw":{"case_insensitive":false,"value":"*bar*"}}}]}}
func (*Evalostic) ExportElasticSearchQueryMap ¶
func (e *Evalostic) ExportElasticSearchQueryMap(wildcardField string, useMatchPhrase bool) map[string]interface{}
ExportElasticSearchQuery exports the compiled query into an ElasticSearch query, e.g. `"foo" OR "baz"` will be compiled to {"bool":{"should":[{"wildcard":{"raw":{"case_insensitive":false,"value":"foo"}}},{"wildcard":{"raw":{"case_insensitive":false,"value":"bar"}}}]}}