ruby - rails elastic search relationship attributes not indexed -
basically got 3 models(book,chapter,author), , want include of books , author attributes when indexing chapter.
here chapter.rb
class chapter < activerecord::base belongs_to :book, :counter_cache => true include elasticsearch::model index_name [rails.env, model_name.collection.gsub(/\//, '-')].join('_') mappings indexes :id, type: :integer indexes :title, type: :string indexes :description, type: :string indexes :content, type: :string indexes :updated_at, type: :date # date example indexes :book_title indexes :book_type indexes :author_name indexes :book_id end def book_title book.title end def book_type book.book_type end def author_name " #{book.author.firstname} #{book.author.lastname} " end def to_indexed_json to_json methods: [:book_title, :book_type, :author_name] end end
http://localhost:9200/development_chapters/_mapping?pretty shows correct mapping
{ "development_chapters" : { "mappings" : { "chapter" : { "properties" : { "author_name" : { "type" : "string" }, "book_title" : { "type" : "string" },.... } } } } }
then why not author_name, book_title etc... in search results
<elasticsearch::model::response::result:0x00000105e393a0 @result=#<hashie::mash _id="415" _index="development_chapters" _score=1.0 _source=#<hashie::mash book_id=153 content="[\"explicabo accusantium odit .\"]" created_at="2015-04-22t18:43:58.586z" description="you can't generate application without quantifying cross-platform sdd bandwidth!" id=415 title="future communications orchestrator" updated_at="2015-04-22t18:43:58.586z"> _type="chapter">>
you defining wrong serialization method. elasticsearch::model
searches method as_indexed_json
, defining to_indexed_json
. in elasticesearch-model gem can find examples https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/examples/activerecord_associations.rb#l82
it should this:
def as_indexed_json(options = {}) as_json methods: [:book_title, :book_type, :author_name] end
Comments
Post a Comment