Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion docs/painless/painless-contexts/painless-filter-context.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,43 @@ query to include and exclude documents.

*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>>.

This script finds all unsold documents that cost less than $18.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "allows to find", I'd just say "finds". Given the straightforwardness of the example, I'd just summarize the intent of the script, instead of repeating what the code says:

This script finds all unsold documents that cost less than $18.

[source,Painless]
----
doc['sold'].value == false && doc['cost'].value < 18
----

Defining cost as a script parameter enables the cost to be configured
in the script query request. For example, the following request finds
all available theatre seats for evening performances that are under $18.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd tweak this slightly to put the emphasis on parameterizing cost:

Defining cost as a script parameter enables the cost to be configured in the script query request. For example,
the following request finds all available theatre seats for evening performances that are under $18.

[source,js]
----
GET evening/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
"params" : {
"cost" : 18
}
}
}
}
}
}
}
----
// CONSOLE
// TEST[skip: requires setup from other pages]
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
Expand Down Expand Up @@ -359,5 +360,41 @@ public void testScriptFieldsScript() {
singletonMap("_source", source), true)
);
}

// Use script query request to filter documents
/*
GET localhost:9200/evening/_search
{
"query": {
"bool" : {
"filter" : {
"script" : {
"script" : {
"source" : "doc['sold'].value == false && doc['cost'].value < params.cost",
"params" : {
"cost" : 18
}
}
}
}
}
}
}
*/

public void testFilterScript() {
Map<String, Object> source = new HashMap<>();
source.put("sold", false);
source.put("cost", 15);

Map<String, Object> params = new HashMap<>();
params.put("_source", source);
params.put("cost", 18);

boolean result = (boolean) exec(
" params['_source']['sold'] == false && params['_source']['cost'] < params.cost;",
params, true);
assertTrue(result);
}
}