Commit b2b95d0243b018e5df2e4a3b6ff02e5655518aac

Authored by Francisco Marcelo A. Lima Júnior
Committed by Rodrigo Souto
1 parent 560a321b

pg_search plugin : fix search with space between words

plugins/pg_search/lib/ext/active_record.rb
... ... @@ -2,8 +2,11 @@ require_dependency 'active_record'
2 2  
3 3 class ActiveRecord::Base
4 4 def self.pg_search_plugin_search(query)
  5 + query.gsub!(/\|/,' ')
  6 + formatted_query = query.split.map{|w| w += ":*"}.join('|')
  7 +
5 8 if defined?(self::SEARCHABLE_FIELDS)
6   - where("to_tsvector('simple', #{pg_search_plugin_fields}) @@ to_tsquery('#{query}:*')")
  9 + where("to_tsvector('simple', #{pg_search_plugin_fields}) @@ to_tsquery('#{formatted_query}')")
7 10 else
8 11 raise "No searchable fields defined for #{self.name}"
9 12 end
... ...
plugins/pg_search/test/unit/pg_search_plugin_test.rb
... ... @@ -14,6 +14,13 @@ class PgSearchPluginTest < ActiveSupport::TestCase
14 14 assert_includes search(Profile, 'water'), profile
15 15 end
16 16  
  17 + should 'locate one or more profiles' do
  18 + profile1 = fast_create(Profile, :identifier => 'administrator')
  19 + profile2 = fast_create(Profile, :identifier => 'debugger')
  20 + assert_includes search(Profile, 'admin deb'), profile1
  21 + assert_includes search(Profile, 'admin deb'), profile2
  22 + end
  23 +
17 24 # TODO This feature is available only on Postgresql 9.0
18 25 # http://www.postgresql.org/docs/9.0/static/unaccent.html
19 26 # should 'ignore accents' do
... ...