2018-07-02 17:44:36 -04:00
|
|
|
[[painless-min-should-match-context]]
|
|
|
|
=== Minimum should match context
|
|
|
|
|
|
|
|
Use a Painless script to specify the
|
2018-07-25 13:59:25 -04:00
|
|
|
{ref}/query-dsl-terms-set-query.html[minimum] number of terms that a
|
2018-07-02 17:44:36 -04:00
|
|
|
specified field needs to match with for a document to be part of the query
|
|
|
|
results.
|
|
|
|
|
|
|
|
*Variables*
|
|
|
|
|
|
|
|
`params` (`Map`, read-only)::
|
|
|
|
User-defined parameters passed in as part of the query.
|
|
|
|
|
|
|
|
`params['num_terms']` (`int`, read-only)::
|
|
|
|
The number of terms specified to match with.
|
|
|
|
|
|
|
|
`doc` (`Map`, read-only)::
|
|
|
|
Contains the fields of the current document where each field is a
|
|
|
|
`List` of values.
|
|
|
|
|
|
|
|
*Return*
|
|
|
|
|
|
|
|
`int`::
|
|
|
|
The minimum number of terms required to match the current document.
|
|
|
|
|
|
|
|
*API*
|
|
|
|
|
2018-11-15 15:10:49 -05:00
|
|
|
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.
|
|
|
|
|
2019-09-09 13:38:14 -04:00
|
|
|
[source,console]
|
2018-11-15 15:10:49 -05:00
|
|
|
----
|
|
|
|
GET seats/_search
|
|
|
|
{
|
2020-07-17 11:31:37 -04:00
|
|
|
"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
|
|
|
|
}
|
2018-11-15 15:10:49 -05:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2018-11-15 15:10:49 -05:00
|
|
|
}
|
2020-07-17 11:31:37 -04:00
|
|
|
}
|
2018-11-15 15:10:49 -05:00
|
|
|
}
|
|
|
|
----
|
|
|
|
// TEST[skip: requires setup from other pages]
|
|
|
|
|