简体   繁体   English

添加了Tyre搜索gem,如何添加多个索引?

[英]Added Tire search gem, how to add multiple indexes?

I have a song.rb model and a genre.rb model that I'd like to be able to search. 我有一个想要搜索的song.rb模型和genre.rb模型。

For some reason Tire is only returning search results for songs and not genres. 由于某种原因,Tire只返回歌曲的搜索结果,而不返回流派的搜索结果。

The code: 编码:

genre.rb

class Genre < ActiveRecord::Base
  has_many :genre_songs, :dependent => :destroy
  has_many :songs, through: :genre_songs

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


end

song_controller snippit song_controller片段

def index
    if params[:query].present?
      @songs = Song.search(params[:query], load: true)
    elsif params[:genre]
      @songs = Song.tagged_with(params[:genre]).paginate(:page => params[:page], :per_page => 15)
      get_last_song
    else      
      @songs = Song.order('id').order('plusminus desc nulls last').paginate(:page => params[:page], :per_page => 15) 
      #@songs = Song.tally.paginate(:page => params[:page], :per_page => 15)
      get_last_song
    end
  end

song.rb snippit song.rb代码段

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

index.html.erb snippit index.html.erb代码段

<%= form_tag songs_path, method: :get do %>
  <p>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil %>
  </p>
<% end %>

in controller: 在控制器中:

  @songs = Song.search(params)

in song model: 在歌曲模型中:

  def self.search(params)
    tire.search(load: true, page: params[:page], per_page: 15) do
      query { string params[:query], default_operator: "AND" } if params[:query].present?

    end
  end

  def to_indexed_json
    to_json(methods: [:genre_names])
  end

  def genre_names
    genres.map{ |g| g.name}   
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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