我在MAC上有一个带有Postgresql(PG_search)的Rails4应用程序。我通过rails migration创建了同义词词典。同义词词典有点工作。设置如下
迁移
CREATE TEXT SEARCH DICTIONARY custom_synonyms_for_keywords ( TEMPLATE = synonym, SYNONYMS = synonyms_for_keywords );
CREATE TEXT SEARCH CONFIGURATION simple_syns_keywords (copy=simple);
ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords ALTER MAPPING FOR asciiword WITH custom_synonyms_for_keywords;
my_synonyms文件:
fshk bim24os
冰箱冰箱
postgres pgsql
postgresql pgsql
指数指数*
控制器
class Product < ActiveRecord::Base
pg_search_scope :pg_search, against: :sku,
using: {
tsearch: { prefix: true, dictionary: "simple_syns_keywords" }
}
end
当我开始
Product.pg_search("fshk") # returns record of "bim24os"
Product.pg_search("fridge") # returns no record of "refrigerator"
但是当我运行它时,它会返回正确的值
SELECT ts_lexize('custom_synonyms_for_keywords', 'fshk'); # returns "bim24os"
SELECT ts_lexize('custom_synonyms_for_keywords', 'fridge'); # returns "refrigerator"
结果
Product.pg_search("fshk")
Product Load (3.0ms) SELECT "products".*, ((ts_rank((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))), (to_tsquery('simple_syns_keywords', ''' ' || 'fshk' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "products" WHERE (((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))) @@ (to_tsquery('simple_syns_keywords', ''' ' || 'fshk' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "products"."id" ASC
=> #<ActiveRecord::Relation [#Product id: 1, sku: "BIM24OS"]
Product.pg_search("fridge")
Product Load (1.8ms) SELECT "products".*, ((ts_rank((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))), (to_tsquery('simple_syns_keywords', ''' ' || 'fridge' || ' ''' || ':*')), 0))) AS pg_search_rank FROM "products" WHERE (((to_tsvector('simple_syns_keywords', coalesce("products"."sku"::text, ''))) @@ (to_tsquery('simple_syns_keywords', ''' ' || 'fridge' || ' ''' || ':*')))) ORDER BY pg_search_rank DESC, "products"."id" ASC
=> ActiveRecord::Relation []
我错过了一步,因为我不确定为什么它不起作用。 任何帮助表示赞赏。
由于
答案 0 :(得分:0)
通过在我的SQL迁移的第三行添加“simple”来实现它。
发件人强>
ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords
ALTER MAPPING FOR asciiword
WITH custom_synonyms_for_keywords;
致
ALTER TEXT SEARCH CONFIGURATION simple_syns_keywords
ALTER MAPPING FOR asciiword
WITH custom_synonyms_for_keywords, simple;