Make search cache order aware & accept values instead of column names
This commit is contained in:
@@ -7,19 +7,20 @@ module SearchCache
|
||||
|
||||
def calculate_tsvector
|
||||
ActiveRecord::Base.connection.execute("
|
||||
UPDATE proposals SET tsv = (to_tsvector('spanish', #{fields_to_sql})) WHERE id = #{self.id}")
|
||||
UPDATE proposals SET tsv = (#{searchable_values_sql}) WHERE id = #{self.id}")
|
||||
end
|
||||
|
||||
def fields_to_sql
|
||||
fields.collect { |field| "coalesce(#{field},'')" }.join(" || ' ' || ")
|
||||
private
|
||||
|
||||
def searchable_values_sql
|
||||
cx = ActiveRecord::Base.connection
|
||||
arr = []
|
||||
searchable_values.each do |val, weight|
|
||||
if val.present?
|
||||
arr << "setweight(to_tsvector('spanish', coalesce(#{cx.quote(val)}, '')), #{cx.quote(weight)})"
|
||||
end
|
||||
end
|
||||
arr.join(" || ")
|
||||
end
|
||||
|
||||
def fields
|
||||
searchable_fields << tags_to_sql
|
||||
end
|
||||
|
||||
def tags_to_sql
|
||||
"\'#{tag_list.join(' ')}\'"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -58,8 +58,15 @@ class Proposal < ActiveRecord::Base
|
||||
order_within_rank: "proposals.created_at DESC"
|
||||
}
|
||||
|
||||
def searchable_fields
|
||||
%w(title question summary description)
|
||||
def searchable_values
|
||||
values = {
|
||||
title => 'A',
|
||||
question => 'B',
|
||||
summary => 'C',
|
||||
description => 'D'
|
||||
}
|
||||
tag_list.each{ |tag| values[tag] = 'D' }
|
||||
values
|
||||
end
|
||||
|
||||
def description
|
||||
|
||||
Reference in New Issue
Block a user