在我的Rails应用程序中,我使用的是太阳黑子,因此用户可以搜索“引脚”(如帖子)。一切正常,我将数据作为JSON返回。
但是,我还想在JSON响应中包含与每个“pin”相关联的“用户”数据。
我不需要将“用户”包含在搜索中,而只需要在搜索完成后将每个“引脚”作为数据。
我的搜索工作正常,但我无法弄清楚如何添加用户数据。
module Api
module V1
class PinsController < ApplicationController
respond_to :json
def index
_search = Sunspot.search(Pin) do
fulltext params[:q] do
highlight :title
phrase_fields :title => 2.0
phrase_slop 1
minimum_match 1
end
facet(:position_id, :type_id, :instrument_id, :genre_ids, :skill_id)
with(:position_id, params[:position]) if params[:position].present?
with(:type_id, params[:type]) if params[:type].present?
with(:instrument_id, params[:instrument]) if params[:instrument].present?
with(:genre_ids).all_of(params[:genre]) if params[:genre].present?
with(:skill_id, params[:skill]) if params[:skill].present?
paginate(:page => params[:page], :per_page => 25)
end
_pins = _search.results
_pins_positions = []
_pins_types = []
_pins_instruments = []
_pins_genres = []
_pins_skills = []
_search.facet(:position_id).rows.each do |row|
_facet = Position.find_by_id(row.value)
_object = _facet.attributes
_object["count"] = row.count
_pins_positions << _object
end
_search.facet(:type_id).rows.each do |row|
_facet = Type.find_by_id(row.value)
_object = _facet.attributes
_object["count"] = row.count
_pins_types << _object
end
_search.facet(:instrument_id).rows.each do |row|
_facet = Instrument.find_by_id(row.value)
_object = _facet.attributes
_object["count"] = row.count
_pins_instruments << _object
end
_search.facet(:genre_ids).rows.each do |row|
_facet = Genre.find_by_id(row.value)
_object = _facet.attributes
_object["count"] = row.count
_pins_genres << _object
end
_search.facet(:skill_id).rows.each do |row|
_facet = Skill.find_by_id(row.value)
_object = _facet.attributes
_object["count"] = row.count
_pins_skills << _object
end
_pins_positions = _pins_positions.sort_by { |e| e["title"] }
_pins_types = _pins_types.sort_by { |e| e["title"] }
_pins_instruments = _pins_instruments.sort_by { |e| e["title"] }
_pins_genres = _pins_genres.sort_by { |e| e["title"] }
_pins_skills = _pins_skills.sort_by { |e| e["title"] }
_response = {
:pins => _pins,
:facets => {
:positions => _pins_positions,
:types => _pins_types,
:instruments => _pins_instruments,
:genres => _pins_genres,
:skills => _pins_skills
}
}
respond_with _response
end
end
end
end
典型的回答如下:
{"pins":[{
"id":95,
"user_id":22,
"title":"Ratione sint odio vitae in quis aspernatur.",
"description":"Sunt ut pariatur aut ipsum sit unde alias.",
"latitude":null,
"longitude":null,
"position_id":1,
"type_id":1,
"instrument_id":4,
"skill_id":1,
"soundcloud_url":null,
"created_at":"2012-09-04T06:54:17.000Z",
"updated_at":"2013-10-14T21:58:17.000Z",
"deleted_at":null
}],
"facets":{
"positions":[{
"id":1,
"title":"Non-Professional",
"token":"non-professional","count":2
}],
"types":[{
"id":1,
"title":"Wanted",
"token":"wanted",
"count":2
}],
"instruments":[{
"id":4,
"title":"Synth",
"token":"synth",
"count":2
}],
"genres":[{
"id":12,
"title":"Folk",
"token":"folk",
"count":2
},
{
"id":16,"title":"Hip Hop","token":"hiphop","count":2}
],
"skills":[{
"id":3,
"title":"Advanced",
"token":"advanced",
"count":1},
{
"id":1,
"title":"Novice",
"token":"novice",
"count":1
}]
}}
但是,理想情况下,我想在JSON中为每个“pin”包含相关的“用户”数据。正如你所看到的,我在这里所有的都是“user_id”...但是我希望这个JSON API相当“平坦”并包含另一个名为“user”的哈希键,其中包含“first_name”,“age”等字样, “性别”等。
如果我正在使用传统的View模板,我可以将“pin”作为实例变量公开,并执行以下操作:@pin.user.profile.first_name
- 在Sunspot中构建数据时如何执行此操作?
希望我已经说清楚了。非常感谢!
答案 0 :(得分:0)
我通过创建一个空数组,从Sunspot循环结果,找到相应的用户并推送到数组来找到解决方案。
_results = _search.results
_pins = []
_results.each do |pin|
_object = pin.attributes
_object["user"] = { :profile => pin.user.profile }
_pins << _object
end
我现在得到一个结构,其中配置文件数据嵌套在引脚哈希中的用户密钥中,如下所示: