From 25184666a51bc21d4cd6fb010ec769a27a496480 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Wed, 11 Nov 2015 22:27:38 +0100 Subject: [PATCH] uses tsvector column to search --- app/models/proposal.rb | 16 +++++++++++++++- ...0151111202657_adds_tsvector_update_trigger.rb | 13 +++++++++++++ db/schema.rb | 4 +++- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20151111202657_adds_tsvector_update_trigger.rb diff --git a/app/models/proposal.rb b/app/models/proposal.rb index dd2560ba1..60ef20420 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -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 diff --git a/db/migrate/20151111202657_adds_tsvector_update_trigger.rb b/db/migrate/20151111202657_adds_tsvector_update_trigger.rb new file mode 100644 index 000000000..b837f7fc6 --- /dev/null +++ b/db/migrate/20151111202657_adds_tsvector_update_trigger.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 95bd4a36d..4a4df2497 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"