ruby-on-rails - 使用 Tire 和 ElasticSearch 搜索多个模型

标签 ruby-on-rails ruby ruby-on-rails-3 elasticsearch tire

我有两个模型,帖子和 channel ,我正在使用 Tire 和 ElasticSearch 进行搜索。

目前,一切正常。但是,我正在执行两次搜索并返回两者的结果。我想将其整合为一个——同时搜索这两个模型。

下面是我用来执行此操作的 Controller 操作:

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

下面是 channel 模型:

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

同样,我的目标是执行一次搜索,并使用一个循环显示混杂在一起的帖子和 channel 的结果。

如有任何帮助,我们将不胜感激......我正在用这个来解决问题!


更新

添加应用跟踪...

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'

回答

我将 Controller 中的浏览操作替换为:

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

然后在 View 中循环遍历@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

关于ruby-on-rails - 使用 Tire 和 ElasticSearch 搜索多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950470/

相关文章:

css - 我的 .scss 不在我的 ruby​​ 应用程序中

ruby-on-rails - 如何从另一个 Controller 内部获取 Controller 的响应?

ruby-on-rails - Rails 3 应用程序中未初始化的常量 UsersController::PostComment

ruby-on-rails - 在 AASM 中自动更改状态的最佳方法是什么

ruby-on-rails - Ruby on Rails RMagick gem 安装问题

ruby-on-rails - Rails - Rake 错误 : Library not loaded

ruby-on-rails - 如何将所有 shopify 过滤器添加到标准液体中

jquery - 仅创建第一个动态生成的选择框

ruby-on-rails - 从字符串中删除尾随 "?"

javascript - Rails 仅在 current_user logged_in 时执行 javascript