简体   繁体   中英

Include other table on Search of Elasticsearch on Rails (gem Tire)

I have the models Book and Author and I'd like to make the rows of Authors' table load using includes method at the same time I use elasticsearch (tire). Let me show the code.

Here's my controller:

  def index
    @books = Book.search(params)
  end

and my Model:

class Book < ActiveRecord::Base
  has_and_belongs_to_many :authors

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :id
    indexes :title, analyzer: 'snowball'
    indexes :summary, analyzer: 'snowball'
    indexes :released_at, type: 'date'
    indexes :edition
    indexes :isbn, analyzer: 'keyword'
    indexes :authors_name
    indexes :author do
      indexes :id
      indexes :name
    end

  end

  def self.search(params)
    tire.search(load: true) do
      query { string params[:query] } if params[:query].present?
    end
  end

  def authors_name
    authors.all
  end

  # Needed to be searchable
  def to_indexed_json
    to_json( include: { authors: { only: [:name] } } )
  end

end

That's the view and rails log:

<%= book.authors_name.map {|a| link_to a.name, author_path(a.id) }.join(', ').html_safe %>


tarted GET "/books?utf8=%E2%9C%93&query=&commit=Search" for 127.0.0.1 at 2014-05-15 17:38:58 -0300
Processing by BooksController#index as HTML
  Parameters: {"utf8"=>"✓", "query"=>"", "commit"=>"Search"}
  Book Load (0.2ms)  SELECT "books".* FROM "books"  WHERE "books"."id" IN (2, 1)
  Author Load (0.2ms)  SELECT "authors".* FROM "authors" INNER JOIN "authors_books" ON "authors"."id" = "authors_books"."author_id" WHERE "authors_books"."book_id" = ?  [["book_id", 2]]
  Author Load (0.1ms)  SELECT "authors".* FROM "authors" INNER JOIN "authors_books" ON "authors"."id" = "authors_books"."author_id" WHERE "authors_books"."book_id" = ?  [["book_id", 1]]
  Rendered books/index.html.erb within layouts/application (11.2ms)
Completed 200 OK in 69ms (Views: 62.7ms | ActiveRecord: 0.5ms)

But till now I didn't manage to it. Is there a way I can do it?

I made it!

As I already loaded the authors' indexes:

indexes :author do
  indexes :id
  indexes :name
end

I just needed to make sure id and name were send via json:

def to_indexed_json
  to_json include: :authors
end

Then I can do this in the view:

<span>By <%= book.authors.map { |a| link_to a.name, author_path(a.id) }.join(', ').html_safe %></span>

And I removed load: true from the code which makes the page load pretty much faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM