Skip to content

Commit

Permalink
Painless Context Doc: Add filter context example
Browse files Browse the repository at this point in the history
relates to elastic#34829
  • Loading branch information
mayya-sharipova committed Nov 6, 2018
1 parent a937d7f commit 2892b68
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
43 changes: 42 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,45 @@ 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 allows to find all documents where the value of `doc['sold']`
is `false`, and where the cost is less than 18.

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

To make a request more configurable, you can define `cost` as
a script parameter. Submit the following script query request
to filter all theatre seats for evening performances that are not
sold yet, and where the cost is less than `params.cost`:

[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
Expand Up @@ -19,6 +19,9 @@

package org.elasticsearch.painless;

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

/**
* 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 +311,43 @@ 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 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 testScriptFieldsScript() {
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);
}

}

0 comments on commit 2892b68

Please sign in to comment.