Elasticsearch学习之Terms set 查询

作者:Elasticsearch 时间:2021-12-01 01:52:19 

什么是 terms set 查询?

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

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

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

什么是 minimum_should_match_field 参数?

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

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<String> tags = new ArrayList<String>();
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<String, JsonData> param = new HashMap<String, JsonData>();
 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<String, Object> param = new HashMap<String, Object>();
Script script = new Script(ScriptType.INLINE, "painless", "params.num_terms", param);
List<String> tags = new ArrayList<String>();
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<String, Object> param = new HashMap<String, Object>();
 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)

来源:https://juejin.cn/post/7195385253211406373

标签:Elasticsearch,Terms,set,查询
0
投稿

猜你喜欢

  • java将一个目录下的所有数据复制到另一个目录下

    2023-01-08 15:11:44
  • SpringBoot如何实现word文档转pdf

    2023-04-19 09:33:55
  • ehcache模糊批量移除缓存的方法

    2023-01-11 12:30:37
  • C#微信公众号开发之自定义菜单

    2023-01-23 02:07:08
  • c# 基于GMap.NET实现电子围栏功能(WPF版)

    2023-01-28 05:13:01
  • c# 复写Equals方法的实现

    2023-06-09 01:35:35
  • SWT(JFace)体验之复制粘贴

    2022-11-18 13:47:46
  • Java多线程Thread类的使用详解

    2023-11-11 13:08:11
  • 详解JAVA 弱引用

    2022-03-12 01:30:29
  • Springboot JPA 枚举Enum类型存入到数据库的操作

    2023-11-25 16:11:40
  • 详解Spring框架注解扫描开启之配置细节

    2022-11-01 18:04:14
  • SpringMVC框架整合Junit进行单元测试(案例详解)

    2022-04-10 20:53:37
  • Java实现图形化界面的日历

    2023-03-04 04:07:47
  • java使用OGEngine开发2048

    2023-07-23 06:07:43
  • Java读取json数据并存入数据库的操作代码

    2023-09-23 06:00:57
  • 使用maven自定义插件开发

    2022-10-07 02:21:37
  • 浅谈C#与Java两种语言的比较

    2023-09-26 13:05:19
  • 使用Stargate访问K8ssandra的过程之Springboot整合Cassandra

    2022-02-08 23:12:25
  • java实现FTP文件上传与文件下载

    2023-08-16 08:28:38
  • Android仿百度图片查看功能

    2023-09-26 07:50:24
  • asp之家 软件编程 m.aspxhome.com