You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+81-1Lines changed: 81 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,9 +148,89 @@ Over time this value and argument might change as we get more experience, in the
148
148
149
149
Regular Expressions can also be set when using the Validation functions, the same rules apply as for aliases (see above). In general aliases are resolved prior to validation rules and operator checks.
150
150
151
+
### Working with ASTs
152
+
153
+
#### Reduce & Semantic Reduce
154
+
155
+
The library provides two approaches for processing AST trees: `ReduceAst()` and `SemanticReduceAst()`.
156
+
157
+
##### ReduceAst()
158
+
159
+
`ReduceAst()` is a low-level generic function that recursively processes an AST tree. It's useful when you need to process all nodes uniformly, regardless of their operator type. For example, extracting all field names, calculating tree depth, or transforming field names.
160
+
161
+
```go
162
+
// Example: Collect all field names from the AST
163
+
result, _:= epsearchast_v3.ReduceAst(ast, func(node *epsearchast_v3.AstNode, children []*[]string) (*[]string, error) {
164
+
fields:= []string{}
165
+
iflen(node.Args) > 0 {
166
+
fields = append(fields, node.Args[0])
167
+
}
168
+
for_, child:=range children {
169
+
if child != nil {
170
+
fields = append(fields, *child...)
171
+
}
172
+
}
173
+
return &fields, nil
174
+
})
175
+
```
176
+
177
+
##### SemanticReduceAst()
178
+
179
+
`SemanticReduceAst()` is a higher-level wrapper that uses the `SemanticReducer` interface to provide individual methods for each operator type (VisitEq, VisitLt, etc.). This is the recommended approach for generating queries, as each operator can be translated differently.
- Use `ReduceAst()` when you care about the tree structure but not the specific operators (e.g., collecting field names, calculating depth, transforming field names)
189
+
- Use `SemanticReduceAst()` when you need operator-specific behavior (e.g., generating database queries where EQ, LT, GE each translate differently)
190
+
151
191
#### Customizing ASTs
152
192
153
-
You can use the `IdentitySemanticReducer` type to simplify rewriting ASTs, by embedding this struct you can only override and process the specific parts you care about. Post processing the AST tree might be simplier than trying to post process a query written in your langauge, or while rebuilding a query.
193
+
You can use the `IdentitySemanticReducer` type to simplify rewriting ASTs, by embedding this struct you can only override and process the specific parts you care about. Post-processing the AST tree might be simplier than trying to post process a query written in your langauge, or while rebuilding a query.
194
+
195
+
#### Util Functions
196
+
197
+
The library provides several utility functions for working with ASTs:
Returns true if a specific field name appears anywhere in the AST. Useful for quickly checking if a field is referenced before performing expensive operations.
212
+
213
+
```go
214
+
hasStatus:= epsearchast_v3.HasFirstArg(ast, "status") // true if "status" appears as a field name anywhere in the query
215
+
```
216
+
217
+
##### GetAstDepth()
218
+
219
+
Returns the maximum depth of the AST tree. Useful for limiting query complexity.
220
+
221
+
```go
222
+
depth:= epsearchast_v3.GetAstDepth(ast)
223
+
```
224
+
225
+
##### GetEffectiveIndexIntersectionCount()
226
+
227
+
Returns a heuristic measure of query complexity based on potential index intersections. Used internally to cap OR query complexity (default limit is 4). See the "OR Filter Restrictions" section for more details.
0 commit comments