From 2892b68342db7f2bb1a5fa016b88ce372b7577cd Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Tue, 6 Nov 2018 10:17:44 -0500 Subject: [PATCH 1/2] Painless Context Doc: Add filter context example relates to #34829 --- .../painless-filter-context.asciidoc | 43 ++++++++++++++++++- .../painless/ContextExampleTests.java | 42 ++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/docs/painless/painless-contexts/painless-filter-context.asciidoc b/docs/painless/painless-contexts/painless-filter-context.asciidoc index 96fddf13b500d..36ac21e14e3c5 100644 --- a/docs/painless/painless-contexts/painless-filter-context.asciidoc +++ b/docs/painless/painless-contexts/painless-filter-context.asciidoc @@ -23,4 +23,45 @@ 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 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] \ 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 15eed75bcb8df..757bbc1e0c212 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 @@ -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- @@ -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 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); + } + } From dc2b12d19b6ae977b32e3b66569645b5b43a0f4e Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Tue, 13 Nov 2018 14:35:09 -0500 Subject: [PATCH 2/2] Address Deb's comments --- .../painless-contexts/painless-filter-context.asciidoc | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/docs/painless/painless-contexts/painless-filter-context.asciidoc b/docs/painless/painless-contexts/painless-filter-context.asciidoc index 36ac21e14e3c5..bf4741cfc02fc 100644 --- a/docs/painless/painless-contexts/painless-filter-context.asciidoc +++ b/docs/painless/painless-contexts/painless-filter-context.asciidoc @@ -30,18 +30,16 @@ The standard <> is available. To run this example, first follow the steps in <>. -This script allows to find all documents where the value of `doc['sold']` -is `false`, and where the cost is less than 18. +This script finds all unsold documents that cost 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`: +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] ----