简体   繁体   English

使用pg_search与公寓gem

[英]using pg_search with apartment gem

I'm trying to use the pg_search gem in combination with a multi tenancy application enabled by the apartment gem. 我正在尝试将pg_search gem与由公寓gem启用的多租户应用程序结合使用。 Apartment seperates my data with a schema per tenant. Apartment用每个租户的模式分隔我的数据。 Searching withing each tenant works well with the pg_search default settings, but I'm having trouble using it with pg_trgm enabled. 与每个租户一起搜索时,使用pg_search的默认设置效果很好,但是我在启用pg_trgm的情况下无法使用它。

I have enabled the pg_trgm extension on my database by adding a seperate schema called 'shared extensions' and enabling the pg_trgm extension on that. 我通过在数据库中添加了一个名为“共享扩展”的独立模式并在其上启用pg_trgm扩展来启用了pg_trgm扩展。 This schema is always included in the search path by configuring apartment: 通过配置单元,此架构始终包含在搜索路径中:

config.persistent_schemas = %w{ shared_extensions }

But when I try to do a trigram search for a model I get an error. 但是,当我尝试对模型进行Trigram搜索时,出现错误。

pg_search_scope :search_by_name, against: :name, using: :trigram

Meeting.search_by_name('blabla').first

PG::UndefinedFunction: ERROR:  operator does not exist: text % unknown

This makes me think pg_trgm has not been enabled correctly, however I can execute the following query on my database just fine: 这使我认为pg_trgm没有正确启用,但是我可以在数据库上执行以下查询:

SELECT name, similarity(name, 'blabla') AS sml
FROM aa.meetings
WHERE name % 'blabla'
ORDER BY sml DESC, name;

Any help would be appreciated! 任何帮助,将不胜感激! Thanks 谢谢

I found out what was wrong. 我发现了问题所在。 Perhaps it can someday help someone. 也许有一天它可以帮助某人。

While you can have many schemas for a Postgres database, you can only enable extensions on one of the schemas. 尽管您可以为Postgres数据库设置许多架构,但只能在其中一种架构上启用扩展。 I already had pg_trgm enabled for a schema that was not included in every search path, therefore installing the extension for the shared_extensions schema failed. 我已经为未包含在每个搜索路径中的架构启用了pg_trgm,因此为shared_extensions架构安装扩展名失败。

I changed my rake task to this: 我将耙任务更改为:

namespace :db do

desc 'Create shared_extensions Schema'
  task :extensions => :environment  do
    ActiveRecord::Base.connection.execute 'DROP EXTENSION IF EXISTS "pg_trgm"'
    ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;'
    ActiveRecord::Base.connection.execute 'CREATE EXTENSION "pg_trgm" SCHEMA shared_extensions;'
  end
end

Rake::Task["db:create"].enhance do
  Rake::Task["db:extensions"].invoke
end

Rake::Task["db:test:purge"].enhance do
  Rake::Task["db:extensions"].invoke
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM