ElasticSearch + Rails多个`必须'无法正常工作

时间:2017-05-18 16:00:11

标签: ruby-on-rails ruby elasticsearch elasticsearch-model

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

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}}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果有多个语句,则必须将其添加到数组中,如下所示:

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