我试图在我的 Rails 4 应用程序中实现textacular gem 。基本和高级搜索工作正常,但是当我尝试实现模糊搜索时,我遇到了一些麻烦。
来自存储库的安装说明:
最后,#fuzzy_search 方法允许您使用 Postgres 的三元组搜索功能。
为了使用它,您需要确保您的数据库安装了 pg_trgm 模块。在您的开发机器上,您可以要求 textacular/tasks 并运行
rake textacular:install_trigram
根据您的生产环境,您可能能够使用 rake 任务,或者您可能必须手动运行命令。对于 Postgres 9.1 及更高版本,您需要运行
创建扩展 pg_trgm;
我试过的
我尝试在我的应用程序的根目录中运行rake textacular:install_trigram
并收到以下消息:bundle exec rake textacular:install_trigram
/usr/local/Cellar/postgresql/9.2.4/share/postgresql/extension/pg_trgm.control
rake aborted!
PG::InsufficientPrivilege: ERROR: permission denied to create extension "pg_trgm"
HINT: Must be superuser to create this extension.
: CREATE EXTENSION pg_trgm;
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `exec'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/postgres_module_installer.rb:54:in `install_postgres_91_module'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/postgres_module_installer.rb:7:in `install_module'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/gems/textacular-3.1.0/lib/textacular/tasks.rb:14:in `block (2 levels) in <top (required)>'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => textacular:install_trigram
基于此,我尝试sudo rake textacular:install_trigram
并得到了这个:
cannot load such file -- bundler/setup
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/config/boot.rb:4:in `<top (required)>'
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/config/application.rb:1:in `<top (required)>'
/Users/ssapra/Sameet/Programming/WebDev/Rails/myapp/Rakefile:4:in `<top (required)>'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `eval'
/Users/ssapra/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in `<main>'
(See full trace by running task with --trace)
最后,在阅读了For Postgres 9.1 and above之后,我尝试运行psql
然后CREATE EXTENSION pg_trgm;
. 没有错误(尽管我现在做的任何时候,它都会告诉我它已经存在)。所以我\quit
然后替换我的有效代码行:
@search = Content.advanced_search(params[:search].split(" ").join("|")).basic_search(params[:search])
和:
@search = Content.fuzzy_search(title: params[:search]).fuzzy_search(text: params[:search])
甚至:
@search = Content.fuzzy_search(params[:search])
我明白了:
PG::UndefinedFunction: ERROR: operator does not exist: character varying % unknown
LINE 1: ...NT(*) FROM "contents" WHERE (("contents"."title" % 'inv')) ...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "contents" WHERE (("contents"."title" % 'inv')) AND (("contents"."text" % 'inv'))
它卡在第一行:
-if @contents.empty?
%p="No results found for: #{@query}"
-elsif !@query.empty?
%p="Your search results for: #{@query}"
内容控制器
在我的contents_controller.rb
: 我有这三个补充:
require 'textacular'
require 'textacular/tasks'
ActiveRecord::Base.extend(Textacular)
我正在运行 Postgres 9.2.4。有任何想法吗?