ruby-on-railsrubyelasticsearchelasticsearch-model

ElasticSearch + Rails multiple `must` not working correctly


As per docs I am using this syntax:

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

I believe it should be looking for all 3 conditions, but query is ignoring all must but last:

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

What am I doing wrong?


Solution

  • When you have multiple statements, you must add it inside an array, like below:

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