elasticsearch,tire,Ruby On Rails,Ruby,Ruby On Rails 3,elasticsearch,Tire" /> elasticsearch,tire,Ruby On Rails,Ruby,Ruby On Rails 3,elasticsearch,Tire" />

Ruby on rails 使用轮胎和ElasticSearch搜索多个模型

Ruby on rails 使用轮胎和ElasticSearch搜索多个模型,ruby-on-rails,ruby,ruby-on-rails-3,elasticsearch,tire,Ruby On Rails,Ruby,Ruby On Rails 3,elasticsearch,Tire,我有两个模型,帖子和频道,我正在使用Tire和ElasticSearch进行搜索 目前,一切正常。但是,我正在执行两次搜索并返回两次搜索的结果。我想将其合并为一个模型,并同时搜索两个模型 下面是我用于执行此操作的控制器操作: def browse @user = current_user @channels = Channel.search(params) @posts = Post.search(params) end 以下是Post模型: class Post &l

我有两个模型,帖子和频道,我正在使用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
再一次–我的目标是执行一次搜索,并使用一个循环显示混乱在一起的帖子和频道的结果

任何帮助都将不胜感激。。。我要用这个把头发拔出来


更新 正在添加应用程序跟踪

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

我试着把它放在“def self.search(params)”块中——运气不好:没有为#定义方法'each'。鉴于上面的代码,您建议我如何实现这一点?我尝试了许多方法来重新调整它的用途,但没有取得任何成功。这显然是我想要的,但是,你需要把它放在浏览方法中,而不是self.search方法中。检查我编辑过的答案。啊,好的,谢谢——我想我已经很接近了。。。然而,在实现了编辑后的答案后,我得到了一个类型错误:“不能将符号转换为整数”,当然,谢谢您花时间来帮助我。我已将应用程序跟踪添加到我的原始帖子中。第111行是查询{…尝试在Tire.search调用之前获取params散列,或者您也可以使用block方法。请参阅我的编辑块方法。检查这些链接并
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
      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