elasticsearch学习之terms set 查询-kb88凯时官网登录

来自:网络
时间:2023-09-07
阅读:
目录

什么是 terms set 查询?

查询根据匹配给定字段的精确术语的最少数量返回文档。

terms set 查询与 term 查询有何不同?

terms set query 和 terms query 之间的唯一区别是你可以提供必须匹配的最少数量的术语才能检索特定文档。

什么是 minimum_should_match_field 参数?

指向文档的数字()字段名称,其值应用作要匹配的最少术语数,以便返回文档。

minimum_should_match_script 参数是什么?

一个自定义脚本,用于确定为了返回文档而必须匹配的最少术语数。 如果你必须动态设置匹配所需的术语数,那么它将很有帮助。

示例

让我们首先创建索引:

`
 put product
 {
   "mappings": {
     "properties": {
       "name": {
         "type": "keyword"
       },
       "tags": {
         "type": "keyword"
        },
        "tags_count": {
          "type": "long"
        }
      }
    }
  }
`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

让我们索引样本文件:

`
  post product/_doc/prod1
  {
    "name":"iphone 13",
    "tags":["apple","iphone","mobile"],
    "tags_count":3
  }
  post product/_doc/prod2
  {
    "name":"iphone 12",
    "tags":["apple","iphone"],
    "tags_count":2
  }
  post product/_doc/prod3
  {
    "name":"iphone 11",
    "tags":["apple","mobile"],
    "tags_count":2
  }
`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

使用 minimum_should_match_field 参数查询:

用例 1:下面的查询将返回所有 3 个文档,因为 prod1 的最小术语匹配 (tags_count) 是 3,prod2 是 2,prod3 是 2,查询中传递了总共 3 个术语("apple", "iphone", "mobile")。

 post product/_search
 {
   "query": {
     "terms_set": {
       "tags": {
         "terms": [ "apple", "iphone", "mobile" ],
         "minimum_should_match_field": "tags_count"
       }
     }
    }
  }

上述查询的结果是:

 `    "hits": {
     "total": {
       "value": 3,
       "relation": "eq"
     },
     "max_score": 1.4010588,
     "hits": [
       {
         "_index": "product",
          "_id": "prod1",
          "_score": 1.4010588,
          "_source": {
            "name": "iphone 13",
            "tags": [
              "apple",
              "iphone",
              "mobile"
            ],
            "tags_count": 3
          }
        },
        {
          "_index": "product",
          "_id": "prod2",
          "_score": 0.7876643,
          "_source": {
            "name": "iphone 12",
            "tags": [
              "apple",
              "iphone"
            ],
            "tags_count": 2
          }
        },
        {
          "_index": "product",
          "_id": "prod3",
          "_score": 0.7876643,
          "_source": {
            "name": "iphone 11",
            "tags": [
              "apple",
              "mobile"
            ],
            "tags_count": 2
          }
        }
      ]
    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

用例二:下面的查询将只返回一个文档,因为查询中只传递了 2 个术语,仅与 prod3 匹配。 prod1 将不会返回,因为 tags_count 值为 3 并且查询中传递的总术语仅为 2。

 post product/_search
 {
   "query": {
     "terms_set": {
       "tags": {
         "terms": [ "apple", "mobile" ],
         "minimum_should_match_field": "tags_count"
       }
     }
    }
  }

上述查询的结果为:

 `    "hits": {
     "total": {
       "value": 1,
       "relation": "eq"
     },
     "max_score": 0.5007585,
     "hits": [
       {
         "_index": "product",
          "_id": "prod3",
          "_score": 0.5007585,
          "_source": {
            "name": "iphone 11",
            "tags": [
              "apple",
              "mobile"
            ],
            "tags_count": 2
          }
        }
      ]
    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

minimum_should_match_script 示例:

现在让我们看看如何使用 minimum should match 的动态值检索相同的索引数据。

在下面的示例中,查询中提供的术语总数的值将作为最小应匹配值传递。 我们将使用 params.num_terms 来计算查询中提供的术语数。 需要匹配的词条数不能超过 params.num_terms,即 terms 字段中提供的词条数。

 post product/_search
 {
   "query": {
     "terms_set": {
       "tags": {
         "terms": ["apple","iphone"],
         "minimum_should_match_script": {
           "source": "params.num_terms"
         }
        }
      }
    }
  }

它将返回 prod1 和 prod2,因为 minimum_should_match 值将设置为 2,因为我们在查询中仅传递了 2 个术语。上述命令的返回值为:

 `      "hits": [
       {
         "_index": "product",
         "_id": "prod2",
         "_score": 0.5007585,
         "_source": {
           "name": "iphone 12",
           "tags": [
             "apple",
              "iphone"
            ],
            "tags_count": 2
          }
        },
        {
          "_index": "product",
          "_id": "prod1",
          "_score": 0.5007585,
          "_source": {
            "name": "iphone 13",
            "tags": [
              "apple",
              "iphone",
              "mobile"
            ],
            "tags_count": 3
          }
        }
      ]
    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

让我们考虑一个场景,你想要考虑 tags_count 的最小值或查询中的术语数; 在这种情况下,以下查询会有所帮助:

 post product/_search
 {
   "query": {
     "terms_set": {
       "tags": {
         "terms": ["apple","iphone"],
         "minimum_should_match_script": {
           "source": "math.min(params.num_terms, doc['tags_count'].value)"
         }
        }
      }
    }
  }

上述查询的结果为:

 `      "hits": [
       {
         "_index": "product",
         "_id": "prod2",
         "_score": 0.61233616,
         "_source": {
           "name": "iphone 12",
           "tags": [
             "apple",
              "iphone"
            ],
            "tags_count": 2
          }
        },
        {
          "_index": "product",
          "_id": "prod1",
          "_score": 0.61233616,
          "_source": {
            "name": "iphone 13",
            "tags": [
              "apple",
              "iphone",
              "mobile"
            ],
            "tags_count": 3
          }
        }
      ]
    }`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

terms set 查询 elasticsearch java 客户端

下面的代码将有助于使用 elasticsearch java 客户端实现术语集查询。

using new java api client (8.x)

`
 list tags = new arraylist();
 tags.add("apple");
 tags.add("iphone");
 // using minimum_should_match_field param
 query query1 = query.of(q -> q.bool(boolquery.of(bq -> bq.must(ts -> ts.termsset(
 		termssetquery.of(tq -> tq.field("tags").minimumshouldmatchfield("tags_count").terms(tags)))))));
 //using minimum_should_match_script param
  map param = new hashmap();
  query query1 = query
  		.of(q -> q.bool(boolquery.of(bq -> bq.must(ts -> ts.termsset(termssetquery.of(tq -> tq.field("tags")
  				.minimumshouldmatchscript(sc -> sc.inline(in -> in.lang("painless").source("params.num_terms").params(param)))
  				.terms(tags)))))));
`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

使用 java high level 客户端(已弃用)

`
 map param = new hashmap();
 script script = new script(scripttype.inline, "painless", "params.num_terms", param);
 list tags = new arraylist();
 tags.add("apple");
 tags.add("iphone");
 // using minimum_should_match_field
 querybuilder query = querybuilders.boolquery()
  		.must(new termssetquerybuilder("tags", tags).setminimumshouldmatchfield("tags_count"));
  // using minimum_should_match_script
  map param = new hashmap();
  script script = new script(scripttype.inline, "painless", "params.num_terms", param);
  querybuilder query = querybuilders.boolquery()
  		.must(new termssetquerybuilder("tags", tags).setminimumshouldmatchscript(script));
`![](https://csdnimg.cn/release/blogv2/dist/pc/img/newcodemorewhite.png)

以上就是elasticsearch学习之terms set 查询的详细内容,更多关于elasticsearch terms set 查询的资料请关注其它相关文章!

返回顶部
顶部
网站地图