结合Filter和Bool的ElasticSearch

juud5qan  于 2023-03-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(122)

我试图将“filter”与“bool”/“should”组合在“must”中。下面的查询是由应用程序自动生成的(因此出现了嵌套)。要在通配符条件和filter语句之间使用AND条件,查询必须是什么样子?

"query":{
      "bool":{
         "filter": {
            "bool": {
              "must": [],
              "must_not": [],
              "should": [],
              "filter": {
                "bool": {
                  "should": [
                    {
                      "wildcard": {
                        "attachment.content_type.keyword": "video/*"
                      }
                    }
                  ],
                  "must": [],
                  "must_not": [],
                  "filter": [
                    {
                      "terms": {
                        "status.keyword": [
                          "xxx"
                        ]
                      }
                    }
                  ]
                }
              }
            }
          }
      }
   }
lsmepo6l

lsmepo6l1#

如果你需要这两个条件之间的AND,那么你只需要这个,因为两个约束都是精确匹配,没有任何评分,那么bool/filter就是你所需要的。

{
  "query": {
    "bool": {
      "filter": [
        {
          "wildcard": {
            "attachment.content_type.keyword": "video/*"
          }
        },
        {
          "terms": {
            "status.keyword": [
              "xxx"
            ]
          }
        }
      ]
    }
  }
}

更新日期:

你可以把所有的东西都放进巢里,但它不会带来任何东西

{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": {
              "wildcard": {
                "attachment.content_type.keyword": "video/*"
              }
            }
          }
        },
        {
          "bool": {
            "filter": {
              "terms": {
                "status.keyword": [
                  "xxx"
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题