views:

51

answers:

2

I use the Sunspot gem in my RoR app to do searches on the Post model, and it works great for that. However, I need to use it's matching algorithm against a single post object.

For example, I can search all Posts like this:

Sunspot.search Post do
  ...
end

But, I need to do the search against a single post object, like so:

Sunspot.search @post do
  ...
end

Is this possible?

I want to use the same matching algorithm on a single post object to check whether it matches or it doesn't.

A: 

I don't think that's possible. It's Solr and Lucene, not Sunspot, who have all the algorithms that determine if something is a match or not for any given query. Solr clients merely construct the query parameters and feed them to Solr, then parse back Solr results.

Mauricio Scheffer
I figured as much but was hoping for something hidden away in the API I could use. Oh well.
Jey Balachandran
A: 

I'm not exactly sure how to do this with sunspot, but one thing you could try is to query RSolr directly, pass in the ID of the model you want to check in the :fq, and see if it returns a result or not. Should return pretty fast because of the filter query:

solr = RSolr.connect(:url => Sunspot.session.config.solr.url)
solr.select :q => solr_query, :fq => ['type:Post', "id:#{@post.id}"]
njorden