Passing id array as option in find_by_solr

Needs: I wan'to apply the solr query by keyword to a subgroups of documents.

I'm putting on the index only title and content of each, is it possibile to do something like this:

@articles = Article.find_by_solr(params[:query], {:scores => true, :id => [1,2,3]})

this above does not work, I get: "Invalid parameters: id"

it's ok if u know any other way to obtain that. :wink:

This is an example of xml result to a query:

<doc> <float name="score">0.18781202</float>

Needs: I wan'to apply the solr query by keyword to a subgroups of documents.

I'm putting on the index only title and content of each, is it possibile to do something like this:

@articles = Article.find_by_solr(params[:query], {:scores => true, :id => [1,2,3]})

this above does not work, I get: "Invalid parameters: id"

[snip]

last possibility.. i'd need to force find_by_solr to filter using "Article.id IN (?)", [1,2,3] (or a variable of array..of course)

(see this post: Embedded groups are no longer supported for more discussion)

The short answer is that you can limit the records that find_by_solr returns by calling it on a scoped instance instead of the model class. So in your example, you'd want something like:

Article.scoped(:conditions => { :id => some_array_of_ids }).find_by_solr(...whatever you usually pass...)

Note that this is not actually scoping the find_by_solr; it's just scoping the results (find_by_solr gets an array of IDs back from Solr, then does a find(:all, :conditions => { :id => results }) which gets scoped.

In your particular case (having a bunch of IDs to limit to), you could also use find_id_by_solr, like this:

solr_ids = Article.find_ids_by_solr(....) @articles = Article.find(limit_ids & solr_ids)

The other technique is much more generally applicable, whereas this ONLY works for limiting by ID.

--Matt Jones

> Needs: I wan'to apply the solr query by keyword to a subgroups of > documents.

> I'm putting on the index only title and content of each, is it > possibile to do something like this:

> @articles = Article.find_by_solr(params[:query], {:scores => true, :id > => [1,2,3]})

> this above does not work, I get: "Invalid parameters: id" [snip]

> last possibility.. i'd need to force find_by_solr to filter using > "Article.id IN (?)", [1,2,3] > (or a variable of array..of course)

(see this post:http://groups.google.com/group/rubyonrails-talk/tree/browse_frm/threa… for more discussion)

The short answer is that you can limit the records that find_by_solr returns by calling it on a scoped instance instead of the model class. So in your example, you'd want something like:

Article.scoped(:conditions => { :id => some_array_of_ids }).find_by_solr(...whatever you usually pass...)

Note that this is not actually scoping the find_by_solr; it's just scoping the results (find_by_solr gets an array of IDs back from Solr, then does a find(:all, :conditions => { :id => results }) which gets scoped.

This would be good at least..but there's a problem: I've tryed this..it comes out with a synch error. and it makes sense because it finds all the solr records in the index but less in the DB..that's not actually true..but it is for solr because its new DB is the one scoped (where most of the times occur not all the ids retrieved by solr query)

In your particular case (having a bunch of IDs to limit to), you could also use find_id_by_solr, like this:

solr_ids = Article.find_ids_by_solr(....) @articles = Article.find(limit_ids & solr_ids)

the second line is the syntax for intersection right? is find_ids_by_solr different from find_id_by_solr ?

The other technique is much more generally applicable, whereas this ONLY works for limiting by ID.

I'm afraid is the only option... All of this of course is a matter on tons of records I need to solve (into studying area) the needs is that if I have only 5 ids to look keywords into..i don't want to search all the index... and this happen anyway in the cases showned above :S

thanks a lot!

The big deal is that option only allowed RANGE and only OR chain..in fact this:

find_by_solr "query-string AND (id:(1) OR id:(42))"

work.. (so I've been told) and logically talking it does the right thing..in only search for query-string among the subset of ids.. but..is this the only way?..so ugly? the guy who tried this moreover said that this fails often in production..probably due to the high number of ids.. (ref: http://groups.google.com/group/acts_as_solr/browse_thread/thread/4657b9712402106e )