uses tsvector column to search
This commit is contained in:
@@ -28,6 +28,7 @@ class Proposal < ActiveRecord::Base
|
||||
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
|
||||
|
||||
before_validation :set_responsible_name
|
||||
before_validation :calculate_tsvector
|
||||
|
||||
before_save :calculate_hot_score, :calculate_confidence_score
|
||||
|
||||
@@ -50,13 +51,26 @@ class Proposal < ActiveRecord::Base
|
||||
tags: :name
|
||||
},
|
||||
using: {
|
||||
tsearch: { dictionary: "spanish" },
|
||||
tsearch: { dictionary: "spanish", tsvector_column: 'tsv' },
|
||||
trigram: { threshold: 0.1 },
|
||||
},
|
||||
ranked_by: '(:tsearch + proposals.cached_votes_up)',
|
||||
order_within_rank: "proposals.created_at DESC"
|
||||
}
|
||||
|
||||
#remember to add tags.name
|
||||
def searchable_fields
|
||||
%w(title question summary description)
|
||||
end
|
||||
|
||||
def searchable_fields_to_sql
|
||||
searchable_fields.collect { |field| "coalesce(#{field},'')" }.join(" || ' ' || ")
|
||||
end
|
||||
|
||||
def calculate_tsvector
|
||||
ActiveRecord::Base.connection.execute("UPDATE proposals SET tsv = (to_tsvector('spanish', #{searchable_fields_to_sql})) WHERE id = #{self.id}")
|
||||
end
|
||||
|
||||
def description
|
||||
super.try :html_safe
|
||||
end
|
||||
|
||||
13
db/migrate/20151111202657_adds_tsvector_update_trigger.rb
Normal file
13
db/migrate/20151111202657_adds_tsvector_update_trigger.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class AddsTsvectorUpdateTrigger < ActiveRecord::Migration
|
||||
|
||||
def up
|
||||
add_column :proposals, :tsv, :tsvector
|
||||
add_index :products, :tsv, using: "gin"
|
||||
end
|
||||
|
||||
def down
|
||||
remove_index :products, :tsv
|
||||
remove_column :proposals, :tsv
|
||||
end
|
||||
|
||||
end
|
||||
@@ -11,7 +11,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20151103194329) do
|
||||
ActiveRecord::Schema.define(version: 20151111202657) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -215,6 +215,7 @@ ActiveRecord::Schema.define(version: 20151103194329) do
|
||||
t.text "summary"
|
||||
t.string "video_url"
|
||||
t.integer "physical_votes", default: 0
|
||||
t.tsvector "tsv"
|
||||
end
|
||||
|
||||
add_index "proposals", ["author_id", "hidden_at"], name: "index_proposals_on_author_id_and_hidden_at", using: :btree
|
||||
@@ -227,6 +228,7 @@ ActiveRecord::Schema.define(version: 20151103194329) do
|
||||
add_index "proposals", ["question"], name: "index_proposals_on_question", using: :btree
|
||||
add_index "proposals", ["summary"], name: "index_proposals_on_summary", using: :btree
|
||||
add_index "proposals", ["title"], name: "index_proposals_on_title", using: :btree
|
||||
add_index "proposals", ["tsv"], name: "index_proposals_on_tsv", using: :gin
|
||||
|
||||
create_table "settings", force: :cascade do |t|
|
||||
t.string "key"
|
||||
|
||||
Reference in New Issue
Block a user