【发布时间】:2017-05-30 14:21:53
【问题描述】:
我正在 Rails 中构建 JSON API,我想使用 Elasticsearch 来加快响应速度并允许搜索。
我刚刚为我的第一个模型实现了 elasticsearch-rails Gem,我可以从控制台成功查询 ES。
现在我想向 API 消费者提供结果,例如,对 /articles/index.json?q="blah" 的 GET 请求将从 ES 检索匹配的文章并根据 JSON:API 呈现它们标准。
是否可以使用 rails active_model_serializers gem 来实现这一点?我问是因为(与 jbuilder 相比)JSON:API 格式已经得到处理。
编辑:这就是我现在的立场:
在我的模型中,我有以下内容:
require 'elasticsearch/rails'
require 'elasticsearch/model'
class Thing < ApplicationRecord
validates :user_id, :active, :status, presence: true
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
index_name Rails.application.class.parent_name.underscore
document_type self.name.downcase
settings index: { number_of_shards: 1, number_of_replicas: 1 } do
mapping dynamic: 'strict' do
indexes :id, type: :string
indexes :user_id, type: :string
indexes :active, type: :boolean
indexes :status, type: :string
end
end
def as_indexed_json(options = nil)
self.as_json({
only: [:id, :user_id, :active, :status],
})
end
def self.search(query)
__elasticsearch__.search( {
query: {
multi_match: {
query: query,
fields: ['id^5', 'user_id']
}
}
} )
end
end
这可以正确索引 ES 中的模型,并可以搜索 ES 索引。 在我的控制器中,我有:
class ThingsController < ApplicationController
def index
things = Thing.search(params[:query]).results.map{|m| m._source}
render json: things, each_serializer: ThingSerializer
end
end
在序列化器中,目前如下:
class ThingSerializer < ActiveModel::Serializer
attributes :id, :user_id, :active, :status
end
不幸的是,这会在视图中显示以下 JSON:
{"data":[{"id":"","type":"hashie-mashes","attributes":{"user-id":null,"active":null,"status":null}}]}
所以序列化程序没有正确解析结果,结果是从 ES gem 包装到这个 Hashie::Mash 对象中。
【问题讨论】:
-
您最好提供一些代码,这样我们就不会在黑暗中开枪了。但是,您想要实现的目标可以通过 jbuilder 实现
-
我没有代码 atm,我在问这是否可行。我在模型中实现了 elasticsearch-rails,我想通过 active_model_serializers 呈现 JSON。 Jbuilder 显然是替代方案,但正如问题中所述,我必须让每个模型的数据 JSON:API 都符合我自己的要求
标签: ruby-on-rails json ruby elasticsearch active-model-serializers