# Passing id array as option in find\_by\_solr

**URL:** <https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137>\
**Category:** Rails\
**Created:** [October 12, 2009, 10:42am UTC](https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137 "2009-10-12T10:42:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [October 12, 2009, 10:42am UTC](https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137/1 "2009-10-12T10:42:23Z")

</div>

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. 😉

This is an example of xml result to a query:

## 0.18781202

## Article:77
 77 

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)

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [October 13, 2009, 8:54pm UTC](https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137/2 "2009-10-13T20:54:18Z")

</div>

On Oct 12, 4:40 am, adedip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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/thread/09017243a0129678/831c2318fcc0a972](http://groups.google.com/group/rubyonrails-talk/tree/browse_frm/thread/09017243a0129678/831c2318fcc0a972)  
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 J.

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [October 15, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137/3 "2009-10-15T14:01:55Z")

</div>

On Oct 13, 8:52 pm, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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!

---

<div class="post-metadata">

**Author:** ![adedip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e495f1/32.png) [@adedip](https://www.ruby-forum.com/u/adedip)\
**Post date:** [October 15, 2009, 4:52pm UTC](https://www.ruby-forum.com/t/passing-id-array-as-option-in-find-by-solr/177137/4 "2009-10-15T16:52:16Z")

</div>

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](http://groups.google.com/group/acts_as_solr/browse_thread/thread/4657b9712402106e)  
)
