Skip to content

Commit

Permalink
Painless Context Doc: Add field context example
Browse files Browse the repository at this point in the history
relates to #34829
  • Loading branch information
mayya-sharipova committed Nov 8, 2018
1 parent e008402 commit b6b5ff3
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
58 changes: 57 additions & 1 deletion docs/painless/painless-contexts/painless-field-context.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,60 @@ a customized value for each document in the results of a query.

*API*

The standard <<painless-api-reference, Painless API>> is available.
The standard <<painless-api-reference, Painless API>> is available.


*Example*

To run this example, first follow the steps in
<<painless-context-examples, context examples>>.

You can then use these two example scripts to compute custom information
for each search hit and output it to two new fields.

The first script gets the doc value for the `datetime` field and calls
the `getDayOfWeek` function to determine the corresponding day of the week.

[source,Painless]
----
doc['datetime'].value.getDayOfWeek();
----

The second script calculates the number of actors. Actors' names are stored
as a text array in the `actors` field.

[source,Painless]
----
params['_source']['actors'].length; <1>
----

<1> By default, doc values are not available for text fields. However,
you can still calculate the number of actors by extracting actors
from `_source`. Note that `params['_source']['actors']` is a list.


Submit the following request:

[source,js]
----
GET seats/_search
{
"query" : {
"match_all": {}
},
"script_fields" : {
"day-of-week" : {
"script" : {
"source": "doc['datetime'].value.getDayOfWeek()"
}
},
"number-of-actors" : {
"script" : {
"source": "params['_source']['actors'].length"
}
}
}
}
----
// CONSOLE
// TEST[skip: requires setup from other pages]
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

package org.elasticsearch.painless;

import java.util.HashMap;
import java.util.Map;

import static java.util.Collections.singletonMap;

/**
* These tests run the Painless scripts used in the context docs against
* slightly modified data designed around unit tests rather than a fully-
Expand Down Expand Up @@ -308,4 +313,51 @@ public void testIngestProcessorScript() {
curl -XPOST localhost:9200/seats/seat/_bulk?pipeline=seats -H "Content-Type: application/x-ndjson" --data-binary "@/home/jdconrad/test/seats.json"
*/


// Use script_fields API to add two extra fields to the hits

/*
curl -X GET localhost:9200/seats/_search
{
"query" : {
"match_all": {}
},
"script_fields" : {
"day-of-week" : {
"script" : {
"source": "doc['datetime'].value.getDayOfWeek()"
}
},
"number-of-actors" : {
"script" : {
"source": "params['_source']['actors'].length"
}
}
}
}
*/


// Testing only params, as I am not sure how to test Script Doc Values in painless
public void testScriptFieldsScript() {
Map<String, Object> hit = new HashMap<>();
Map<String, Object> fields = new HashMap<>();
fields.put("number-of-actors", 4);
hit.put("fields", fields);

Map<String, Object> source = new HashMap<>();
String[] actors = {"James Holland", "Krissy Smith", "Joe Muir", "Ryan Earns"};
source.put("actors", actors);

assertEquals(hit, exec(
"Map fields = new HashMap();" +
"fields[\"number-of-actors\"] = params['_source']['actors'].length;" +
"Map rtn = new HashMap();" +
"rtn[\"fields\"] = fields;" +
"return rtn;",
singletonMap("_source", source), true)
);
}
}

0 comments on commit b6b5ff3

Please sign in to comment.