Skip to content

undefined method to_hash' for class Elasticsearch::Model::Response::Result' #430

Closed
@johaned

Description

@johaned

I cannot use Model.search({}).to_a and similar methods when I am using Mongoid, I have the following code:

# app/models/user.rb
# Code extracted from a devise model, also it is using Mongoid 4.0.2 as its ODM
class User
  include Mongoid::Document
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  index_name "users-#{Rails.env}"

  field :email, type: String, default: ""
  field :first_name, type: String, default: ""
  field :last_name, type: String, default: ""

  def as_indexed_json
    as_json(except: [:_id])
  end

end

I have created my index like this:

  User.__elasticsearch__.create_index! force: true
  User.import

but the search does not work when I try to use the array's methods

  User.search('allam').to_a
  #=> output error: #<NameError: undefined method `to_hash' for class `Elasticsearch::Model::Response::Result'>

I only can use the following code

  User.search('allam').records.to_a

But I do not want to hit the database for retrieving the searched documents

Perhaps did I miss some step?

Thank you for your support!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions