轮胎/ Mongoid单表继承 - 搜索多个模型

时间:2013-09-27 09:09:01

标签: ruby-on-rails mongoid elasticsearch tire

我正试图让STI使用轮胎/ mongoid数小时。我希望有人可以帮助我。

我有两个不同索引的模型。应该可以通过Event.search搜索两个索引,但也可以使用CoursePlan.search搜索适当的索引。

第一个模型:事件

# encoding: utf-8
class Event
  include Mongoid::Document
  include Mongoid::Timestamps
  include Tire::Model::Search
  include Tire::Model::Callbacks

  # rest of class omitted...

  # Elasticsearch
  index_name  "events" # Indexname /initializers/tire.rb
  mapping do
    indexes :_id, :index => :not_analyzed
    indexes :title
    indexes :place
    indexes :description
    indexes :fill_out
    indexes :current_user_keyword, analyzer: "keyword"
  end

第二种模式:CoursePlan

# encoding: utf-8
class CoursePlan < Event
  include Mongoid::Document
  include Mongoid::Timestamps
  include Tire::Model::Search
  include Tire::Model::Callbacks
  include TimeHelper

  # rest of class omitted...

  # Elasticsearch
  index_name  "course_plans"
  tire.index.add_alias "events"
  mapping do
    indexes :_id, :index => :not_analyzed
    indexes :title
    indexes :place
    indexes :description
    indexes :fill_out
    indexes :user_email
    indexes :course_title
    indexes :course_area
    indexes :course_user_email
    indexes :date
  end

我已经尝试过richarddong的解决方案,请参阅tire issue #178。但这似乎只在某些情况下有效。

这个问题有解决方案吗?也许我错了。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

请检查此答案,

Search multiple models with Tire and ElasticSearch

     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