gpt4 book ai didi

ruby-on-rails - Rails Postgresql - 同义词字典有点不更新

转载 作者:行者123 更新时间:2023-11-29 11:52:04 26 4
gpt4 key购买 nike

我在 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
索引索引*

Controller

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 []

我错过了一步,因为我不确定为什么它不起作用。任何帮助表示赞赏。

谢谢

最佳答案

通过在我的 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;

关于ruby-on-rails - Rails Postgresql - 同义词字典有点不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com