[英]Search multiple models with Tire and ElasticSearch
我有兩個模型,帖子和頻道,我正在使用Tire和ElasticSearch搜索。
目前,一切正常。 但是,我正在執行兩次搜索並返回兩者的結果。 我想將其合並為一個 - 同時搜索兩個模型。
下面是我正在使用的控制器操作:
def browse
@user = current_user
@channels = Channel.search(params)
@posts = Post.search(params)
end
以下是Post模型:
class Post < ActiveRecord::Base
attr_accessible :description, :name, :tag_list, :thumbnail_image, :status, :post_type, :subtitle, :summary, :visibility, :user
acts_as_taggable
has_one :publication
has_one :user, :through => :publication
has_many :subscriptions, dependent: :destroy
has_many :channel_post_connections, dependent: :destroy
has_many :channels, :through => :channel_post_connections
accepts_nested_attributes_for :channel_post_connections
accepts_nested_attributes_for :channels
accepts_nested_attributes_for :publication
accepts_nested_attributes_for :user
has_many :post_pages, dependent: :destroy
validates_presence_of :post_type, :name, :subtitle, :tag_list, :summary, :thumbnail_image, :if => :post_is_published?
include Tire::Model::Search
include Tire::Model::Callbacks
index_name 'posts_index'
def self.search(params)
tire.search(load: true) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
sort { by :created_at, "desc" } if params[:query].blank?
end
end
def to_indexed_json
to_json(methods: [:user_first_name, :user_display_name])
end
def user_first_name
self.user.first_name if self.user.present?
end
def user_display_name
self.user.display_name if self.user.present?
end
end
以下是渠道模型:
class Channel < ActiveRecord::Base
attr_accessible :title, :description, :cover_image, :status, :writable, :visibility, :thumbnail_image
has_one :channel_publication
has_one :user, :through => :channel_publication
has_many :channel_subscriptions
has_many :channel_post_connections
accepts_nested_attributes_for :channel_publication
accepts_nested_attributes_for :user
mount_uploader :thumbnail_image, ChannelThumbnailImageUploader
include Tire::Model::Search
include Tire::Model::Callbacks
index_name 'channels_index'
def self.search(params)
tire.search(load: true) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
sort { by :created_at, "desc" } if params[:query].blank?
end
end
def to_indexed_json
to_json(methods: [:user_first_name, :user_display_name])
end
def user_first_name
self.user.first_name if self.user.present?
end
def user_display_name
self.user.display_name if self.user.present?
end
end
然后,在我看來,我有兩個循環:
- @posts.each do |post|
%h3
= post.name
- @channels.each do |channel|
%h3
= channel.title
再一次 - 我的目標是執行一次搜索,並使用一個循環顯示帖子和頻道混雜在一起的結果。
任何幫助將不勝感激...我正在用這一個拉我的頭發!
添加應用跟蹤...
app/controllers/posts_controller.rb:111:in `[]'
app/controllers/posts_controller.rb:111:in `block in browse'
app/controllers/posts_controller.rb:110:in `browse'
我將控制器中的瀏覽操作替換為:
def browse
@user = current_user
@search_items = Tire.search(['posts_index', 'channels_index'],{load: true}) do |search|
if params[:query].present?
search.query do |q|
q.string params[:query], default_operator: "AND"
end
end
search.sort { by :created_at, "desc" }
end
@results = @search_items.results
end
然后在視圖中循環@results - 它工作了!
您可以使用,
def browse
@search_items = Tire.search(['posts_index', 'channels_index'],{load: true}) do |search|
search.query do |q|
q.string params[:query], default_operator: "AND" if params[:query].present?
end
search.sort { by :created_at, "desc" } if params[:query].blank?
end
@results = @search_items.results
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.