匹配ElasticSearch中提供的类别ID中的任一个

aoyhnmkz  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(101)

我有以下ElasticSearch参数。
我正在尝试使用查询中的任何ID搜索类别类型。
但是,下面的搜索参数正在获取所有类别类型。

{
  "size": 25,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "organization_id": "dummy_organization"
          }
        },
        {
          "match": {
            "object_type": "category"
          }
        },
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "should": [
                    {
                      "match": {
                        "category_type_id": "94f7dc26b611a5c2"
                      }
                    },
                    {
                      "match": {
                        "category_type_id": "b630613358520d51"
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must_not": [

                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "category_name": {
        "order": "asc"
      }
    }
  ]
}
bq8i3lrv

bq8i3lrv1#

您遗漏了shouds上的最小should匹配计数。
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#bool-min-should-match

You can use the minimum_should_match parameter to specify the number or percentage of should clauses returned documents must match.

If the bool query includes at least one should clause and no must or filter clauses, the default value is 1. Otherwise, the default value is 0.

您还可以简化查询:

{
  "size": 25,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "organization_id": "dummy_organization"
          }
        },
        {
          "match": {
            "object_type": "category"
          }
        }
      ],
      "should": [
        {
          "match": {
            "category_type_id": "94f7dc26b611a5c2"
          }
        },
        {
          "match": {
            "category_type_id": "b630613358520d51"
          }
        }
      ],
      "minimum_should_match": 1
    }
  },
  "sort": [
    {
      "category_name": {
        "order": "asc"
      }
    }
  ]
}

相关问题