Skip to content

Commit

Permalink
Add support for specifying a custom unaccent function
Browse files Browse the repository at this point in the history
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
Daniel Fox & Grant Hutchins committed Sep 12, 2012
1 parent 48c27f8 commit b068464
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_search/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions spec/pg_search/normalizer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b068464

Please sign in to comment.