9
9
// `"foo" OR "baz"` will be compiled to
10
10
// {"bool":{"should":[{"wildcard":{"raw":{"case_insensitive":false,"value":"*foo*"}}},{"wildcard":{"raw":{"case_insensitive":false,"value":"*bar*"}}}]}}
11
11
func (e * Evalostic ) ExportElasticSearchQuery (wildcardField string ) string {
12
- b , _ := json .Marshal (e .ExportElasticSearchQueryMap (wildcardField ))
12
+ b , _ := json .MarshalIndent (e .ExportElasticSearchQueryMap (wildcardField ), "" , " " )
13
13
return string (b )
14
14
}
15
15
@@ -21,7 +21,7 @@ func (e *Evalostic) ExportElasticSearchQueryMap(wildcardField string) map[string
21
21
for k , v := range e .strings {
22
22
indexToStrings [v ] = k
23
23
}
24
- query := e .exportElasticSearchQuerySub (wildcardField , indexToStrings , decisionTreeEntry {value : - 1 }, e .decisionTree )
24
+ query := e .exportElasticSearchQuerySub (wildcardField , indexToStrings , decisionTreeEntry {value : - 1 }, e .decisionTree , false )
25
25
if query == nil {
26
26
return make (map [string ]interface {})
27
27
}
@@ -30,8 +30,7 @@ func (e *Evalostic) ExportElasticSearchQueryMap(wildcardField string) map[string
30
30
31
31
var wildcardReplacer = strings .NewReplacer ("\\ " , "\\ \\ " , "*" , "\\ *" , "?" , "\\ ?" )
32
32
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 {} {
35
34
isLeaf := len (node .outputs ) != 0
36
35
wildcard := map [string ]interface {}{
37
36
"wildcard" : map [string ]interface {}{
@@ -41,6 +40,13 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
41
40
},
42
41
},
43
42
}
43
+ if not {
44
+ wildcard = map [string ]interface {}{
45
+ "bool" : map [string ]interface {}{
46
+ "must_not" : []interface {}{wildcard },
47
+ },
48
+ }
49
+ }
44
50
if entry .value == - 1 {
45
51
// special case: do not use root node as wildcard
46
52
wildcard = nil
@@ -50,20 +56,20 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
50
56
return wildcard
51
57
}
52
58
53
- var should , shouldNot []map [string ]interface {}
59
+ var should []map [string ]interface {}
54
60
55
61
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 {
57
63
should = append (should , subQuery )
58
64
}
59
65
}
60
66
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 )
63
69
}
64
70
}
65
71
66
- toQuery := func (should []map [string ]interface {}, not bool ) map [string ]interface {} {
72
+ toQuery := func (should []map [string ]interface {}) map [string ]interface {} {
67
73
if len (should ) == 0 {
68
74
return nil
69
75
}
@@ -77,22 +83,10 @@ func (e *Evalostic) exportElasticSearchQuerySub(wildcardField string, indexToStr
77
83
},
78
84
}
79
85
}
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
- }
88
86
return res
89
87
}
90
88
91
- notChildQuery := toQuery (shouldNot , true )
92
- if notChildQuery != nil {
93
- should = append (should , notChildQuery )
94
- }
95
- childQuery := toQuery (should , false )
89
+ childQuery := toQuery (should )
96
90
if childQuery == nil {
97
91
return nil
98
92
}
0 commit comments