Multiple filters with Ruby DSL

Hi

I'm trying to implement multiple filters inside one query with Ruby DSL API, but couldn't figure out how to do it from the documentation. Since "filtered" queries are removed since ES 2.0 I have to use the "boolean" query as I understood, but there is no proper example.

I'm basically trying to create this query through Ruby DSL:

    {
      "query" : {
        "bool": {
           "filter" : [
             { "term" : {"DestWeather": "Rain"}},
             { "range": { "timestamp": { "gte": "now-100d" } } }
            ]
            }
        }
      }

I found the answer from the source code. You need to repeat "filter" clauses inside the "bool", like this:

    my_query = search do
                  query do
                    bool do
                      filter do
                          term DestWeather: "Rain"
                        end
                        filter do
                          term DestCountry: "IT"
                        end
                        filter do
                          range :timestamp do
                            gte 'now-100d'
                        end
                      end
                    end
                  end
                end

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.