Lance,
> 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
end
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
engine-users mailing list
[email protected]
http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org