2017-10-27 07:14:44 -04:00
|
|
|
[[query-dsl-terms-set-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Terms set query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Terms set</titleabbrev>
|
|
|
|
++++
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
Returns documents that contain a minimum number of *exact* terms in a provided
|
|
|
|
field.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
The `terms_set` query is the same as the <<query-dsl-terms-query, `terms`
|
|
|
|
query>>, except you can define the number of matching terms required to
|
|
|
|
return a document. For example:
|
|
|
|
|
|
|
|
* A field, `programming_languages`, contains a list of known programming
|
|
|
|
languages, such as `c++`, `java`, or `php` for job candidates. You can use the
|
|
|
|
`terms_set` query to return documents that match at least two of these
|
|
|
|
languages.
|
|
|
|
|
|
|
|
* A field, `permissions`, contains a list of possible user permissions for an
|
|
|
|
application. You can use the `terms_set` query to return documents that
|
|
|
|
match a subset of these permissions.
|
|
|
|
|
|
|
|
[[terms-set-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
[[terms-set-query-ex-request-index-setup]]
|
|
|
|
===== Index setup
|
|
|
|
In most cases, you'll need to include a <<number, numeric>> field mapping in
|
|
|
|
your index to use the `terms_set` query. This numeric field contains the
|
|
|
|
number of matching terms required to return a document.
|
|
|
|
|
|
|
|
To see how you can set up an index for the `terms_set` query, try the
|
|
|
|
following example.
|
|
|
|
|
|
|
|
. Create an index, `job-candidates`, with the following field mappings:
|
|
|
|
+
|
|
|
|
--
|
|
|
|
|
|
|
|
* `name`, a <<keyword, `keyword`>> field. This field contains the name of the
|
|
|
|
job candidate.
|
|
|
|
|
|
|
|
* `programming_languages`, a <<keyword, `keyword`>> field. This field contains
|
|
|
|
programming languages known by the job candidate.
|
|
|
|
|
|
|
|
* `required_matches`, a <<number, numeric>> `long` field. This field contains
|
|
|
|
the number of matching terms required to return a document.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
PUT /job-candidates
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
2019-06-28 12:56:22 -04:00
|
|
|
"name": {
|
|
|
|
"type": "keyword"
|
|
|
|
},
|
|
|
|
"programming_languages": {
|
|
|
|
"type": "keyword"
|
|
|
|
},
|
2019-01-22 09:13:52 -05:00
|
|
|
"required_matches": {
|
|
|
|
"type": "long"
|
2017-10-27 07:14:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
|
|
|
|
|
|
|
--
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
. Index a document with an ID of `1` and the following values:
|
|
|
|
+
|
|
|
|
--
|
|
|
|
|
|
|
|
* `Jane Smith` in the `name` field.
|
|
|
|
|
|
|
|
* `["c++", "java"]` in the `programming_languages` field.
|
|
|
|
|
|
|
|
* `2` in the `required_matches` field.
|
|
|
|
|
|
|
|
Include the `?refresh` parameter so the document is immediately available for
|
|
|
|
search.
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
PUT /job-candidates/_doc/1?refresh
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
2019-06-28 12:56:22 -04:00
|
|
|
"name": "Jane Smith",
|
|
|
|
"programming_languages": ["c++", "java"],
|
2017-10-27 07:14:44 -04:00
|
|
|
"required_matches": 2
|
|
|
|
}
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
|
|
|
|
--
|
|
|
|
|
|
|
|
. Index another document with an ID of `2` and the following values:
|
|
|
|
+
|
|
|
|
--
|
|
|
|
|
|
|
|
* `Jason Response` in the `name` field.
|
|
|
|
|
|
|
|
* `["java", "php"]` in the `programming_languages` field.
|
|
|
|
|
|
|
|
* `2` in the `required_matches` field.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
PUT /job-candidates/_doc/2?refresh
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
2019-06-28 12:56:22 -04:00
|
|
|
"name": "Jason Response",
|
|
|
|
"programming_languages": ["java", "php"],
|
2017-10-27 07:14:44 -04:00
|
|
|
"required_matches": 2
|
|
|
|
}
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
2017-10-27 07:14:44 -04:00
|
|
|
// CONSOLE
|
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
--
|
|
|
|
|
|
|
|
You can now use the `required_matches` field value as the number of
|
|
|
|
matching terms required to return a document in the `terms_set` query.
|
|
|
|
|
|
|
|
[[terms-set-query-ex-request-query]]
|
|
|
|
===== Example query
|
|
|
|
|
|
|
|
The following search returns documents where the `programming_languages` field
|
|
|
|
contains at least two of the following terms:
|
|
|
|
|
|
|
|
* `c++`
|
|
|
|
* `java`
|
|
|
|
* `php`
|
|
|
|
|
|
|
|
The `minimum_should_match_field` is `required_matches`. This means the
|
|
|
|
number of matching terms required is `2`, the value of the `required_matches`
|
|
|
|
field.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
GET /job-candidates/_search
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"terms_set": {
|
2019-06-28 12:56:22 -04:00
|
|
|
"programming_languages": {
|
|
|
|
"terms": ["c++", "java", "php"],
|
2017-10-27 07:14:44 -04:00
|
|
|
"minimum_should_match_field": "required_matches"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
2017-10-27 07:14:44 -04:00
|
|
|
// CONSOLE
|
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
[[terms-set-top-level-params]]
|
|
|
|
==== Top-level parameters for `terms_set`
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
`<field>`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, object) Field you wish to search.
|
2019-06-28 12:56:22 -04:00
|
|
|
|
|
|
|
[[terms-set-field-params]]
|
|
|
|
==== Parameters for `<field>`
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
`terms`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Required, array of strings) Array of terms you wish to find in the provided
|
|
|
|
`<field>`. To return a document, a required number of terms must exactly match
|
|
|
|
the field values, including whitespace and capitalization.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
The required number of matching terms is defined in the
|
|
|
|
`minimum_should_match_field` or `minimum_should_match_script` parameter.
|
|
|
|
--
|
2017-10-27 07:14:44 -04:00
|
|
|
|
2019-06-28 12:56:22 -04:00
|
|
|
`minimum_should_match_field`::
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, string) <<number, Numeric>> field containing the number of matching
|
|
|
|
terms required to return a document.
|
2019-06-28 12:56:22 -04:00
|
|
|
|
|
|
|
`minimum_should_match_script`::
|
|
|
|
+
|
|
|
|
--
|
2019-07-31 14:18:22 -04:00
|
|
|
(Optional, string) Custom script containing the number of matching terms
|
|
|
|
required to return a document.
|
2019-06-28 12:56:22 -04:00
|
|
|
|
|
|
|
For parameters and valid values, see <<modules-scripting, Scripting>>.
|
|
|
|
|
|
|
|
For an example query using the `minimum_should_match_script` parameter, see
|
|
|
|
<<terms-set-query-script, How to use the `minimum_should_match_script`
|
|
|
|
parameter>>.
|
|
|
|
--
|
|
|
|
|
|
|
|
[[terms-set-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[terms-set-query-script]]
|
|
|
|
===== How to use the `minimum_should_match_script` parameter
|
|
|
|
You can use `minimum_should_match_script` to define the required number of
|
|
|
|
matching terms using a script. This is useful if you need to set the number of
|
|
|
|
required terms dynamically.
|
|
|
|
|
|
|
|
[[terms-set-query-script-ex]]
|
|
|
|
====== Example query using `minimum_should_match_script`
|
|
|
|
|
|
|
|
The following search returns documents where the `programming_languages` field
|
|
|
|
contains at least two of the following terms:
|
|
|
|
|
|
|
|
* `c++`
|
|
|
|
* `java`
|
|
|
|
* `php`
|
|
|
|
|
|
|
|
The `source` parameter of this query indicates:
|
|
|
|
|
|
|
|
* The required number of terms to match cannot exceed `params.num_terms`, the
|
|
|
|
number of terms provided in the `terms` field.
|
|
|
|
* The required number of terms to match is `2`, the value of the
|
|
|
|
`required_matches` field.
|
2017-10-27 07:14:44 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
GET /job-candidates/_search
|
2017-10-27 07:14:44 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"terms_set": {
|
2019-06-28 12:56:22 -04:00
|
|
|
"programming_languages": {
|
|
|
|
"terms": ["c++", "java", "php"],
|
2017-10-27 07:14:44 -04:00
|
|
|
"minimum_should_match_script": {
|
|
|
|
"source": "Math.min(params.num_terms, doc['required_matches'].value)"
|
2019-06-28 12:56:22 -04:00
|
|
|
},
|
|
|
|
"boost": 1.0
|
2017-10-27 07:14:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-28 12:56:22 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|