在轮胎中加倍?

时间:2014-06-10 14:13:10

标签: ruby-on-rails elasticsearch tire

使用ROR 4.01,elasticsearch和轮胎宝石。

文章模型

class Article
 belongs_to: region
 belongs_to: house
end

def self.search(params)
    tire.search(page: params[:page], per_page: 12) do
      query { string params[:query], default_operator: "AND" } if params[:query].present?
      filter :term, region_id: params[:region_id] if params[:region_id].present?
      filter :term, house_id: params[:house_id] if params[:house_id].present?

      facet "regions" do
        terms :region_id
      end

      facet "houses" do
        terms :house_id
      end

    end  
  end

物品管理员

def index
    if params[:query].present?
      @articles = Article.search(params)
    else
      @articles = Article.all
    end
end

文章索引视图

%h3 Regions
          %ul
          - @articles.facets['regions']['terms'].each do |facet|
            %li
              = link_to_unless_current Region.find(facet['term']).name, params.merge(region_id: facet['term'])
              - if params[:region_id] == facet['term'].to_s
                (#{link_to "remove", region_id: nil})
              - else
                (#{facet['count']})
          %h3 House Owners
          %ul
          - @articles.facets['houses']['terms'].each do |facet|
            %li
              = link_to_unless_current House.find(facet['term']).name, params.merge(house_id: facet['term'])
              - if params[:house_id] == facet['term'].to_s
                (#{link_to "remove", house_id: nil})
              - else
                (#{facet['count']})

基于地区或房主的文章过滤/方面工作。但不是两个。它如何一起工作?

那么使用多面?可能吗?

谢谢

REMCO

1 个答案:

答案 0 :(得分:1)

tire is deprecated并且当前的Elasticsearch版本不支持,因此我将从内存和源代码开始,但是......

terms应该接受一系列术语。

因此,您应该能够使用这两个术语创建一个方面:

  facet "houses_regions" do
    terms [:house_id, :region_id]
  end