Skip to content

Painless Context Doc: Add min should match example #35423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,53 @@ results.

*API*

The standard <<painless-api-reference, Painless API>> is available.
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 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "where your favorite actors play", maybe "by your favorite actors."?

The second sentence could be pared down to: "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."

`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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pare this down to: "To ensure that the minimum number of actors doesn't exceed the number of favorite actors, you can use num_terms 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:

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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]

Original file line number Diff line number Diff line change
Expand Up @@ -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<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);
}

}