Added package.text_search for full-text searching the Package model
This commit is contained in:
parent
fca503df55
commit
ee530072eb
1 changed files with 17 additions and 0 deletions
|
|
@ -1,3 +1,20 @@
|
||||||
class Package < ActiveRecord::Base
|
class Package < ActiveRecord::Base
|
||||||
has_many :screenshots, :inverse_of=>:package
|
has_many :screenshots, :inverse_of=>:package
|
||||||
|
|
||||||
|
# Use PostgreSQL's full-text search capability for the user search
|
||||||
|
def self.text_search(query)
|
||||||
|
if query.present?
|
||||||
|
# Add a rank column to each result
|
||||||
|
rank = <<-RANK
|
||||||
|
ts_rank(to_tsvector(packages.name), plainto_tsquery(#{sanitize(query)}))
|
||||||
|
+
|
||||||
|
ts_rank(to_tsvector(packages.description), plainto_tsquery(#{sanitize(query)}))
|
||||||
|
RANK
|
||||||
|
|
||||||
|
# Do the full-text search and return results by rank
|
||||||
|
where("packages.name @@ :q or packages.description @@ :q", q: query).order("#{rank} desc")
|
||||||
|
else
|
||||||
|
scoped
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue