Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:我可以同时使用elasticsearch模型和active\u模型\u序列化程序吗?_Ruby On Rails_Json_Ruby_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Active Model Serializers - Fatal编程技术网 elasticsearch,active-model-serializers,Ruby On Rails,Json,Ruby,elasticsearch,Active Model Serializers" /> elasticsearch,active-model-serializers,Ruby On Rails,Json,Ruby,elasticsearch,Active Model Serializers" />

Ruby on rails Rails:我可以同时使用elasticsearch模型和active\u模型\u序列化程序吗?

Ruby on rails Rails:我可以同时使用elasticsearch模型和active\u模型\u序列化程序吗?,ruby-on-rails,json,ruby,elasticsearch,active-model-serializers,Ruby On Rails,Json,Ruby,elasticsearch,Active Model Serializers,我正在Rails中构建一个JSON API,我想使用Elasticsearch来加速响应并允许搜索 我刚刚为我的第一个模型实现了ElasticSearchRails Gem,我可以从控制台成功地查询ES 现在,我想让API消费者可以使用结果,例如,对/articles/index.json?q=“blah”的GET请求将从ES检索匹配的文章,并根据json:API标准呈现它们 是否可以使用rails活动\模型\序列化器gem来实现这一点?我这样问是因为(与jbuilder相比)JSON:API格

我正在Rails中构建一个JSON API,我想使用Elasticsearch来加速响应并允许搜索

我刚刚为我的第一个模型实现了ElasticSearchRails Gem,我可以从控制台成功地查询ES

现在,我想让API消费者可以使用结果,例如,对/articles/index.json?q=“blah”的GET请求将从ES检索匹配的文章,并根据json:API标准呈现它们

是否可以使用rails活动\模型\序列化器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
class ThingSerializer < ActiveModel::Serializer
    attributes :id, :user_id, :active, :status
end

因此,序列化程序没有正确解析从ES gem包装到这个Hashie::Mash对象中的结果。

我最终成功地使其工作良好,而不需要从数据库中获取记录。以下是未来谷歌的完整解决方案:

序列化程序(最好为搜索结果创建专用序列化程序):

有了它,您可以按照本指南中的描述构建JSON API,还可以直接从Elasticsearch提供数据:


我终于设法使它工作得很好,而不需要从数据库中获取记录。以下是未来谷歌的完整解决方案:

序列化程序(最好为搜索结果创建专用序列化程序):

有了它,您可以按照本指南中的描述构建JSON API,还可以直接从Elasticsearch提供数据:


您最好提供少量代码,这样我们就不会在黑暗中拍摄。然而,您希望通过jbuilderI实现的目标是可以实现的。我没有代码atm,我想问这是否可行。。我在一个模型中实现了ElasticSearchRails,我希望通过活动的\u model\u序列化程序呈现JSON。Jbuilder显然是另一种选择,但是,如问题中所述,我必须自己为每个模型制作符合JSON:API的数据。您还可以提供少量代码,这样我们就不会在黑暗中拍摄。然而,您希望通过jbuilderI实现的目标是可以实现的。我没有代码atm,我想问这是否可行。。我在一个模型中实现了ElasticSearchRails,我希望通过活动的\u model\u序列化程序呈现JSON。Jbuilder显然是另一种选择,但是,如问题中所述,我必须让数据JSON:API与我自己的模型兼容
class ThingSerializer < ActiveModel::Serializer
    attributes :id, :user_id, :active, :status
end
{"data":[{"id":"","type":"hashie-mashes","attributes":{"user-id":null,"active":null,"status":null}}]}
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
def index
  things = Thing.search(params[:query])
  render json: things, each_serializer: SearchableThingSerializer
end