@@ -25,7 +25,7 @@ The only variable that is available is `params`, which can be used to access use
2525The result of the script is always converted to a string.
2626If no context is specified then this context is used by default.
2727
28- ==== Example
28+ ====== Example
2929
3030Request:
3131
@@ -52,4 +52,92 @@ Response:
5252 "result": "0.1"
5353}
5454--------------------------------------------------
55- // TESTRESPONSE
55+ // TESTRESPONSE
56+
57+ ===== Filter script context
58+
59+ The `filter` context executes scripts as if they were executed inside a `script` query.
60+ For testing purposes a document must be provided that will be indexed temporarily in-memory and
61+ is accessible to the script being tested. Because of this the _source, stored fields and doc values
62+ are available in the script being tested.
63+
64+ The following parameters are required inside a filter context:
65+
66+ document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
67+ index:: The name of an index containing a mapping that is compatable with the document being indexed.
68+
69+ ====== Example
70+
71+ [source,js]
72+ ----------------------------------------------------------------
73+ POST /_scripts/painless/_execute
74+ {
75+ "script": {
76+ "source": "doc['field'].value.length() <= params.max_length",
77+ "params": {
78+ "max_length": 4
79+ }
80+ "context": {
81+ "index": "my-index",
82+ "document": {
83+ "field": "four"
84+ }
85+ }
86+ }
87+ }
88+ ----------------------------------------------------------------
89+ // CONSOLE
90+
91+ Response:
92+
93+ [source,js]
94+ --------------------------------------------------
95+ {
96+ "result": true
97+ }
98+ --------------------------------------------------
99+ // TESTRESPONSE
100+
101+
102+ ===== Score script context
103+
104+ The `score` context executes scripts as if they were executed inside a `script_score` function in
105+ `function_score` query.
106+
107+ Available parameters inside a score context:
108+
109+ document:: Contains the document that will be temporarily indexed in-memory and is accessible from the script.
110+ index:: The name of an index containing a mapping that is compatable with the document being indexed.
111+ query:: If `_score` is used in the script then a query can specified that will be used the compute a score.
112+
113+ ====== Example
114+
115+ [source,js]
116+ ----------------------------------------------------------------
117+ POST /_scripts/painless/_execute
118+ {
119+ "script": {
120+ "source": "doc['rank'].value / params.max_rank",
121+ "params": {
122+ "max_rank": 4
123+ }
124+ "context": {
125+ "index": "my-index",
126+ "document": {
127+ "rank": 4
128+ }
129+ }
130+ }
131+ }
132+ ----------------------------------------------------------------
133+ // CONSOLE
134+
135+ Response:
136+
137+ [source,js]
138+ --------------------------------------------------
139+ {
140+ "result": 0.8
141+ }
142+ --------------------------------------------------
143+ // TESTRESPONSE
0 commit comments