diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6ea3e7f..d962e60 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -165,7 +165,7 @@ class ApplicationController < ActionController::Base def fallback_find_by_contents(asset, scope, query, paginate_options, options) return {:results => scope.paginate(paginate_options)} if query.blank? - {:results => scope.like_search(conditions).paginate(paginate_options)} + {:results => scope.like_search(query).paginate(paginate_options)} end end diff --git a/plugins/pg_search/db/migrate/20130320010051_create_indexes_for_search.rb b/plugins/pg_search/db/migrate/20130320010051_create_indexes_for_search.rb deleted file mode 100644 index 48a61d2..0000000 --- a/plugins/pg_search/db/migrate/20130320010051_create_indexes_for_search.rb +++ /dev/null @@ -1,19 +0,0 @@ -class CreateIndexesForSearch < ActiveRecord::Migration - def self.up - searchables = %w[ article comment qualifier national_region certifier profile license scrap category ] - klasses = searchables.map {|searchable| searchable.camelize.constantize } - klasses.each do |klass| - klass::SEARCHABLE_FIELDS.keys.each do |field| - execute "create index pg_search_plugin_#{klass.name.singularize.downcase}_#{field} on #{klass.table_name} using gin(to_tsvector('simple', #{field}))" - end - end - end - - def self.down - klasses.each do |klass| - klass::SEARCHABLE_FIELDS.keys.each do |field| - execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}_#{field}" - end - end - end -end diff --git a/plugins/pg_search/db/migrate/20130320010053_create_indexes_for_search.rb b/plugins/pg_search/db/migrate/20130320010053_create_indexes_for_search.rb new file mode 100644 index 0000000..ea5e619 --- /dev/null +++ b/plugins/pg_search/db/migrate/20130320010053_create_indexes_for_search.rb @@ -0,0 +1,19 @@ +class CreateIndexesForSearch < ActiveRecord::Migration + def self.up + searchables = %w[ article comment qualifier national_region certifier profile license scrap category ] + klasses = searchables.map {|searchable| searchable.camelize.constantize } + klasses.each do |klass| + klass::SEARCHABLE_FIELDS.keys.each do |field| + execute "create index pg_search_plugin_#{klass.name.singularize.downcase}_#{field} on #{klass.table_name} using gin(to_tsvector('simple', \"#{klass.table_name}\".#{field}))" + end + end + end + + def self.down + klasses.each do |klass| + klass::SEARCHABLE_FIELDS.keys.each do |field| + execute "drop index pg_search_plugin_#{klass.name.singularize.downcase}_#{field}" + end + end + end +end diff --git a/plugins/pg_search/lib/ext/active_record.rb b/plugins/pg_search/lib/ext/active_record.rb index 67e7ba3..1c7b2ff 100644 --- a/plugins/pg_search/lib/ext/active_record.rb +++ b/plugins/pg_search/lib/ext/active_record.rb @@ -3,7 +3,7 @@ require_dependency 'active_record' class ActiveRecord::Base def self.pg_search_plugin_search(query) if defined?(self::SEARCHABLE_FIELDS) - conditions = self::SEARCHABLE_FIELDS.map {|field, weight| "to_tsvector('simple', #{field}) @@ '#{query}'"}.join(' OR ') + conditions = self::SEARCHABLE_FIELDS.map {|field, weight| "to_tsvector('simple', \"#{self.table_name}\".#{field}) @@ '#{query}'"}.join(' OR ') where(conditions) else raise "No searchable fields defined for #{self.name}" diff --git a/plugins/pg_search/lib/pg_search_plugin.rb b/plugins/pg_search/lib/pg_search_plugin.rb index 13f2f52..36361bd 100644 --- a/plugins/pg_search/lib/pg_search_plugin.rb +++ b/plugins/pg_search/lib/pg_search_plugin.rb @@ -11,6 +11,7 @@ class PgSearchPlugin < Noosfero::Plugin end def find_by_contents(asset, scope, query, paginate_options={}, options={}) + return if query.blank? {:results => scope.pg_search_plugin_search(query).paginate(paginate_options)} end diff --git a/plugins/pg_search/test/integration/performance_test.rb b/plugins/pg_search/test/integration/performance_test.rb deleted file mode 100644 index 9f93ef9..0000000 --- a/plugins/pg_search/test/integration/performance_test.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'test_helper' -require 'benchmark' - -class PerformanceTest < ActionController::IntegrationTest - - searchables = %w[ article comment qualifier national_region certifier profile license scrap category ] - quantities = [10, 100, 1000] - - searchables.each do |searchable| - self.send(:define_method, "test_#{searchable}_performance") do - klass = searchable.camelize.constantize - asset = searchable.pluralize.to_sym - quantities.each do |quantity| - create(klass, quantity) - get 'index' - like = Benchmark.measure { 10.times { @controller.send(:find_by_contents, asset, klass, searchable) } } - puts "Like for #{quantity}: #{like}" - Environment.default.enable_plugin(PgSearchPlugin) - get 'index' - like = Benchmark.measure { 10.times { @controller.send(:find_by_contents, asset, klass, searchable) } } - puts "Pg for #{quantity}: #{pg}" - end - end - end - - private - - def create(klass, quantity) - klass.destroy_all - quantity.times.each {fast_create(klass)} - end -end -- libgit2 0.21.2