2013-04-22 69 views
0

我使用輪胎rubygem連接到ElasticSearch。你如何根據特定字段的值返回方面?按輪胎/彈性搜索中的整個字段值分組

例如,我有一個字段areas,我想顯示前10個領域的過濾器選項:

地區

  • 上東區22
  • 華西村15
  • Soho 8
  • etc ...
 
def self.search(params={}) 
     tire.search(page: 1 || params[:page], per_page: 10, load: true) do 
      # Set up query 
      query do 
      boolean do 
       must { string params[:query], default_operator: "AND" } if params[:query].present? 
       must { range :price, { gte: (params[:min_price] || 0), lte: params[:max_price] } } if params[:max_price].present? 
      end 
      end 
      # Facets to return 
      # this is where I get stuck: 
      facet 'areas' do 
      terms [:area_name], size: 10 
      end 
     end 
end 

小面塊剛剛返回:

{"areas"=>{"_type"=>"terms", "missing"=>5754, "total"=>0, "other"=>0, "terms"=>[]}} 

即使所有的記錄有區域名稱的值。

回答