ElasticSearch + Rails 多个“必须”无法正常工作

ElasticSearch + Rails multiple `must` not working correctly

根据文档,我使用的是这种语法:

self.search(
  query: {
    bool: {
      must: { match: { type: 'user' } },
      must: { match: { status: status } },
      must: { range: { created_at: { from: date_from, to: date_to } } }
    }
  },
  size: 1000
)

我相信它应该寻找所有 3 个条件,但查询忽略了所有 must 但最后一个:

<Elasticsearch::Model::Searching::SearchRequest:0x000000039b2c08 @definition= {:index=>"uusers", :type=>"user", :body=>{:query=>{:bool=>{:must=>{:range=>{:created_at=>{:from=>Mon, 25 Aug 2014, :to=>Thu, 18 May 2017}}}}}, :size=>1000}}

我做错了什么?

当你有多个语句时,你必须将它添加到一个数组中,如下所示:

self.search(
  query: {
    bool: {
      must: [
        { match: { type: 'user' } },
        { match: { status: status } },
        { range: { created_at: { from: date_from, to: date_to } } }
      ]
    }
  },
  size: 1000
)