coc*_*nup 1 ruby json ruby-on-rails elasticsearch active-model-serializers
我正在 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
Run Code Online (Sandbox Code Playgroud)
这正确地索引了 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
Run Code Online (Sandbox Code Playgroud)
目前,在序列化程序中,如下所示:
class ThingSerializer < ActiveModel::Serializer
attributes :id, :user_id, :active, :status
end
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会在视图中显示以下 JSON:
{"data":[{"id":"","type":"hashie-mashes","attributes":{"user-id":null,"active":null,"status":null}}]}
Run Code Online (Sandbox Code Playgroud)
所以序列化器没有正确解析结果,结果是从 ES gem 包装到这个 Hashie::Mash 对象中的。
我终于设法让它很好地工作,而无需从数据库中获取记录。这是未来谷歌员工的完整解决方案:
序列化程序(可能更好地为搜索结果创建一个专用的序列化程序):
class SearchableThingSerializer < ActiveModel::Serializer
type 'things' # This needs to be overridden, otherwise it will print "hashie-mashes"
attributes :id # For some reason the mapping below doesn't work with :id
[:user_id, :active, :status].map{|a| attribute(a) {object[:_source][a]}}
def id
object[:_source].id
end
end
Run Code Online (Sandbox Code Playgroud)
控制器:
def index
things = Thing.search(params[:query])
render json: things, each_serializer: SearchableThingSerializer
end
Run Code Online (Sandbox Code Playgroud)
有了这个,您可以按照本指南中的描述构建一个 JSON API,还有直接从 Elasticsearch 提供数据的额外好处:
| 归档时间: |
|
| 查看次数: |
1026 次 |
| 最近记录: |