-
我想要这个表单
"数据":[{"id":2."searchable_type":"用户";,"电子邮件":"abc";,"first_name":"abc";,"last_name":"xyz";,"created_at":"2022-08-05T09:40:18.986Z";,"updated_at":"2022-08-05T09:40:18.986Z";},{"id":3."searchable_type":"博客";,"tittle":"用户";,"created_at":"2022-08-05T09:40:18.986Z";,"updated_at":"2022-08-05T09:40:18.986Z";}]
我想通过使用Pg Search多重搜索分别返回每个模型的整个对象,它只返回一个内容,searchable_type和searchable_id作为下面的附加图像。这是我的邮递员图像,用于博客返回它是我的邮差图像,用于用户返回我的搜索控制器,用户和博客模型]
class Api::V1::SearchController < Api::V1::ApiController
def index
@query = params[:value]
@results = PgSearch.multisearch(@query)
render json: { data: @results}, status: :ok
end
end
class Blog < ApplicationRecord
include PgSearch::Model
multisearchable against: [:id, :title, :created_at, :updated_at ]
end
class User < ApplicationRecord
include PgSearch::Model
multisearchable against: [:id, :email, :first_name, :last_name ]
end
更改
@results = PgSearch.multisearch(@query)
至
@results = PgSearch.multisearch(@query).includes(:searchable).map(&:searchable)