Rails轮胎搜索在协会has_many

时间:2015-03-24 14:06:31

标签: ruby-on-rails elasticsearch tire retire

这是我的模特:

模型/ business_center.rb:

mapping do
  indexes :id, type: 'integer'
  indexes :name, type: 'string'
  indexes :address, type: 'string'
  indexes :offices_pub_count, type: 'integer'
  indexes :subtype_id, type: 'integer'
  indexes :offices_pub_retail_count, type: 'integer'
  indexes :metro_id, type: 'integer'
  indexes :area_id, type: 'integer'
  indexes :district_id, type: 'integer'
  indexes :latitude, type: 'integer'
  indexes :longitude, type: 'integer'

  indexes :offices do
    indexes :id,      type: 'integer'
    indexes :desc,      type: 'string'
    indexes :floor,      type: 'string'
    indexes :inner_info,      type: 'string'
    indexes :decoration,      type: 'string'
    indexes :fire_safety,      type: 'string'
    indexes :air_conditioning,      type: 'string'
    indexes :parking,      type: 'string'
    indexes :planning,      type: 'string'
    indexes :commercial_terms,      type: 'string'
    indexes :operation_cost_id,      type: 'string'
    indexes :retail,      type: 'boolean'

end

def as_indexed_json(options={})
  to_json(methods: [:offices_pub_count],
      include: {offices: { only: [:text,:desc,:floor,:inner_info,:decoration,:fire_safety,:air_conditioning,:parking,:planning,
                                     :commercial_terms,:operation_cost_id, :retail]
                }
      }

def offices_pub_count
  offices_pub.size
end

has_many :offices, :dependent => :destroy
has_many :offices_pub, class_name: 'Office', foreign_key:   'business_center_id', conditions: {published: true}    

下次我尝试搜索:

BusinessCenter.search(:load => { :include => 'offices' }) do
    query { string '*' }
    filter :term, "offices.retail" => true
end

及其获取空数组

 => #<Tire::Results::Collection:0x0000000641fa90 @response={"took"=>4, "timed_out"=>false, "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0}, "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]}}, @options={:load=>{:include=>"offices"}, :size=>10}, @time=4, @total=0, @facets=nil, @max_score=0.0, @wrapper=Tire::Results::Item>

我犯错误的地方?如何在has_many模型中使用属性过滤请求(在我的情况下是Office)?

我看到了许多示例,但它们与has_one或belongs_to关联一起使用。 https://gist.github.com/karmi/3200212中的示例现在正常工作。 求你帮我!)

1 个答案:

答案 0 :(得分:0)

据我所知,Tire不支持此功能。 Chewy(由TopTal开发)gem可以很好地在关联模型中进行简单搜索。你应该使用他或其他东西,但轮胎错误的方式。这是真的)