Skip to content

Commit d6f9365

Browse files
committed
[DSL] Add documentation to README about calling methods outside query blocks
1 parent a5db9ef commit d6f9365

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,56 @@ NOTE: You have to enable dynamic scripting to be able to execute the `function_s
239239

240240
**Please see the extensive RDoc examples in the source code and the integration tests.**
241241

242+
## Accessing methods outside DSL blocks' scopes
243+
244+
Methods can be defined and called from within a block. This can be done for values like a `Hash`,
245+
`String`, `Array`, etc. For example:
246+
247+
```ruby
248+
def match_criteria
249+
{ title: 'test' }
250+
end
251+
252+
s = search do
253+
query do
254+
match match_criteria
255+
end
256+
end
257+
258+
s.to_hash
259+
# => { query: { match: { title: 'test' } } }
260+
261+
```
262+
263+
To define subqueries in other methods, `self` must be passed to the method and the subquery must be defined in a block
264+
passed to `instance_eval` called on the object argument. Otherwise, the subquery does not have access to the scope of
265+
the block from which the method was called. For example:
266+
267+
```ruby
268+
def not_clause(obj)
269+
obj.instance_eval do
270+
_not do
271+
term color: 'red'
272+
end
273+
end
274+
end
275+
276+
s = search do
277+
query do
278+
filtered do
279+
filter do
280+
not_clause(self)
281+
end
282+
end
283+
end
284+
end
285+
286+
s.to_hash
287+
# => { query: { filtered: { filter: { not: { term: { color: 'red' } } } } } }
288+
289+
```
290+
291+
242292
## Development
243293

244294
To work on the code, clone the repository and install the dependencies:

0 commit comments

Comments
 (0)