I’ve given this some thought, and I’m not sure of the best way to
achieve it
at the moment without intersection support. I had been kind of
procrastinating doing something like this until there was some demand -
and
you are the first person to request it.
A very basic (and not very efficient) implementation would be to iterate
over the terms storing the results in an array and using ruby’s
intersection
operator (&) to find the set of matches for all terms.
I’ll noodle on this a bit - and if you have other ideas, please feel
free to
email or post them.
A very basic (and not very efficient) implementation would be to iterate
over the terms storing the results in an array and using ruby’s
intersection operator (&) to find the set of matches for all terms.
If you can see past the novice coding, then the following seems to work.
As you say though, there are almost certainly more graceful ways of
doing it.
def IndexableRecord.search(phrase)
results = []
terms = Tokenizer.tokenize(phrase).collect do |t| t.stem end #terms.empty? ? [] : Context.find(:all, :include => :terms,
# :conditions => [“term IN (?)”, terms])
if terms.empty?
return results
else
first_term = true
holder =[]
terms.each do |t|
@query = "Select contexts.id from contexts, contexts_terms,
terms where
terms.term = ‘#{t}’ and terms.id =
contexts_terms.term_id and
contexts_terms.context_id = contexts.id"
if first_term
first_results = Context.find_by_sql(@query)
first_term = false
holder = first_results
else
next_results = Context.find_by_sql(@query)
holder = holder & next_results
end
if holder.empty?
return results
end
end
holder.each do |h|
results << Context.find(h.id)
end
return results
end
I’ve made modifications to the code so that you may now supply a :type
option to the search
method, as in IndexableRecord::search(“foo bar”, :type => :all). The
:type
can option can be either :any or :all with the default being :any. When
set
to :any, results containing any of the terms in the search phrase are
returned. When set to :all results containing all of the terms in the
search
phrase are returned.
This is in version 0.2.1
The implementation for the :all option looks like this: