elasticsearch-rails: Search method conflicting with Ransack gem

I’ve been using the Ransack Gem for a while, and recently started playing around with ElasticSearch. It seems that the search method added to AR objects by ElasticSearch overrides the one added by Ransack, which obviously breaks search forms built using Ransack.

Example:

# Gemfile
gem "ransack"
gem "elasticsearch-model" 
gem "elasticsearch-rails"
# /models/article.rb
class Article < ActiveRecord::Base
     include Elasticsearch::Model
end

Using the above will return an Elastic Search object

Article.search
=> #<Elasticsearch::Model::Response::Response:0x007fc449f37680....

When removing “include Elasticsearch::Model” as follows:

# Gemfile
gem "ransack"
gem "elasticsearch-model" 
gem "elasticsearch-rails"
# /models/article.rb
class Article < ActiveRecord::Base
end

Using the above will return a Ransack object

Article.search
=> Ransack::Search<class: OpportunityLocation, base: Grouping <combinator: and>>

What would be the best way to resolve this?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 3
  • Comments: 17 (8 by maintainers)

Most upvoted comments

def self.search_for(*args,&block)
      __elasticsearch__.search(*args, &block)
end

worked for me 😉

@dmitry Raise the issue with Ransack, that it simply overwrites the search method on your model. The elasticsearch-model doesn’t overwrite any existing method.

You shouldn’t use the SearchRequest class – you should simply wrap the MyModel.__elasticsearch__.search proxy method. It is designed specifically with problems like these in mind. Have you checked the corresponding README section? https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model#the-__elasticsearch__-proxy