2
votes

I have this request :

curl -XGET localhost:9200/users/_search -d '
{
  "query": {
    "filtered": {
      "query": {"match_all": {}},
      "filter": {
        "nested": {
          "path": "apps_events",
          "query":{
            "filtered": {
              "query": { "match_all": {}},
              "filter": {
                "and": [
                  {"term": {"apps_events.status": "active"}},
                  {"terms": {"apps_events.type": ["sale"]}}
                ]
              }
            }
          }
        }
      }
    }
  }
}'

I don't succeed to convert it into Tire (rails gem) language... I don't find in Tire tests any example of nested with filters...

Any ideas?

1

1 Answers

3
votes

Okay I found the answer :

nested_filter = Tire::Search::Query.new do
  filtered do
    query { all }
    filter :term,  { 'apps_events.status' => 'active' }
    filter :terms, { 'apps_events.type'   => ['sale'] }
  end
end

tire.search(page: params[:page], per_page: params[:per_page], load: params[:load]) do
  query do
    filtered do
      query { all }
      # Merge the defined filter as a hash into the `nested` filter
      filter :nested, { path: 'apps_events'}.merge({ query: nested_filter.to_hash })
    end
  end
end

Thanks to @karmiq https://github.com/karmi/tire/issues/660