Merge pull request #708 from AyuntamientoMadrid/search-performance

Mejoras en la velocidad de las búsquedas
This commit is contained in:
Raimond Garcia
2015-11-18 09:12:59 -08:00
6 changed files with 59 additions and 7 deletions

View File

@@ -0,0 +1,27 @@
module SearchCache
extend ActiveSupport::Concern
included do
after_save :calculate_tsvector
end
def calculate_tsvector
ActiveRecord::Base.connection.execute("
UPDATE proposals SET tsv = (#{searchable_values_sql}) WHERE id = #{self.id}")
end
private
def searchable_values_sql
searchable_values.collect { |value, weight| set_tsvector(value, weight) }.join(" || ")
end
def set_tsvector(value, weight)
"setweight(to_tsvector('spanish', coalesce(#{quote(value)}, '')), #{quote(weight)})"
end
def quote(value)
ActiveRecord::Base.connection.quote(value)
end
end

View File

@@ -5,6 +5,7 @@ class Proposal < ActiveRecord::Base
include Measurable
include Sanitizable
include PgSearch
include SearchCache
apply_simple_captcha
acts_as_votable
@@ -50,13 +51,24 @@ 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"
}
def searchable_values
values = {
title => 'A',
question => 'B',
summary => 'C',
description => 'D'
}
tag_list.each{ |tag| values[tag] = 'D' }
values
end
def description
super.try :html_safe
end

View File

@@ -0,0 +1,13 @@
class AddsTsvectorUpdateTrigger < ActiveRecord::Migration
def up
add_column :proposals, :tsv, :tsvector
add_index :proposals, :tsv, using: "gin"
end
def down
remove_index :proposals, :tsv
remove_column :proposals, :tsv
end
end

View File

@@ -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"
@@ -95,10 +95,10 @@ ActiveRecord::Schema.define(version: 20151103194329) do
t.string "visit_id"
t.datetime "hidden_at"
t.integer "flags_count", default: 0
t.datetime "ignored_flag_at"
t.integer "cached_votes_total", default: 0
t.integer "cached_votes_up", default: 0
t.integer "cached_votes_down", default: 0
t.datetime "ignored_flag_at"
t.integer "comments_count", default: 0
t.datetime "confirmed_hide_at"
t.integer "cached_anonymous_votes_total", default: 0
@@ -114,7 +114,6 @@ ActiveRecord::Schema.define(version: 20151103194329) do
add_index "debates", ["cached_votes_total"], name: "index_debates_on_cached_votes_total", using: :btree
add_index "debates", ["cached_votes_up"], name: "index_debates_on_cached_votes_up", using: :btree
add_index "debates", ["confidence_score"], name: "index_debates_on_confidence_score", using: :btree
add_index "debates", ["description"], name: "index_debates_on_description", using: :btree
add_index "debates", ["hidden_at"], name: "index_debates_on_hidden_at", using: :btree
add_index "debates", ["hot_score"], name: "index_debates_on_hot_score", using: :btree
add_index "debates", ["title"], name: "index_debates_on_title", using: :btree
@@ -215,18 +214,19 @@ 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
add_index "proposals", ["author_id"], name: "index_proposals_on_author_id", using: :btree
add_index "proposals", ["cached_votes_up"], name: "index_proposals_on_cached_votes_up", using: :btree
add_index "proposals", ["confidence_score"], name: "index_proposals_on_confidence_score", using: :btree
add_index "proposals", ["description"], name: "index_proposals_on_description", using: :btree
add_index "proposals", ["hidden_at"], name: "index_proposals_on_hidden_at", using: :btree
add_index "proposals", ["hot_score"], name: "index_proposals_on_hot_score", using: :btree
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"

View File

@@ -1,6 +1,6 @@
namespace :debates do
desc "Updates all debates by recalculating their hot_score"
task hot_score: :environment do
task touch: :environment do
Debate.find_in_batches do |debates|
debates.each(&:save)
end

View File

@@ -1,6 +1,6 @@
namespace :proposals do
desc "Updates all proposals by recalculating their hot_score"
task hot_score: :environment do
task touch: :environment do
Proposal.find_in_batches do |proposals|
proposals.each(&:save)
end