Skip to content

Commit 490e437

Browse files
Painless Context Doc: Add field context example
relates to elastic#34829
1 parent bdd8460 commit 490e437

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

docs/painless/painless-contexts/painless-field-context.asciidoc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,64 @@ a customized value for each document in the results of a query.
2626
*API*
2727

2828
The standard <<painless-api-reference, Painless API>> is available.
29+
30+
31+
*Example*
32+
33+
To run this example, first follow the steps in
34+
<<painless-context-examples, context examples>>.
35+
36+
We will use two scripts to create two additional fields for each search hit.
37+
These script fields will output computed custom information.
38+
39+
The first script calculates the day of the week for the field `datetime`:
40+
41+
[source,Painless]
42+
----
43+
doc['datetime'].value.getDayOfWeek(); <1>
44+
----
45+
46+
<1> Accesses a doc value for the `datetime` field, and uses the function
47+
`getDayOfWeek` to calculate the day of the week for this date.
48+
49+
50+
51+
The second script calculates the number of actors. Actors' names are stored
52+
as an array in the `actors` field:
53+
54+
[source,Painless]
55+
----
56+
params['_source']['actors'].length;
57+
----
58+
59+
<1> As `actors` field is of type `text`, by default, doc values are
60+
not available for this field. We parse `_source` to extract
61+
`actors` and calculate their number. Note how `params['_source']['actors']`
62+
is a list.
63+
64+
65+
Submit the following request:
66+
67+
[source,js]
68+
----
69+
GET seats/_search
70+
{
71+
"query" : {
72+
"match_all": {}
73+
},
74+
"script_fields" : {
75+
"day-of-week" : {
76+
"script" : {
77+
"source": "doc['datetime'].value.getDayOfWeek()"
78+
}
79+
},
80+
"number-of-actors" : {
81+
"script" : {
82+
"source": "params['_source']['actors'].length"
83+
}
84+
}
85+
}
86+
}
87+
----
88+
// CONSOLE
89+
// TEST[skip: requires setup from other pages]

modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
package org.elasticsearch.painless;
2121

22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import static java.util.Collections.singletonMap;
26+
2227
/**
2328
* These tests run the Painless scripts used in the context docs against
2429
* slightly modified data designed around unit tests rather than a fully-
@@ -308,4 +313,51 @@ public void testIngestProcessorScript() {
308313
curl -XPOST localhost:9200/seats/seat/_bulk?pipeline=seats -H "Content-Type: application/x-ndjson" --data-binary "@/home/jdconrad/test/seats.json"
309314
310315
*/
316+
317+
318+
// Use script_fields API to add two extra fields to the hits
319+
320+
/*
321+
curl -X GET localhost:9200/seats/_search
322+
{
323+
"query" : {
324+
"match_all": {}
325+
},
326+
"script_fields" : {
327+
"day-of-week" : {
328+
"script" : {
329+
"source": "doc['datetime'].value.getDayOfWeek()"
330+
}
331+
},
332+
"number-of-actors" : {
333+
"script" : {
334+
"source": "params['_source']['actors'].length"
335+
}
336+
}
337+
}
338+
}
339+
*/
340+
341+
342+
// Testing only params, as I am not sure how to test Script Doc Values in painless
343+
public void testScriptFieldsScript() {
344+
Map<String, Object> hit = new HashMap<>();
345+
Map<String, Object> fields = new HashMap<>();
346+
fields.put("number-of-actors", 4);
347+
hit.put("fields", fields);
348+
349+
Map<String, Object> source = new HashMap<>();
350+
String[] actors = {"James Holland", "Krissy Smith", "Joe Muir", "Ryan Earns"};
351+
source.put("actors", actors);
352+
353+
assertEquals(hit, exec(
354+
"Map fields = new HashMap();" +
355+
"fields[\"number-of-actors\"] = params['_source']['actors'].length;" +
356+
"Map rtn = new HashMap();" +
357+
"rtn[\"fields\"] = fields;" +
358+
"return rtn;",
359+
singletonMap("_source", source), true)
360+
);
361+
}
311362
}
363+

0 commit comments

Comments
 (0)