elasticsearch terms_set:无法找到minimum_should_match字段

s3fp2yjn  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(2)|浏览(253)

尝试使用“terms_set”查询时,我完全搞不懂。

首先,in the docs,它说minimum_should_match_field是一个可选字段。但我得到这个failed to create query: No minimum should match has been specified错误,如果我不包括在请求中。如下:

POST bw-ecom-1/_search
{
  "query": {
    "terms_set": {
      "category": { 
        "terms": [
            "Men's wear", "Women's wear", "Unisex"
        ]
      }
    }
  }
}

然后,当我包含minimum_should_match_field时,我得到了另一个错误failed to find minimum_should_match field。我也尝试过传递几个值。例如required_matches123,...

这是我索引的文档:

{
    "id": 1,
    "name": "white t-shirt for men",
    "short_description": "",
    "category": [
        "Men's wear",
        "Women's wear",
        "Unisex"
    ],
    "min_price": 10000,
    "max_price": 15000,
    "discount": "0.05",
    "currency": "VND",
    "created": "2022-03-13",
    "modifided": "1678700615438",
    "indexed_at": "2022-03-13",
    "variants": [
        {
            "color": ["red"],
            "size": ["XL", "L"],
            "form": "short sleeve",
            "price": 12000,
            "variant_id": "v-001",
            "label": "red short sleeve men t-shirt"
        }
    ],
    "inventory": [
        {
            "variant_id": "v-001",
            "storage_qty": 100
        }
    ]
}

谢谢你帮我!

8yparm6h

8yparm6h1#

我不知道您使用terms_set查询的原因,但是required_matches应该在文档中。
例如:

PUT /job-candidates/_doc/1?refresh
{
  "name": "Jane Smith",
  "programming_languages": [ "c++", "java" ],
  "required_matches": 2
}

PUT /job-candidates/_doc/2?refresh
{
  "name": "Jason Response",
  "programming_languages": [ "java", "php" ],
  "required_matches": 2
}

GET /job-candidates/_search
{
  "query": {
    "terms_set": {
      "programming_languages": {
        "terms": [ "c++", "java", "php" ],
        "minimum_should_match_field": "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
      }
    }
  }
}

是的,minimum_should_match_fieldminimum_should_match_script是可选的。但是您需要至少使用其中的一个:)

gv8xihay

gv8xihay2#

抱歉,我忘了说为什么要使用terms_set,因为我想搜索包含所有术语的文档,在这种情况下,您应该这样做:

{
  "query": {
    "terms_set": {
      "category": {
        "terms": ["Men's wear", "Women's wear", "Unisex"],
        "minimum_should_match_script": {
          "source": "params.num_terms"
        }
      }
    }
  }
}

相关问题