From aa7d1c2fbebfe960e6e7a5d523e722c0b5e8a629 Mon Sep 17 00:00:00 2001 From: Anton Batenev Date: Tue, 23 Jul 2013 03:07:59 +0400 Subject: [PATCH] Fix locale dependend stemmer for FTS Fix locale dependend stemmer for FTS to improve search relevance on non English languages. --- app/models/search_observer.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/search_observer.rb b/app/models/search_observer.rb index 750a0dd0d6f..abc7d03f696 100644 --- a/app/models/search_observer.rb +++ b/app/models/search_observer.rb @@ -1,3 +1,5 @@ +require_dependency 'search' + class SearchObserver < ActiveRecord::Observer observe :topic, :post, :user, :category @@ -9,11 +11,14 @@ class SearchObserver < ActiveRecord::Observer table_name = "#{table}_search_data" foreign_key = "#{table}_id" + # for user login and name use "simple" lowercase stemmer + stemmer = table == "user" ? "simple" : Search.long_locale + # Would be nice to use AR here but not sure how to execut Postgres functions # when inserting data like this. - rows = Post.exec_sql_row_count("UPDATE #{table_name} SET search_data = TO_TSVECTOR('english', ?) WHERE #{foreign_key} = ?", search_data, id) + rows = Post.exec_sql_row_count("UPDATE #{table_name} SET search_data = TO_TSVECTOR('#{stemmer}', ?) WHERE #{foreign_key} = ?", search_data, id) if rows == 0 - Post.exec_sql("INSERT INTO #{table_name} (#{foreign_key}, search_data) VALUES (?, TO_TSVECTOR('english', ?))", id, search_data) + Post.exec_sql("INSERT INTO #{table_name} (#{foreign_key}, search_data) VALUES (?, TO_TSVECTOR('#{stemmer}', ?))", id, search_data) end rescue # don't allow concurrency to mess up saving a post