Limit the Elasticsearch results count in Rails - ruby-on-rails

I'm running Rails with Elasticsearch using the elasticsearch-rails and will_paginate gems and things are working great.
The only problem I'm having is that my records count has grown over 10.000 which gives the error "Result window is too large, from + size must be less than or equal to: [10000] but was [10200]" when someone clicks on the last page of the pagination links.
Now I don't want to mess with index.max_result_window because my dataset is going be much much larger and I really don't need visitors to be able to view every page and record anyway.
So basically I just want to set a limit of 10.000 on my results but I'm having no luck getting that working.
This is the query I have in my controller action:
#response = Price.search(params)
#response = #response.paginate(page: params[:page], per_page: 24)
#prices = #response.records.includes(shop: :pictures)
#prices_count = #response.total_entries
#prices = #prices.decorate
#results = #response.results
And I feed the #response to will_paginate using
will_paginate(#response)
I've tried using .limit(10000) in various places and giving a total_entries value to will_paginate but nothing seems to work.
Do you guys have any ideas?
Thanks!

I had the same Issue, in my case I'm using kaminari but should work the same with will_paginate:
= paginate #keyword_search_results
With 25 results per page after 400 page it throws the index.max_result_window error, so I just add the :total_pages option to paginate
= paginate #keyword_search_results, {:total_pages => 400}
Would be a good idea to calculate that value dynamically based on your needs.

use per_page option
#response = Price.search(params, per_page: 24)

Related

Rails Sunspot Kaminari Max results 30

I'm using Sunspot and Kaminari for pagination. However, I'm seeing that the max results returned (even though there is more) is 30.
Here's my code:
Kaminari.paginate_array(search.results).page(params[:page]).per(10)
Please advise on how to return all results and paginate accordingly.
All results from Solr are paginated... By default, Sunspot requests the first 30 results from Solr. In your example, you're paginating the array of the first 30 results returned by Solr.
I find it easiest to just call paginate in the search blocks in my controllers...
#search = Sunspot.search(BlogPost) do
# filters...
paginate(page: params[:page], per_page: 10)
end
Then use Kaminari's paginate helper in my views...
<%= paginate #search.results %>
* you could use #search.hits if you don't want to load model instances
Alternatively, it looks like you can pass in :total_count as a keyword argument to Kaminari's paginate_array method. So this should work if you want to stick with the method you're using above...
Kaminari.paginate_array(search.results, total_count: search.total).page(params[:page]).per(10)

Tire + Will_Paginate not taking Tire.options

I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.
Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.
#results.inspect yields this:
#<Tire::Search::Search:0x007f88ab9153d0 #indices=["deja-set-development"], #types=[], #options={:load=>true, :page=>1, :per_page=>2}, #path="/deja-set-development/_search", #query=#<Tire::Search::Query:0x007f88ab915088 #value={:query_string=>{:query=>"oh"}}>, #facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>
Here is where I call will_paginate:
= will_paginate #search_results.results, params
Here is where I iterate through the results
#search_results.results.each
Does anyone have any thoughts?
Edit ---
I'm not sure what is going on, but I did this and it is working.
#search_results = #search_results.paginate(:page => params[:page], :per_page => 5)
Please see the integration test in Tire, and make sure you're passing all options properly.
So to clarify, I've attached my github correspondence with #karmi here.
https://github.com/karmi/tire/issues/627#issuecomment-13449368
I was using Tire.search as opposed to searching by model. As #karmi notes, at the moment :per_page and :page are not supported with Tire.
Here is how I solved this:
#search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size do
query do
string q, :default_operator => 'AND', :fields => [:name1, :name2]
end
end
I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.
http://www.elasticsearch.org/guide/reference/api/search/from-size.html
You're able to still access
= #search_results.results.total_entries/next_page/previous_page
which helps with pagination.
Thank you again #karmi.

Lazy load in RoR App (paginate results)

I want to realize some kind of lazy loading in my rails app. That means I have a query result of about 50 000 lines. I want them to be shown on 50 pages with 100 lines each.
I can't load all the data before because it makes my browser crashing down.
So how do I just load a part of my data at a time?
Does anyone can tell me some keywords that I can google?!
Thanks in advance!
Try the will_paginate gem for this functionality: https://github.com/mislav/will_paginate
It allows you to do things like #posts = Post.order('created_at DESC').paginate(:page => params[:page]) in the controller and <%= will_paginate #posts %> in the view.

Limiting Number of Posts Per Page - Ruby on Rails Blog

I am currently developing a Ruby on Rails blog. I have my blog posts show up on the main page, however, I would like to list the posts 5 at a time, so that my frontpage doesn't go on forever and my blog will look much cleaner.
Let me know if you can help. Much appreciated.
Looks like you need a pagination solution - consider using kaminari or will_paginate ( https://github.com/amatsuda/kaminari, https://github.com/mislav/will_paginate/wiki )
And if you need an endless page, there is a nice screencast about that: http://railscasts.com/episodes/114-endless-page
For example, if using will_paginate for pagination, you just call paginate method at end of line your query inside controller, for example inside your controller
def index
#blogs = Blog.all.paginate(:page => params[:page], :per_page => 5)
end
from your view, just simply put:
will_paginate #blogs
at specify location, to show pagination.
If I understand right, you want to limit the number of post on the home page . Then you should do like
Model.find(:all, :limit => 5, :order=> 'created_at desc')
you can remove the order if you don't need it. If you need to make pagination take a look at will_paginate

What's a better way to use helper methods with will_paginate?

I'm using the latest version of will_paginate with rails 3. I'd like to use out_of_bounds? to set the current page to the last page if the page parameter is higher than the last page. I found a way to do it like this:
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.paginate(:page => (people.out_of_bounds? ? people.total_pages : params[:page))
The problem with this is that I have to call paginate twice. The first time to create a WillPaginate::Collection in order to use the out_of_bounds? and total_pages methods, and the second time to actually set the current page. I also need to do this with more actions so it's getting kinda out of hand. I can't use a before_filter or an after_filter, either (I don't think?). Is there a better way to achieve this?
I don't know the best solution, but at least you can do the check before the second call to the method (I suppose that in most of the cases, the page parameter will be ok, so you wont have to call paginate twice):
people = People.all.paginate :page => params[:page], :per_page => 20
#people = people.out_of_bounds? ? people.paginate(:page => people.total_pages) : people

Resources