Skip to content

Commit ee9a271

Browse files
Painless Context Doc: Add min should match example (#35423)
1 parent 2c3597f commit ee9a271

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

docs/painless/painless-contexts/painless-min-should-match-context.asciidoc

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,52 @@ results.
2525

2626
*API*
2727

28-
The standard <<painless-api-reference, Painless API>> is available.
28+
The standard <<painless-api-reference, Painless API>> is available.
29+
30+
*Example*
31+
32+
To run this example, first follow the steps in
33+
<<painless-context-examples, context examples>>.
34+
35+
Imagine that you want to find seats to performances by your favorite
36+
actors. You have a list of favorite actors in mind, and you want
37+
to find performances where the cast includes at least a certain
38+
number of them. `terms_set` query with `minimum_should_match_script`
39+
is a way to accomplish this. To make the query request more configurable,
40+
you can define `min_actors_to_see` as a script parameter.
41+
42+
To ensure that the parameter `min_actors_to_see` doesn't exceed
43+
the number of favorite actors, you can use `num_term`s to get
44+
the number of actors in the list and `Math.min` to get the lesser
45+
of the two.
46+
47+
[source,Painless]
48+
----
49+
Math.min(params['num_terms'], params['min_actors_to_see'])
50+
----
51+
52+
The following request finds seats to performances with at least
53+
two of the three specified actors.
54+
55+
[source,js]
56+
----
57+
GET seats/_search
58+
{
59+
"query" : {
60+
"terms_set": {
61+
"actors" : {
62+
"terms" : ["smith", "earns", "black"],
63+
"minimum_should_match_script": {
64+
"source": "Math.min(params['num_terms'], params['min_actors_to_see'])",
65+
"params" : {
66+
"min_actors_to_see" : 2
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
----
74+
// CONSOLE
75+
// TEST[skip: requires setup from other pages]
76+

modules/lang-painless/src/test/java/org/elasticsearch/painless/ContextExampleTests.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
23
/*
34
* Licensed to Elasticsearch under one or more contributor
45
* license agreements. See the NOTICE file distributed with
@@ -396,5 +397,34 @@ public void testFilterScript() {
396397
params, true);
397398
assertTrue(result);
398399
}
400+
401+
402+
// Use script_fields API to add two extra fields to the hits
403+
/*
404+
curl -X GET localhost:9200/seats/_search
405+
{
406+
"query" : {
407+
"terms_set": {
408+
"actors" : {
409+
"terms" : ["smith", "earns", "black"],
410+
"minimum_should_match_script": {
411+
"source": "Math.min(params['num_terms'], params['min_actors_to_see'])",
412+
"params" : {
413+
"min_actors_to_see" : 2
414+
}
415+
}
416+
}
417+
}
418+
}
419+
}
420+
*/
421+
public void testMinShouldMatchScript() {
422+
Map<String, Object> params = new HashMap<>();
423+
params.put("num_terms", 3);
424+
params.put("min_actors_to_see", 2);
425+
426+
double result = (double) exec("Math.min(params['num_terms'], params['min_actors_to_see']);", params, true);
427+
assertEquals(2, result, 0);
428+
}
399429
}
400430

0 commit comments

Comments
 (0)