Skip to content

Instantly share code, notes, and snippets.

@tfl
Created July 2, 2011 16:11
Show Gist options
  • Save tfl/1060997 to your computer and use it in GitHub Desktop.
Save tfl/1060997 to your computer and use it in GitHub Desktop.
A very simple Ruby class using mysql fulltext indexes
class ReThinkingSphinx
QUERIES = {
:Post => {
:default => "SELECT * FROM posts WHERE MATCH (title, body) AGAINST (?)",
:boolean => "SELECT * FROM posts WHERE MATCH (title, body) AGAINST (? IN BOOLEAN MODE)"
},
:Event => {
:default => "SELECT * FROM events WHERE MATCH (name, description) AGAINST (?)",
:boolean => "SELECT * FROM events WHERE MATCH (name, description) AGAINST (? IN BOOLEAN MODE)"
},
:Gallerie => {
:default => "SELECT * FROM galleries WHERE MATCH (title, description) AGAINST (?)",
:boolean => "SELECT * FROM galleries WHERE MATCH (title, description) AGAINST (? IN BOOLEAN MODE)"
}
}
def self.search(term)
results = []
ReThinkingSphinx::QUERIES.each do |object, query|
if term.include? '*'
rc = object.to_s.constantize.find_by_sql([query[:boolean], term])
rc.each do |r|
results << r
end
else
rc = object.to_s.constantize.find_by_sql([query[:default], term])
rc.each do |r|
results << r
end
end
end
return results
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment