File tree 2 files changed +77
-1
lines changed
docs/painless/painless-contexts
modules/lang-painless/src/test/java/org/elasticsearch/painless 2 files changed +77
-1
lines changed Original file line number Diff line number Diff line change @@ -23,4 +23,43 @@ query to include and exclude documents.
23
23
24
24
*API*
25
25
26
- The standard <<painless-api-reference, Painless API>> is available.
26
+ The standard <<painless-api-reference, Painless API>> is available.
27
+
28
+ *Example*
29
+
30
+ To run this example, first follow the steps in
31
+ <<painless-context-examples, context examples>>.
32
+
33
+ This script finds all unsold documents that cost less than $18.
34
+
35
+ [source,Painless]
36
+ ----
37
+ doc['sold'].value == false && doc['cost'].value < 18
38
+ ----
39
+
40
+ Defining cost as a script parameter enables the cost to be configured
41
+ in the script query request. For example, the following request finds
42
+ all available theatre seats for evening performances that are under $18.
43
+
44
+ [source,js]
45
+ ----
46
+ GET evening/_search
47
+ {
48
+ "query": {
49
+ "bool" : {
50
+ "filter" : {
51
+ "script" : {
52
+ "script" : {
53
+ "source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
54
+ "params" : {
55
+ "cost" : 18
56
+ }
57
+ }
58
+ }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ ----
64
+ // CONSOLE
65
+ // TEST[skip: requires setup from other pages]
Original file line number Diff line number Diff line change
1
+
1
2
/*
2
3
* Licensed to Elasticsearch under one or more contributor
3
4
* license agreements. See the NOTICE file distributed with
@@ -359,5 +360,41 @@ public void testScriptFieldsScript() {
359
360
singletonMap ("_source" , source ), true )
360
361
);
361
362
}
363
+
364
+ // Use script query request to filter documents
365
+ /*
366
+ GET localhost:9200/evening/_search
367
+ {
368
+ "query": {
369
+ "bool" : {
370
+ "filter" : {
371
+ "script" : {
372
+ "script" : {
373
+ "source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
374
+ "params" : {
375
+ "cost" : 18
376
+ }
377
+ }
378
+ }
379
+ }
380
+ }
381
+ }
382
+ }
383
+ */
384
+
385
+ public void testFilterScript () {
386
+ Map <String , Object > source = new HashMap <>();
387
+ source .put ("sold" , false );
388
+ source .put ("cost" , 15 );
389
+
390
+ Map <String , Object > params = new HashMap <>();
391
+ params .put ("_source" , source );
392
+ params .put ("cost" , 18 );
393
+
394
+ boolean result = (boolean ) exec (
395
+ " params['_source']['sold'] == false && params['_source']['cost'] < params.cost;" ,
396
+ params , true );
397
+ assertTrue (result );
398
+ }
362
399
}
363
400
You can’t perform that action at this time.
0 commit comments