Commit f1ba1f6fd436dc7cf349014b4d2fd4c22514556e

Authored by Rodrigo Souto
1 parent 9f9de166

Fixing pg search and like search details

app/controllers/application_controller.rb
... ... @@ -165,7 +165,7 @@ class ApplicationController < ActionController::Base
165 165  
166 166 def fallback_find_by_contents(asset, scope, query, paginate_options, options)
167 167 return {:results => scope.paginate(paginate_options)} if query.blank?
168   - {:results => scope.like_search(conditions).paginate(paginate_options)}
  168 + {:results => scope.like_search(query).paginate(paginate_options)}
169 169 end
170 170  
171 171 end
... ...
plugins/pg_search/db/migrate/20130320010051_create_indexes_for_search.rb
... ... @@ -1,19 +0,0 @@
1   -class CreateIndexesForSearch < ActiveRecord::Migration
2   - def self.up
3   - searchables = %w[ article comment qualifier national_region certifier profile license scrap category ]
4   - klasses = searchables.map {|searchable| searchable.camelize.constantize }
5   - klasses.each do |klass|
6   - klass::SEARCHABLE_FIELDS.keys.each do |field|
7   - execute "create index pg_search_plugin_#{klass.name.singularize.downcase}_#{field} on #{klass.table_name} using gin(to_tsvector('simple', #{field}))"
8   - end
9   - end
10   - end
11   -
12   - def self.down
13   - klasses.each do |klass|
14   - klass::SEARCHABLE_FIELDS.keys.each do |field|
15   - execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}_#{field}"
16   - end
17   - end
18   - end
19   -end
plugins/pg_search/db/migrate/20130320010053_create_indexes_for_search.rb 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +class CreateIndexesForSearch < ActiveRecord::Migration
  2 + def self.up
  3 + searchables = %w[ article comment qualifier national_region certifier profile license scrap category ]
  4 + klasses = searchables.map {|searchable| searchable.camelize.constantize }
  5 + klasses.each do |klass|
  6 + klass::SEARCHABLE_FIELDS.keys.each do |field|
  7 + execute "create index pg_search_plugin_#{klass.name.singularize.downcase}_#{field} on #{klass.table_name} using gin(to_tsvector('simple', \"#{klass.table_name}\".#{field}))"
  8 + end
  9 + end
  10 + end
  11 +
  12 + def self.down
  13 + klasses.each do |klass|
  14 + klass::SEARCHABLE_FIELDS.keys.each do |field|
  15 + execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}_#{field}"
  16 + end
  17 + end
  18 + end
  19 +end
... ...
plugins/pg_search/lib/ext/active_record.rb
... ... @@ -3,7 +3,7 @@ require_dependency &#39;active_record&#39;
3 3 class ActiveRecord::Base
4 4 def self.pg_search_plugin_search(query)
5 5 if defined?(self::SEARCHABLE_FIELDS)
6   - conditions = self::SEARCHABLE_FIELDS.map {|field, weight| "to_tsvector('simple', #{field}) @@ '#{query}'"}.join(' OR ')
  6 + conditions = self::SEARCHABLE_FIELDS.map {|field, weight| "to_tsvector('simple', \"#{self.table_name}\".#{field}) @@ '#{query}'"}.join(' OR ')
7 7 where(conditions)
8 8 else
9 9 raise "No searchable fields defined for #{self.name}"
... ...
plugins/pg_search/lib/pg_search_plugin.rb
... ... @@ -11,6 +11,7 @@ class PgSearchPlugin &lt; Noosfero::Plugin
11 11 end
12 12  
13 13 def find_by_contents(asset, scope, query, paginate_options={}, options={})
  14 + return if query.blank?
14 15 {:results => scope.pg_search_plugin_search(query).paginate(paginate_options)}
15 16 end
16 17  
... ...
plugins/pg_search/test/integration/performance_test.rb
... ... @@ -1,32 +0,0 @@
1   -require 'test_helper'
2   -require 'benchmark'
3   -
4   -class PerformanceTest < ActionController::IntegrationTest
5   -
6   - searchables = %w[ article comment qualifier national_region certifier profile license scrap category ]
7   - quantities = [10, 100, 1000]
8   -
9   - searchables.each do |searchable|
10   - self.send(:define_method, "test_#{searchable}_performance") do
11   - klass = searchable.camelize.constantize
12   - asset = searchable.pluralize.to_sym
13   - quantities.each do |quantity|
14   - create(klass, quantity)
15   - get 'index'
16   - like = Benchmark.measure { 10.times { @controller.send(:find_by_contents, asset, klass, searchable) } }
17   - puts "Like for #{quantity}: #{like}"
18   - Environment.default.enable_plugin(PgSearchPlugin)
19   - get 'index'
20   - like = Benchmark.measure { 10.times { @controller.send(:find_by_contents, asset, klass, searchable) } }
21   - puts "Pg for #{quantity}: #{pg}"
22   - end
23   - end
24   - end
25   -
26   - private
27   -
28   - def create(klass, quantity)
29   - klass.destroy_all
30   - quantity.times.each {fast_create(klass)}
31   - end
32   -end