我试图在我的Rails 4 App中实现the textacular gem。基本和高级搜索工作正常,但当我尝试实现模糊搜索时,我遇到了一些麻烦。
最后,#fuzzy_search方法允许你使用Postgres的trigram 搜索功能。
为了使用它,您需要确保您的数据库具有 安装了pg_trgm模块。在您的开发机器上,您可以要求 发表文字/任务并运行
rake textacular:install_trigram
根据您的生产环境,您可以使用 rake任务,或者您可能必须手动运行命令。对于Postgres 9.1及以上,你想要运行
CREATE EXTENSION 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及以上之后,我尝试了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。有什么想法吗?