Terms set Query
词条集查询
Returns documents that contain a minimum number of exact terms in a provided field.
返回字段中包含最少数目的精确词条的文档。
The terms_set
query is the same as the terms
query, except you can define the number of matching terms required to return a document. For example:
terms_set
查询与terms
查询相同,除了您可以定义返回文档所需的匹配词条数目。例如:
-
A field,
programming_languages
, contains a list of known programming languages, such asc++
,java
, orphp
for job candidates. You can use theterms_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 theterms_set
query to return documents that match a subset of these permissions. -
字段,
programming_languages
包含已知的编程语言列表,比如求职者提交的c++
,java
或php
等。您可以使用terms_set
查询与至少两种语言匹配的文档。 -
字段
permissions
包含应用程序可能的用户权限的列表。您可以使用terms_set
查询返回与这些权限的子集匹配的文档。
Example request
Index setup
建立索引
In most cases, you’ll need to include a 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.
在大多数情况下,您需要在索引中包括数字字段mapping才能使用terms_set
查询。此数字字段包含返回文档所需的匹配词条数。
To see how you can set up an index for the terms_set
query, try the following example.
若要查看如何为terms_set
查询建立索引,请尝试以下示例。
-
Create an index,
job-candidates
, with the following field mappings:使用下面的字段mapping创建
job-candidates
索引:-
name
, akeyword
field. This field contains the name of the job candidate. -
programming_languages
, akeyword
field. This field contains programming languages known by the job candidate. -
required_matches
, a numericlong
field. This field contains the number of matching terms required to return a document. -
name
,一个keyword
字段。此字段包含求职者的姓名。 -
programming_languages
,一个keyword
字段。此字段包含已知的求职者的编程语言。 -
required_matches
,一个数字long
字段。此字段包含返回文档所需的匹配词条数。
PUT /job-candidates { "mappings": { "properties": { "name": { "type": "keyword" }, "programming_languages": { "type": "keyword" }, "required_matches": { "type": "long" } } } }
Copy as cURLView in Console
-
-
Index a document with an ID of
1
and the following values:用下面的值添加索引ID为
1
的文档:-
Jane Smith
in thename
field. -
["c++", "java"]
in theprogramming_languages
field. -
2
in therequired_matches
field. -
name
字段为Jane Smith
。 -
programming_languages
字段为["c++", "java"]
。 -
在
required_matches
字段为2
。
Include the
?refresh
parameter so the document is immediately available for search.包括
?refresh
参数,以便文档可以立即进行搜索。PUT /job-candidates/_doc/1?refresh { "name": "Jane Smith", "programming_languages": ["c++", "java"], "required_matches": 2 }
Copy as cURLView in Console
-
-
Index another document with an ID of
2
and the following values:用下面的值添加索引ID为
2
的文档:-
Jason Response
in thename
field. -
["java", "php"]
in theprogramming_languages
field. -
2
in therequired_matches
field. -
name
字段为Jason Response
。 -
programming_languages
字段为["java", "php"]
。 -
required_matches
字段为2
。
PUT /job-candidates/_doc/2?refresh { "name": "Jason Response", "programming_languages": ["java", "php"], "required_matches": 2 }
Copy as cURLView in Console
-
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.
现在,您可以将字段required_matches
的值用作返回terms_set
查询中的文档所需的匹配项数。
Example query
The following search returns documents where the programming_languages
field contains at least two of the following terms:
下面的搜索返回programming_languages
字段至少包含以下两个词条的文档:
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.
minimum_should_match_field
是required_matches
。这意味着所需的匹配项数为2
,即required_matches
字段的值。
GET /job-candidates/_search
{
"query": {
"terms_set": {
"programming_languages": {
"terms": ["c++", "java", "php"],
"minimum_should_match_field": "required_matches"
}
}
}
}
Copy as cURLView in Console
Top-level parameters for terms_set
terms_set
的参数
-
field
(Required, object) Field you wish to search.
(必填,对象)您要搜索的字段。
Parameters for field
-
terms
(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.(必需,字符串数组)您希望在
field
中搜索的词条数组。要返回文档,所需词条数量必须与字段值完全匹配,包括空格和大写字母。The required number of matching terms is defined in the
minimum_should_match_field
orminimum_should_match_script
parameter.所需的匹配项数在
minimum_should_match_field
或minimum_should_match_script
参数中定义 。 -
minimum_should_match_field
(Optional, string) Numeric field containing the number of matching terms required to return a document.
(可选,字符串)数字字段,包含返回文档所需的匹配词条数。
-
minimum_should_match_script
(Optional, string) Custom script containing the number of matching terms required to return a document.
(可选,字符串)自定义脚本中返回文档所需的匹配词条数。
For parameters and valid values, see Scripting.
有关参数和有效值,请参见Scripting。
For an example query using the
minimum_should_match_script
parameter, see How to use theminimum_should_match_script
parameter.有关使用
minimum_should_match_script
参数进行查询的示例,请参阅 如何使用minimum_should_match_script
参数。
Notes
How to use the minimum_should_match_script
parameter
如何使用minimum_should_match_script
参数
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.
您可以使用minimum_should_match_script
来定义脚本所需数量的匹配项。如果您需要动态设置所需术语的数量,这将很有用。
Example query using minimum_should_match_script
使用minimum_should_match_script
的示例
The following search returns documents where the programming_languages
field contains at least two of the following terms:
下面的搜索返回programming_languages
字段至少包含词条数组中的两个的文档:
c++
java
php
The source
parameter of this query indicates:
source
查询的参数表示:
-
The required number of terms to match cannot exceed
params.num_terms
, the number of terms provided in theterms
field. -
The required number of terms to match is
2
, the value of therequired_matches
field. -
匹配
terms
中的项数不能超过字段params.num_terms
提供的项数。 -
匹配
terms
中的项数不能超过2
,即字段required_matches
的值 。
GET /job-candidates/_search
{
"query": {
"terms_set": {
"programming_languages": {
"terms": ["c++", "java", "php"],
"minimum_should_match_script": {
"source": "Math.min(params.num_terms, doc['required_matches'].value)"
},
"boost": 1.0
}
}
}
}
Copy as cURLView in Console