2017-10-27 07:14:44 -04:00
|
|
|
[[query-dsl-terms-set-query]]
|
|
|
|
=== Terms Set Query
|
|
|
|
|
|
|
|
experimental[The terms_set query is a new query and its syntax may change in the future]
|
|
|
|
|
|
|
|
Returns any documents that match with at least one or more of the
|
|
|
|
provided terms. The terms are not analyzed and thus must match exactly.
|
|
|
|
The number of terms that must match varies per document and is either
|
|
|
|
controlled by a minimum should match field or computed per document in
|
|
|
|
a minimum should match script.
|
|
|
|
|
|
|
|
The field that controls the number of required terms that must match must
|
|
|
|
be a number field:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
PUT /my-index
|
|
|
|
{
|
|
|
|
"mappings": {
|
2017-12-14 11:47:53 -05:00
|
|
|
"_doc": {
|
2017-10-27 07:14:44 -04:00
|
|
|
"properties": {
|
|
|
|
"required_matches": {
|
|
|
|
"type": "long"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /my-index/_doc/1?refresh
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
|
|
|
"codes": ["ghi", "jkl"],
|
|
|
|
"required_matches": 2
|
|
|
|
}
|
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT /my-index/_doc/2?refresh
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
|
|
|
"codes": ["def", "ghi"],
|
|
|
|
"required_matches": 2
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
|
|
|
|
|
|
|
An example that uses the minimum should match field:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET /my-index/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"terms_set": {
|
|
|
|
"codes" : {
|
|
|
|
"terms" : ["abc", "def", "ghi"],
|
|
|
|
"minimum_should_match_field": "required_matches"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
|
|
|
Response:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"took": 13,
|
|
|
|
"timed_out": false,
|
|
|
|
"_shards": {
|
2018-05-14 12:22:35 -04:00
|
|
|
"total": 1,
|
|
|
|
"successful": 1,
|
2017-10-27 07:14:44 -04:00
|
|
|
"skipped" : 0,
|
|
|
|
"failed": 0
|
|
|
|
},
|
|
|
|
"hits": {
|
|
|
|
"total": 1,
|
2018-05-14 12:22:35 -04:00
|
|
|
"max_score": 0.87546873,
|
2017-10-27 07:14:44 -04:00
|
|
|
"hits": [
|
|
|
|
{
|
|
|
|
"_index": "my-index",
|
2017-12-14 11:47:53 -05:00
|
|
|
"_type": "_doc",
|
2017-10-27 07:14:44 -04:00
|
|
|
"_id": "2",
|
2018-05-14 12:22:35 -04:00
|
|
|
"_score": 0.87546873,
|
2017-10-27 07:14:44 -04:00
|
|
|
"_source": {
|
|
|
|
"codes": ["def", "ghi"],
|
|
|
|
"required_matches": 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// TESTRESPONSE[s/"took": 13,/"took": "$body.took",/]
|
|
|
|
|
|
|
|
Scripts can also be used to control how many terms are required to match
|
|
|
|
in a more dynamic way. For example a create date or a popularity field
|
|
|
|
can be used as basis for the number of required terms to match.
|
|
|
|
|
|
|
|
Also the `params.num_terms` parameter is available in the script to indicate the
|
|
|
|
number of terms that have been specified.
|
|
|
|
|
|
|
|
An example that always limits the number of required terms to match to never
|
|
|
|
become larger than the number of terms specified:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
GET /my-index/_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"terms_set": {
|
|
|
|
"codes" : {
|
|
|
|
"terms" : ["abc", "def", "ghi"],
|
|
|
|
"minimum_should_match_script": {
|
|
|
|
"source": "Math.min(params.num_terms, doc['required_matches'].value)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|