Skip to content

Commit

Permalink
Merge pull request #6 from dougcole/search_ordering
Browse files Browse the repository at this point in the history
add search order
  • Loading branch information
dougcole authored Apr 6, 2018
2 parents 3225ce7 + bf8396b commit c1548c3
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion app/models/contest_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ContestSearch
def self.search(query)
columns = %w(primary_office_name secondary_office_name electoral_district_name)
tsvector = to_tsvector(columns)
results = Contest.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", to_tsquery(query))
results = Contest.where("to_tsvector(#{tsvector}) @@ to_tsquery('english', ?)", to_tsquery(query)).order(case_statement)

if results.empty?
columns = %w(primary_name primary_party secondary_name)
Expand All @@ -20,4 +20,31 @@ def self.to_tsvector(columns)
def self.to_tsquery(query)
res = query.split(' ').map {|q| "#{q}:*" }.join(" & ")
end

def self.case_statement
<<-EOS
CASE electoral_district_type
WHEN 'congressional' THEN 0
WHEN 'state' THEN 1
WHEN 'state-senate' THEN 2
WHEN 'state-house' THEN 3
WHEN 'judicial' THEN 4
WHEN 'county' THEN 5
WHEN 'county-council' THEN 6
WHEN 'school' THEN 7
WHEN 'city' THEN 8
WHEN 'city-council' THEN 9
WHEN 'town' THEN 10
WHEN 'township' THEN 11
WHEN 'village' THEN 12
WHEN 'borough' THEN 13
WHEN 'utility' THEN 14
WHEN 'ward' THEN 15
WHEN 'special' THEN 16
WHEN 'water' THEN 17
WHEN 'other' THEN 18
ELSE 19
END
EOS
end
end

0 comments on commit c1548c3

Please sign in to comment.