最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Elasticsearch學(xué)習(xí)之Terms?set?查詢

 更新時間:2023年02月02日 14:26:08   作者:Elasticsearch  
這篇文章主要為大家介紹了Elasticsearch學(xué)習(xí)Terms?set?查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

什么是 terms set 查詢?

Terms set 查詢根據(jù)匹配給定字段的精確術(shù)語的最少數(shù)量返回文檔。

terms set 查詢與 term 查詢有何不同?

Terms set query 和 Terms query 之間的唯一區(qū)別是你可以提供必須匹配的最少數(shù)量的術(shù)語才能檢索特定文檔。

什么是 minimum_should_match_field 參數(shù)?

指向文檔的數(shù)字(numeric)字段名稱,其值應(yīng)用作要匹配的最少術(shù)語數(shù),以便返回文檔。

minimum_should_match_script 參數(shù)是什么?

一個自定義腳本,用于確定為了返回文檔而必須匹配的最少術(shù)語數(shù)。 如果你必須動態(tài)設(shè)置匹配所需的術(shù)語數(shù),那么它將很有幫助。

示例

讓我們首先創(chuàng)建索引:

`
 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 參數(shù)查詢:

用例 1:下面的查詢將返回所有 3 個文檔,因為 prod1 的最小術(shù)語匹配 (tags_count) 是 3,prod2 是 2,prod3 是 2,查詢中傳遞了總共 3 個術(shù)語("apple", "iphone", "mobile")。

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

上述查詢的結(jié)果是:

 `    "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 個術(shù)語,僅與 prod3 匹配。 prod1 將不會返回,因為 tags_count 值為 3 并且查詢中傳遞的總術(shù)語僅為 2。

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

上述查詢的結(jié)果為:

 `    "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 示例:

現(xiàn)在讓我們看看如何使用 minimum should match 的動態(tài)值檢索相同的索引數(shù)據(jù)。

在下面的示例中,查詢中提供的術(shù)語總數(shù)的值將作為最小應(yīng)匹配值傳遞。 我們將使用 params.num_terms 來計算查詢中提供的術(shù)語數(shù)。 需要匹配的詞條數(shù)不能超過 params.num_terms,即 terms 字段中提供的詞條數(shù)。

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

它將返回 prod1 和 prod2,因為 minimum_should_match 值將設(shè)置為 2,因為我們在查詢中僅傳遞了 2 個術(shù)語。上述命令的返回值為:

 `      "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 的最小值或查詢中的術(shù)語數(shù); 在這種情況下,以下查詢會有所幫助:

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

上述查詢的結(jié)果為:

 `      "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 客戶端實現(xiàn)術(shù)語集查詢。

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)

以上就是Elasticsearch學(xué)習(xí)之Terms set 查詢的詳細(xì)內(nèi)容,更多關(guān)于Elasticsearch Terms set 查詢的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

兴文县| 扶沟县| 胶南市| 呼伦贝尔市| 达日县| 望江县| 房山区| 江川县| 泸定县| 通榆县| 江北区| 和平县| 龙胜| 延津县| 台南县| 洛扎县| 长顺县| 五家渠市| 瑞昌市| 万州区| 尉犁县| 浙江省| 乐山市| 新竹县| 友谊县| 大英县| 南木林县| 抚顺县| 绵竹市| 饶河县| 迁安市| 龙江县| 玉山县| 邓州市| 永顺县| 通江县| 乌鲁木齐市| 登封市| 昭觉县| 石景山区| 长宁区|