From 01e8616fb2394a739c0cec3745bb6ec638760b47 Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Fri, 9 Nov 2018 16:09:54 -0500 Subject: [PATCH 1/2] Painless Context Doc: Add min should match example relates to #34829 --- ...painless-min-should-match-context.asciidoc | 51 ++++++++++++++++++- .../painless/ContextExampleTests.java | 30 +++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc b/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc index b2ffb63fd7aea..b9c9d7c7b1eaf 100644 --- a/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc +++ b/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc @@ -25,4 +25,53 @@ results. *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 +<>. + +Imagine that you want to find seats to performances where your favorite +actors play. You have a list of favorite actors in mind, and you want +to find performances that have at least certain minimum number of actors +from your favorite list. `terms_set` query with +`minimum_should_match_script` is a way to accomplish this. To make +the query request more configurable, you can define +`min_actors_to_see` as a script parameter. + +To ensure that `min_actors_to_see` parameter doesn't exceed the number +your favorite actors, use the script below. This script +selects the minimum value between the parameter `min_actors_to_see` and +the parameter `num_terms`, which represents the length of the list of +your favorite actors. + +[source,Painless] +---- +Math.min(params['num_terms'], params['min_actors_to_see']) +---- + +Submit the following request to find seats to performances with your favorite actors: + +[source,js] +---- +GET seats/_search +{ + "query" : { + "terms_set": { + "actors" : { + "terms" : ["smith", "earns", "black"], + "minimum_should_match_script": { + "source": "Math.min(params['num_terms'], params['min_actors_to_see'])", + "params" : { + "min_actors_to_see" : 2 + } + } + } + } + } +} +---- +// CONSOLE +// TEST[skip: requires setup from other pages] + 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 f14b270151c67..1d54d875651fd 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 @@ -359,5 +359,35 @@ public void testScriptFieldsScript() { singletonMap("_source", source), true) ); } + + + // Use script_fields API to add two extra fields to the hits + /* + curl -X GET localhost:9200/seats/_search + { + "query" : { + "terms_set": { + "actors" : { + "terms" : ["smith", "earns", "black"], + "minimum_should_match_script": { + "source": "Math.min(params['num_terms'], params['min_actors_to_see'])", + "params" : { + "min_actors_to_see" : 2 + } + } + } + } + } + } + */ + public void testMinShouldMatchScript() { + Map params = new HashMap<>(); + params.put("num_terms", 3); + params.put("min_actors_to_see", 2); + + double result = (double) exec("Math.min(params['num_terms'], params['min_actors_to_see']);", params, true); + assertEquals(2, result, 0); + } + } From 77b69ef3860a2372e7c8408a5aa1d9410d30e623 Mon Sep 17 00:00:00 2001 From: Mayya Sharipova Date: Tue, 13 Nov 2018 15:14:45 -0500 Subject: [PATCH 2/2] Address Deb's comments --- ...painless-min-should-match-context.asciidoc | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc b/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc index b9c9d7c7b1eaf..cd476481381a6 100644 --- a/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc +++ b/docs/painless/painless-contexts/painless-min-should-match-context.asciidoc @@ -32,26 +32,25 @@ The standard <> is available. To run this example, first follow the steps in <>. -Imagine that you want to find seats to performances where your favorite -actors play. You have a list of favorite actors in mind, and you want -to find performances that have at least certain minimum number of actors -from your favorite list. `terms_set` query with -`minimum_should_match_script` is a way to accomplish this. To make -the query request more configurable, you can define -`min_actors_to_see` as a script parameter. - -To ensure that `min_actors_to_see` parameter doesn't exceed the number -your favorite actors, use the script below. This script -selects the minimum value between the parameter `min_actors_to_see` and -the parameter `num_terms`, which represents the length of the list of -your favorite actors. +Imagine that you want to find seats to performances by your favorite +actors. You have a list of favorite actors in mind, and you want +to find performances where the cast includes at least a certain +number of them. `terms_set` query with `minimum_should_match_script` +is a way to accomplish this. To make the query request more configurable, +you can define `min_actors_to_see` as a script parameter. + +To ensure that the parameter `min_actors_to_see` doesn't exceed +the number of favorite actors, you can use `num_term`s to get +the number of actors in the list and `Math.min` to get the lesser +of the two. [source,Painless] ---- Math.min(params['num_terms'], params['min_actors_to_see']) ---- -Submit the following request to find seats to performances with your favorite actors: +The following request finds seats to performances with at least +two of the three specified actors. [source,js] ----