diff --git a/docs/painless/painless-contexts/painless-filter-context.asciidoc b/docs/painless/painless-contexts/painless-filter-context.asciidoc index 96fddf13b50..bf4741cfc02 100644 --- a/docs/painless/painless-contexts/painless-filter-context.asciidoc +++ b/docs/painless/painless-contexts/painless-filter-context.asciidoc @@ -23,4 +23,43 @@ query to include and exclude documents. *API* -The standard <> is available. \ No newline at end of file +The standard <> is available. + +*Example* + +To run this example, first follow the steps in +<>. + +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. + +[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] \ No newline at end of file diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java index f14b270151c..32572529243 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java @@ -1,3 +1,4 @@ + /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with @@ -359,5 +360,41 @@ public class ContextExampleTests extends ScriptTestCase { 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 source = new HashMap<>(); + source.put("sold", false); + source.put("cost", 15); + + Map 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); + } }