From a8833b097beecc9b7fc065b9755dce2d1b28bfad Mon Sep 17 00:00:00 2001
From: Mayya Sharipova <mayya.sharipova@elastic.co>
Date: Thu, 15 Nov 2018 15:10:49 -0500
Subject: [PATCH] Painless Context Doc: Add min should match example (#35423)

---
 ...painless-min-should-match-context.asciidoc | 50 ++++++++++++++++++-
 .../painless/ContextExampleTests.java         | 30 +++++++++++
 2 files changed, 79 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 b2ffb63fd7a..cd476481381 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,52 @@ results.
 
 *API*
 
-The standard <<painless-api-reference, Painless API>> is available.
\ No newline at end of file
+The standard <<painless-api-reference, Painless API>> is available.
+
+*Example*
+
+To run this example, first follow the steps in
+<<painless-context-examples, context examples>>.
+
+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'])
+----
+
+The following request finds seats to performances with at least
+two of the three specified 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 32572529243..adce6b6c864 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,4 +1,5 @@
 
+
 /*
  * Licensed to Elasticsearch under one or more contributor
  * license agreements. See the NOTICE file distributed with
@@ -396,5 +397,34 @@ public class ContextExampleTests extends ScriptTestCase {
             params, true);
         assertTrue(result);
     }
+
+
+    // 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<String, Object> 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);
+    }
 }