Filter applied to only one model with Tire and ElasticSearch in Rails

I have several models indexed using elasticsearch and one of them has an association that needs to be filtered out, but if I add something like this to the method search

  search.filter :range, 'sessions.starts_on' => {:gte => start_date,
                                                 :lte => ends_on}

then all models are filtered out because they don’t have such an association sessions.

So, is there a way to apply a filter to only one model?

Any thoughts are greatly appreciated!

+5
source share
1 answer

, , . , , elastics. , , json elasticsearch

def to_model_json
    self.document.merge({"sessions" => sessions.as_json}).to_json
end

json elistics , .

,

search_object = Tire::Search::Search::new(Model.index_name, other attributes)

, https://github.com/karmi/tire

,

 search_object.filter :range, 'sessions.starts_on' => {:gte => start_date,
                                                  :lte => ends_on}
+1

All Articles