【发布时间】:2018-01-09 22:13:55
【问题描述】:
我正在使用elasticsearch-model,我使用了关注方法,这是我的搜索方法。我已经通过curl尝试了我的查询,它返回了5个条目,但是当我使用这个方法时
def search_index(*args)
self.__elasticsearch__.search(*args)
end
它给我上课了
(byebug) Entry.search_index('test').records
#<Elasticsearch::Model::Response::Records:0x007f8eb85ca280 @klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), @response=#<Elasticsearch::Model::Response::Response:0x007f8eb85ca370 @klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), @search=#<Elasticsearch::Model::Searching::SearchRequest:0x007f8eb85ca438 @klass=[PROXY] Entry(id: integer, created_at: datetime, updated_at: datetime, source_id: integer, data: text, uuid: string, source_entry_id: string, bytesize: integer), @options={}, @definition={:index=>"self_driving_entries", :type=>"entry", :q=>"test"}>, @records=#<Elasticsearch::Model::Response::Records:0x007f8eb85ca280 ...>>, @options={}>
我怎样才能通过这种搜索方法访问我的记录?
【问题讨论】:
-
你有没有试过在结果上调用
to_a? -
当您执行
Entry.search_index('test').records.to_a时会发生什么?此类包含“PROXY”,这表明该类只是记录列表的代理。 -
@MrYoshiji 但我仍然不明白为什么需要将其转换为 array , records 是来自 Elasticsearch::Model::Response::Records
-
它是可打印的,但您打印它的方式是要求打印 proxy,而不是实际记录。当您在控制台中执行简单的
User.where(true)时,输出看起来像一个数组,但如果您执行User.where(true).class,您将看到它不是一个数组,而是来自 proxy 类 ActiveRecord: :关系。 -
在你的 byebug 中,我很确定你可以通过
Entry.search_index('test').records.first返回符合条件的第一个条目(如果有的话)
标签: ruby-on-rails ruby elasticsearch-model