jkolo
jkolo

Reputation:

how to get all docs with acts_as_solr

I'm doing something like this:

Item.find_by_solr('name:ab*')

and it says it returns 297 results:

=> #<ActsAsSolr::SearchResults:0xb6516858 @total_pages=1, @solr_data={:docs=>[doc1, doc2, doc3...]}, :max_score=>1.6935261, :total_pages=>1, :total=>297}, @current_page=1>

Item.count_by_solr('name:ab*') also returns 297.

Yet when iterate it only shows 10 items:

Item.find_by_solr('reference_name:ab*').each do |i| puts i end

I tried adding {:per_page=>80} and :limit=>:all but it still shows those 10. Any idea what I'm missing?

Upvotes: 1

Views: 733

Answers (4)

Punit Rathore
Punit Rathore

Reputation: 970

You have to specify the :offset parameter in your query.

So to the see the next 10 entries(11th to 20th) of Item you have to do this,

Item.find_by_solr('name:ab*', :offset => 10)

And to see the next 10 entries, you have to again increase the :offset parameter by 10. So the next 10 entries would look like this.

Item.find_by_solr('name:ab*', :offset => 20)

10 entries are fetched because the the default value of the :limit parameter is 10. We can change it to something else, if we want to fetch more than 10 entries at a time.

# This query fetches 30 items, offset by 30. (assuming more than 30 entries are found by this query)
Item.find_by_solr('name:ab*', :limit => 30, :offset => 30)

Upvotes: 0

Cody Caughlan
Cody Caughlan

Reputation: 32758

as @mausch said Solr (and by extension acts_as_solr) defaults to 10 results. You can use the :limit option to increase this, but it only takes a Fixnum, not the :all symbol. So specify :limit with a Fixnum.

Upvotes: 1

Gene T
Gene T

Reputation: 5176

I have in my sketchy notes that you can modify parser_methods.rb, around l. 75 to return only AR ID's, not the objects themselves. Worth a try in large datasets.

Upvotes: 0

Mauricio Scheffer
Mauricio Scheffer

Reputation: 99750

From the Solr FAQ:

How can I get ALL the matching documents back? ... How can I return an unlimited number of rows?

This is impractical in most cases. People typically only want to do this when they know they are dealing with an index whose size guarantees the result sets will be always be small enough that they can feasibly be transmitted in a manageable amount -- but if that's the case just specify what you consider a "manageable amount" as your rows param and get the best of both worlds (all the results when your assumption is right, and a sanity cap on the result size if it turns out your assumptions are wrong)

As for specifying a limit with acts_as_solr, try something like :limit => 80

Upvotes: 1

Related Questions