user3269780
user3269780

Reputation: 127

Tire search with single table inheritance in Rails

I'm using tire for the implementation of Elasticsearch. I have done single table inheritance everything is working fine but the problem is I also want to search on all the post have a look at my code:

# Post is Parent Class
class Post < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks    

  mapping do
    indexes :title,     type: 'string'
    indexes :location,  type: 'string'
    indexes :key_words, type: 'string'
    indexes :work_type, type: 'string'
  end

  def self.search(params)
    search =  tire.search do
      if params[:location].present? || params[:query].present?
        query do
          string "location:#{params[:location]}" + "*", default_operator: 'AND' if params[:location].present?
          string "title:#{params[:query]}" + "*",       default_operator: 'AND' if params[:query].present?
          string "key_words:#{params[:query]}" + "*",   default_operator: 'AND' if params[:query].present?
        end
      end
      filter :terms, work_type: params[:work_types] if params[:work_types].present?
    end
    search.results
  end

# child class

class Job < Post
  tire.index_name 'posts'
end

# another child class

class Bid < Post
  tire.index_name 'posts'
end

Following are the few things I want to do:

Post.search(params)
OR
Job.search(params)
Bid.search(params)

How can make the search happen with STI?

Upvotes: 0

Views: 304

Answers (1)

edariedl
edariedl

Reputation: 3352

Besides tire.index_name you have to define tire.document_type, so your children classes should look like following

# child class

class Job < Post
  tire.index_name 'posts'
  tire.document_type 'post'
end

# another child class

class Bid < Post
  tire.index_name 'posts'
  tire.document_type 'post'
end

Tire is dead project, you should consider to use new official elasticsearch gem.

Upvotes: 1

Related Questions