Skip to content

Commit 8ee3a06

Browse files
author
Codehardt
committed
fix: fixed query to elasticsearch with not filters
1 parent 7e112d5 commit 8ee3a06

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

decisiontree.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package evalostic
22

3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
38
type decisionTreeEntry struct {
49
value int
510
ci bool
@@ -11,6 +16,29 @@ type decisionTreeNode struct {
1116
outputs []int
1217
}
1318

19+
func (n *decisionTreeNode) String() string {
20+
return n.string(decisionTreeEntry{value: -1}, 0)
21+
}
22+
23+
func (n *decisionTreeNode) string(entry decisionTreeEntry, depth int) string {
24+
indent := strings.Repeat(" ", depth)
25+
var c string
26+
var nc string
27+
for entry, child := range n.children {
28+
s := child.string(entry, depth+1)
29+
c += s
30+
}
31+
for entry, child := range n.notChildren {
32+
s := child.string(entry, depth+1)
33+
nc += s
34+
}
35+
res := fmt.Sprintf("%s-entry: %d\n%s children:\n%s%s not children:\n%s", indent, entry.value, indent, c, indent, nc)
36+
if len(n.outputs) > 0 {
37+
res += fmt.Sprintf("%s match!\n", indent)
38+
}
39+
return res
40+
}
41+
1442
func (n *decisionTreeNode) add(path andPathIndex, output int) {
1543
if len(path) == 0 {
1644
n.outputs = append(n.outputs, output)

export.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
// `"foo" OR "baz"` will be compiled to
1010
// {"bool":{"should":[{"wildcard":{"raw":{"case_insensitive":false,"value":"*foo*"}}},{"wildcard":{"raw":{"case_insensitive":false,"value":"*bar*"}}}]}}
1111
func (e *Evalostic) ExportElasticSearchQuery(wildcardField string) string {
12-
b, _ := json.Marshal(e.ExportElasticSearchQueryMap(wildcardField))
12+
b, _ := json.MarshalIndent(e.ExportElasticSearchQueryMap(wildcardField), "", " ")
1313
return string(b)
1414
}
1515

@@ -21,7 +21,7 @@ func (e *Evalostic) ExportElasticSearchQueryMap(wildcardField string) map[string
2121
for k, v := range e.strings {
2222
indexToStrings[v] = k
2323
}
24-
query := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, decisionTreeEntry{value: -1}, e.decisionTree)
24+
query := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, decisionTreeEntry{value: -1}, e.decisionTree, false)
2525
if query == nil {
2626
return make(map[string]interface{})
2727
}
@@ -30,8 +30,7 @@ func (e *Evalostic) ExportElasticSearchQueryMap(wildcardField string) map[string
3030

3131
var wildcardReplacer = strings.NewReplacer("\\", "\\\\", "*", "\\*", "?", "\\?")
3232

33-
func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStrings map[int]string, entry decisionTreeEntry, node *decisionTreeNode) map[string]interface{} {
34-
33+
func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStrings map[int]string, entry decisionTreeEntry, node *decisionTreeNode, not bool) map[string]interface{} {
3534
isLeaf := len(node.outputs) != 0
3635
wildcard := map[string]interface{}{
3736
"wildcard": map[string]interface{}{
@@ -41,6 +40,13 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
4140
},
4241
},
4342
}
43+
if not {
44+
wildcard = map[string]interface{}{
45+
"bool": map[string]interface{}{
46+
"must_not": []interface{}{wildcard},
47+
},
48+
}
49+
}
4450
if entry.value == -1 {
4551
// special case: do not use root node as wildcard
4652
wildcard = nil
@@ -50,20 +56,20 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
5056
return wildcard
5157
}
5258

53-
var should, shouldNot []map[string]interface{}
59+
var should []map[string]interface{}
5460

5561
for subEntry, subNode := range node.children {
56-
if subQuery := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, subEntry, subNode); subQuery != nil {
62+
if subQuery := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, subEntry, subNode, false); subQuery != nil {
5763
should = append(should, subQuery)
5864
}
5965
}
6066
for subEntry, subNode := range node.notChildren {
61-
if subQuery := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, subEntry, subNode); subQuery != nil {
62-
shouldNot = append(shouldNot, subQuery)
67+
if subQuery := e.exportElasticSearchQuerySub(wildcardField, indexToStrings, subEntry, subNode, true); subQuery != nil {
68+
should = append(should, subQuery)
6369
}
6470
}
6571

66-
toQuery := func(should []map[string]interface{}, not bool) map[string]interface{} {
72+
toQuery := func(should []map[string]interface{}) map[string]interface{} {
6773
if len(should) == 0 {
6874
return nil
6975
}
@@ -77,22 +83,10 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
7783
},
7884
}
7985
}
80-
if not {
81-
// wrap OR conditions with a NOT
82-
res = map[string]interface{}{
83-
"bool": map[string]interface{}{
84-
"must_not": []interface{}{res},
85-
},
86-
}
87-
}
8886
return res
8987
}
9088

91-
notChildQuery := toQuery(shouldNot, true)
92-
if notChildQuery != nil {
93-
should = append(should, notChildQuery)
94-
}
95-
childQuery := toQuery(should, false)
89+
childQuery := toQuery(should)
9690
if childQuery == nil {
9791
return nil
9892
}

export_test.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package evalostic
22

3-
import "testing"
3+
import (
4+
"fmt"
5+
"testing"
6+
)
47

58
func TestElasticSearchQueryExport(t *testing.T) {
69
for _, condition := range []string{
@@ -20,3 +23,18 @@ func TestElasticSearchQueryExport(t *testing.T) {
2023
t.Logf("%s: %s", condition, query)
2124
}
2225
}
26+
27+
func TestElasticSearchQueryExport2(t *testing.T) {
28+
for _, condition := range []string{
29+
`"A" AND NOT ("B" OR "C" OR "D")`,
30+
} {
31+
fmt.Println(condition)
32+
fmt.Println("-----")
33+
ev, err := New([]string{condition})
34+
if err != nil {
35+
t.Fatalf("could not parse %s: %s", condition, err)
36+
}
37+
query := ev.ExportElasticSearchQuery("raw")
38+
t.Logf("%s: %s", condition, query)
39+
}
40+
}

0 commit comments

Comments
 (0)