处理solr分面结果

q5lcpyga  于 2022-11-05  发布在  Solr
关注(0)|答案(1)|浏览(148)

我有一个图表:

而我

"city_id":[
        "1",131,
        "2",96,
        "3",11,
        "4",10,
        "7",8,
        "6",6,
        "15",5,
        "10",4,
        "34",4,
        "36",3,
        "59",3,
        "71",1]},

引号中是city_id,右侧是该城市的作业计数
我想把它们处理成这样的散列

{:region_1=> 
{["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count},
:region_2=> 
{["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count,
["city_name", "slug"]=>jobs count},
...
}
vjrehmav

vjrehmav1#

这不是最干净的方法,但它可以工作:

final_hash = {}
Hash[*city_id.flatten(1)].each do |city_id, job_count|
  city = City.find(city_id)
  region = city.region
  slug = city.slug
  city_name = city.name
  final_hash[region] = Hash.new unless final_hash.key?(region)
  final_hash[region][[city_name, slug]] = job_count
end

这会使你的奇怪的数组列表变成一个散列:

Hash[*city_id.flatten(1)]

相关问题