Query DSL - Terms set Query

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 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.

  • 字段,programming_languages包含已知的编程语言列表,比如求职者提交的c++javaphp等。您可以使用 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查询建立索引,请尝试以下示例。

  1. Create an index, job-candidates, with the following field mappings:

    使用下面的字段mapping创建job-candidates索引:

    • name, a keyword field. This field contains the name of the job candidate.

    • programming_languages, a keyword field. This field contains programming languages known by the job candidate.

    • required_matches, a numeric long 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

  2. Index a document with an ID of 1 and the following values:

    用下面的值添加索引ID为1的文档:

    • Jane Smith in the name field.

    • ["c++", "java"] in the programming_languages field.

    • 2 in the required_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

  3. Index another document with an ID of 2 and the following values:

    用下面的值添加索引ID为2的文档:

    • Jason Response in the name field.

    • ["java", "php"] in the programming_languages field.

    • 2 in the required_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_fieldrequired_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 or minimum_should_match_script parameter.

    所需的匹配项数在minimum_should_match_fieldminimum_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 the minimum_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 the terms field.

  • The required number of terms to match is 2, the value of the required_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值