将过滤查询转换为 Elasticsearch 5 的 bool 查询时遇到问题

Having trouble converting a filtered query into a bool query for Elasticsearch 5

我正在从 elasticsearch 1.7 升级到 5.0。其中一项更改是删除过滤查询以支持 bool 查询。例如,我在旧版本的 ES 中使用了这个搜索哈希:

{
 "sort": [ { "updated_at" => "desc" } ],
 "query": {
          "filtered": {
                      "filter": {
                                "bool": {
                                    "must": [
                                                { "term": { "account_id" => 2 } },
                                                { "bool": {
                                                            "should": [
                                                                        { "missing": { "field": "workgroup_ids" } },
                                                                        { "term": { "visibility": 2 } }
                                                                      ]
                                                          }
                                                }
                                            ],
                                            "must_not": [
                                                          { "range": { "expires_at": { "lte": "2016-11-17T16:27:22Z" } } },
                                                          { "range": { "published_at": { "gte": "2016-11-17T16:27:22Z" } } }
                                                        ]
                                        }
                                },
                      "query": {
                                "bool": {
                                          "must": [
                                                    { "query_string": { "query": "name:(*orang.jpg*)" } }
                                                  ]
                                        }
                              }
                      }
          }}

所以,我知道这需要更多的格式:

{ "query": "bool": [{ term: { account_id: 2 } }]} 

我也知道缺少的查询需要替换为 5 中的 must_not 存在的查询。也就是说,我无法弄清楚如何转换这个高度嵌套的散列,所以有人知道我是怎么做的吗是否会为 ES5 正确构造此结构?

我也是 using/accessing ES in Rails 利用 elasticsearch-rails gem, fyi.

我想你可以简单地把它改成这样,它应该可以工作:

{
  "sort": [
    {
      "updated_at": "desc"
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "name:(*orang.jpg*)"
          }
        }
      ],
      "must_not": [
        {
          "range": {
            "expires_at": {
              "lte": "2016-11-17T16:27:22Z"
            }
          }
        },
        {
          "range": {
            "published_at": {
              "gte": "2016-11-17T16:27:22Z"
            }
          }
        }
      ],
      "filter": [
        {
          "bool": {
            "minimum_should_match": 1,
            "should": [
              {
                "bool": {
                  "must_not": {
                    "exists": {
                      "field": "workgroup_ids"
                    }
                  }
                }
              },
              {
                "term": {
                  "visibility": 2
                }
              }
            ]
          }
        },
        {
          "term": {
            "account_id": 2
          }
        }
      ]
    }
  }
}