Skip to content

[MODEL] Avoid executing search twice; Reuse response in Response#raw_response #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 16, 2018

Conversation

estolfo
Copy link
Contributor

@estolfo estolfo commented Oct 15, 2018

Alternative to #847

@coneybeare
Copy link

I think it looks great and would solve my particular issue as well, as long as @response.to_hash actually returns the exact same object as a raw_response would.

@estolfo
Copy link
Contributor Author

estolfo commented Oct 16, 2018

Hi @coneybeare Great! I'm glad it resolves your issue. The #to_hash implementation is here so it will return the exact hash representation. But perhaps you are referring to whether the keys will be identical. If that's the case, I believe they will be because HashWrapper inherits from Hashie::Mash and it looks like nothing is done to remove keys when it is initialized here.

@estolfo estolfo merged commit 93a0da0 into elastic:master Oct 16, 2018
@iridakos
Copy link

iridakos commented May 13, 2019

Hi @coneybeare @estolfo

This implementation still triggers double searches if the raw_response is accessed before the response method.

To overcome this I used the following monkey patch:

module Elasticsearch
  module Model
    module Response
      class Response
        def response
          @response ||= HashWrapper.new(raw_response)
        end

        def raw_response
          @raw_response ||= search.execute!
        end
      end
    end
  end
end

Referring commit: 62e2fae#commitcomment-33515229

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants