| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 | [[query-dsl-terms-set-query]]=== Terms Set Queryexperimental[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 theprovided terms. The terms are not analyzed and thus must match exactly.The number of terms that must match varies per document and is eithercontrolled by a minimum should match field or computed per document ina minimum should match script.The field that controls the number of required terms that must match mustbe a number field:[source,js]--------------------------------------------------PUT /my-index{    "mappings": {        "_doc": {            "properties": {                "required_matches": {                    "type": "long"                }            }        }    }}PUT /my-index/_doc/1?refresh{    "codes": ["ghi", "jkl"],    "required_matches": 2}PUT /my-index/_doc/2?refresh{    "codes": ["def", "ghi"],    "required_matches": 2}--------------------------------------------------// CONSOLE// TESTSETUPAn 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"            }        }    }}--------------------------------------------------// CONSOLEResponse:[source,js]--------------------------------------------------{  "took": 13,  "timed_out": false,  "_shards": {    "total": 5,    "successful": 5,    "skipped" : 0,    "failed": 0  },  "hits": {    "total": 1,    "max_score": 0.5753642,    "hits": [      {        "_index": "my-index",        "_type": "_doc",        "_id": "2",        "_score": 0.5753642,        "_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 matchin a more dynamic way. For example a create date or a popularity fieldcan 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 thenumber of terms that have been specified.An example that always limits the number of required terms to match to neverbecome 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
 |