Monday, 12 August 2013

Searching through collections of IDs using Solr

Searching through collections of IDs using Solr

I've been poking through Sunspot documentation and old SO questions, but I
can't seem to figure out the right way to do this.
I have a Rails model (Case) that has_many Tags. Using a fairly thorough
segmented control section, the user should be able to check the relevant
Tags (organized under TagCategories, but that's irrelevant here) and see
the Cases that best match the tags selected.
Here's where I'm starting to get confused: Sunspot, and by extension,
Solr, seem to have a high emphasis on searching text fields. I need to
search through an array of IDs (that's how I'm currently passing in the
selected tags, though I'm open to a different approach).
A couple things I've tried:
(in cases_controller.rb - params[:collected] is a collection of tag IDs)
def index
@search = Case.solr_search do
with(:tag_ids, params[:collected])
end
end
(in case.rb model)
searchable do
integer :tag_ids, multiple: true
end
def tag_ids
tags.map(&:id)
end
I think this was my first issue - I'm thinking that with is a way to limit
queries, not actually search.
I tried changing tag_ids to a stringified array to use fulltext (something
like ["52", "55"] -- equal to what params[:collected] would spit out) but
that didn't work either:
# controller
@search = Case.solr_search do
fulltext params[:collected]
end
# model
searchable do
text :tag_ids
end
def tag_ids
@arr = []
tags.each do |tag|
@arr << tag.id.to_s
end
@arr
end
I'm getting three outputs: a match when the exact IDs are given (a Case
with tag ids of 1 and 2 and selecting the checkboxes for Tag 1 and 2);
none when the params are even slightly different (1 and 3 vs Tag selection
of 2 and 3); and all the models when no query is given (no params checked,
such as on initial view). The third one is fine - as far as I can tell,
that's expected behavior from Sunspot.
The problem is I'm trying to implement a type of fuzzy search - I know
that Sunspot has a scoring system and so what I'm trying to do is score
the results by the number of matching Tag IDs.
If anyone can point me in the right direction, I'd really appreciate it.

No comments:

Post a Comment