0

I have simple Rails app with a Post model that belongs to a creator (which is a User object) and I want to find posts that match the creator's first_name.

class Post < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks
  belongs_to :creator, polymorphic: true

  mapping do
    indexes :id, type: 'integer'
    indexes :title, boost: 10
    indexes :creator do
      indexes :first_name
      indexes :service_type
    end
  end
end

When I run search, there are no results:

Post.tire.search(load: true, page: params[:page], per_page: params[:per_page]) do
  query do
    match 'creator.first_name', 'Matt'
  end
end

Even when I've run rake environment tire:import:all FORCE=true and verified that a Post exists with a creator that has a first name of 'Matt' in the database.

Is there something I am missing? Thanks!

UPDATE:

Adding the following to Post.rb did the trick:

def to_indexed_json
  as_json.merge(
    creator: creator.as_indexed_json
  ).to_json
end

Apparently, you need to specify the association in the to_indexed_json to make it work.

Matt Sears
  • 1
  • 1
  • 1
  • Have you seen http://stackoverflow.com/a/11711477/95696 ? Could you post output of your `Post.first.to_indexed_json`? How did you verify the post exists? Are you running this in automated tests or manually (beware of 1sec delay in ES). – karmi Jul 06 '13 at 11:13
  • I've added an update to my original post, but apparently I need to specify the association in the to_index_json to make it work. I'm not sure why, but it's working now. – Matt Sears Jul 08 '13 at 14:49
  • That might be a bug/deficiency in the default `to_indexed_json` implementation, can you submit a Github issue? – karmi Jul 14 '13 at 21:31

0 Answers0