ElasticSearch 中的条件查询?

Conditional query in ElasticSearch?

我对 ElasticSearch 比较陌生,需要一些建议。 经过几次尝试,我没有找到解决方案,这就是我需要你的原因。

我想根据文档内容进行条件查询

让我解释一下,我在 ES 中有那些文档:

{
   "name": "Product n°1",
   "type": "Mail",
   "sub": "Letter"
},
{
   "name": "Product n°2",
   "type": "Video",
   "sub": null
},
{
   "name": "Product n°3",
   "type": "Mail",
   "sub": "Postcard"
}

用户可以使用复选框按类型和子项进行筛选(因此,用户可以同时搜索多个类型和子项)

已编辑

客户可以select勾选他们想要从 ES 获得的产品类型(例如:视频、图像、邮件),他们可以select所有这些。

"Document" 类型有 4 个子类型:Letter、Postal Card、Paper、Printed,他们也可以 select 要检索哪个子类型。

因此,我们接纳了一位客户 selected Video and Mail,以及 Letter 作为 Mail 的子类型。

我想 ES returns selected 类型中的所有文档,并且当文档是 "Mail" 类型时只包含字母。

抱歉我的错误,我是 ES 的新手。

感谢大家的帮助!


更新 2

这是我对 Andrei Stefan 解决方案的查询

{
  "query": {
    "filtered": {
      "query": {
        "match_all": []
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "type": [
                  "Video",
                  "Mail"
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type": Mail
                    }
                  },
                  {
                    "terms": {
                      "sub": [
                        "Letters"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

ES returns 所有视频和邮件类型的文档,当类型为邮件时不应用过滤器字母。

@Andrei Stefan 一千辆坦克。更新 2 之间的区别是我必须从第一个 "should" 查询中删除 "Mail"。

{
  "query": {
    "filtered": {
      "query": {
        "match_all": []
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "type": [
                  "Video"
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "type": Mail
                    }
                  },
                  {
                    "terms": {
                      "sub": [
                        "Letters"
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}