Skip to content
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

Cache results mapping inside Results #325

Closed
wants to merge 1 commit into from
Closed

Conversation

RmMsr
Copy link

@RmMsr RmMsr commented Jan 27, 2015

Seems to be the intention of the assignment.

Seems to be the intention of the assignment.
@jeebster
Copy link

jeebster commented Feb 3, 2015

+1, in my application I do need to reference Elasticsearch::Model::Response#results and Elasticsearch::Model::Response#records multiple times and can confirm a request is sent everytime these methods are called. It would be great to cache these!

@karmi
Copy link
Contributor

karmi commented Apr 11, 2015

Hi, this is weird -- can you provide more details?

Because as soon as you call response.response, you'll get back a final response object, which is memoized.

I think the assignment is simply wrong on that line. For one, I try to stay away from memoization whenever possible, second, the object should be already memoized.

@jeebster I tried it in the activerecord_article.rb example and the search is not fired when you repeatedly access records or results:

> response = Article.search 'foo';
> response.records.to_a
2015-04-11 10:07:25 +0200: GET http://localhost:9200/articles/article/_search?q=foo [status:200, request:0.005s, query:0.002s]
2015-04-11 10:07:25 +0200: < {"took":2,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.10848885,"hits":[{"_index":"articles","_type":"article","_id":"3","_score":0.10848885,"_source":{"title":"Foo Foo","published_at":null,"created_at":"2015-04-11T08:05:46.375Z","updated_at":"2015-04-11T08:05:46.375Z"}},{"_index":"articles","_type":"article","_id":"1","_score":0.076713204,"_source":{"title":"Foo","published_at":null,"created_at":"2015-04-11T08:05:46.367Z","updated_at":"2015-04-11T08:05:46.367Z"}}]}}
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (3, 1)
=> [#<Article:0x007f901b91db80
  id: 3,
  title: "Foo Foo",
  published_at: nil,
  created_at: 2015-04-11 08:05:46 UTC,
  updated_at: 2015-04-11 08:05:46 UTC>,
 #<Article:0x007f901b91dec8
  id: 1,
  title: "Foo",
  published_at: nil,
  created_at: 2015-04-11 08:05:46 UTC,
  updated_at: 2015-04-11 08:05:46 UTC>]
> response.records.to_a
  Article Load (0.1ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" IN (3, 1)
=> [#<Article:0x007f901d14f988
  id: 3,
  title: "Foo Foo",
  published_at: nil,
  created_at: 2015-04-11 08:05:46 UTC,
  updated_at: 2015-04-11 08:05:46 UTC>,
 #<Article:0x007f901d1547a8
  id: 1,
  title: "Foo",
  published_at: nil,
  created_at: 2015-04-11 08:05:46 UTC,
  updated_at: 2015-04-11 08:05:46 UTC>]
> response.results.to_a
=> [#<Elasticsearch::Model::Response::Result:0x007f901bd844a8
  @result=
   {"_index"=>"articles",
    "_type"=>"article",
    "_id"=>"3",
    "_score"=>0.10848885,
    "_source"=>
     {"title"=>"Foo Foo", "published_at"=>nil, "created_at"=>"2015-04-11T08:05:46.375Z", "updated_at"=>"2015-04-11T08:05:46.375Z"}}>,
 #<Elasticsearch::Model::Response::Result:0x007f901bd84430
  @result=
   {"_index"=>"articles",
    "_type"=>"article",
    "_id"=>"1",
    "_score"=>0.076713204,
    "_source"=>
     {"title"=>"Foo", "published_at"=>nil, "created_at"=>"2015-04-11T08:05:46.367Z", "updated_at"=>"2015-04-11T08:05:46.367Z"}}>]
> response.results.to_a
=> [#<Elasticsearch::Model::Response::Result:0x007f901d494d78
  @result=
   {"_index"=>"articles",
    "_type"=>"article",
    "_id"=>"3",
    "_score"=>0.10848885,
    "_source"=>
     {"title"=>"Foo Foo", "published_at"=>nil, "created_at"=>"2015-04-11T08:05:46.375Z", "updated_at"=>"2015-04-11T08:05:46.375Z"}}>,
 #<Elasticsearch::Model::Response::Result:0x007f901d494b48
  @result=
   {"_index"=>"articles",
    "_type"=>"article",
    "_id"=>"1",
    "_score"=>0.076713204,
    "_source"=>
     {"title"=>"Foo", "published_at"=>nil, "created_at"=>"2015-04-11T08:05:46.367Z", "updated_at"=>"2015-04-11T08:05:46.367Z"}}>]
> 

@karmi karmi added the waiting label Apr 11, 2015
@karmi
Copy link
Contributor

karmi commented Apr 22, 2015

Hi guys, can I ask for more feedback on my last message and code example?

@karmi karmi closed this in 55e50c6 May 22, 2015
@karmi
Copy link
Contributor

karmi commented May 22, 2015

Hi, I've removed the assignment altogether, seems to more floow the intent.

Please reopen or open a new issue if you think there's a problem, especially if you see the search request being executed repeatedly, @jeebster!

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

Successfully merging this pull request may close these issues.

3 participants