0

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?

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.