Permalink
Browse files

Add support for specifying a custom unaccent function

This way, you can use :ignore => [:accents] and index the resulting
expression. This is an issue because PostgreSQL (as of 9.1) does not
mark its included unaccent() function as IMMUTABLE, which makes it
ineligible for being indexed.
  • Loading branch information...
1 parent 48c27f8 commit b06846465d3b5dc4b8218fa21efda8c22e2f33cd Daniel Fox & Grant Hutchins committed Sep 12, 2012
Showing with 19 additions and 1 deletion.
  1. +4 −0 README.rdoc
  2. +3 −0 lib/pg_search.rb
  3. +1 −1 lib/pg_search/normalizer.rb
  4. +11 −0 spec/pg_search/normalizer_spec.rb
View
4 README.rdoc
@@ -495,6 +495,10 @@ Ignoring accents uses the {unaccent contrib package}[http://www.postgresql.org/d
SpanishQuestion.gringo_search("Que") # => [what]
SpanishQuestion.gringo_search("Cüåñtô") # => [how_many]
+Advanced users may wish to add indexes for the expressions that pg_search generates. Unfortunately, the unaccent function supplied by this contrib package is not indexable (as of PostgreSQL 9.1). Thus, you may want to write your own wrapper function and use it instead. This can be configured by calling the following code, perhaps in an initializer.
+
+ PgSearch.unaccent_function = "my_unaccent"
+
=== Using tsvector columns
PostgreSQL allows you the ability to search against a column with type tsvector instead of using an expression; this speeds up searching dramatically as it offloads creation of the tsvector that the tsquery is evaluated against.
View
3 lib/pg_search.rb
@@ -18,6 +18,9 @@ module PgSearch
mattr_accessor :multisearch_options
self.multisearch_options = {}
+ mattr_accessor :unaccent_function
+ self.unaccent_function = "unaccent"
+
module ClassMethods
def pg_search_scope(name, options)
scope = PgSearch::Scope.new(name, self, options)
View
2 lib/pg_search/normalizer.rb
@@ -12,7 +12,7 @@ def add_normalization(sql_expression)
#{config.inspect}
MESSAGE
else
- "unaccent(#{sql_expression})"
+ "#{PgSearch.unaccent_function}(#{sql_expression})"
end
else
sql_expression
View
11 spec/pg_search/normalizer_spec.rb
@@ -10,6 +10,17 @@
normalizer = PgSearch::Normalizer.new(config)
normalizer.add_normalization("foo").should == "unaccent(foo)"
end
+
+ context "when a custom unaccent function is specified" do
+ it "wraps the expression in that function" do
+ PgSearch.stub(:unaccent_function).and_return("my_unaccent")
+
+ config = stub("config", ignore: [:accents], postgresql_version: 90000)
+
+ normalizer = PgSearch::Normalizer.new(config)
+ normalizer.add_normalization("foo").should == "my_unaccent(foo)"
+ end
+ end
end
context "when config[:ignore] does not include :accents" do

0 comments on commit b068464

Please sign in to comment.