我想在我的 Ruby on Rails 应用程序中进行全文搜索。有哪些选项可供选择?
我想在我的 Ruby on Rails 应用程序中进行全文搜索。有哪些选项可供选择?
acts_as_tsearch
为最近版本的PostgreSQL提供了类似内置功能的包装器。acts_as_solr
通过其搜索服务器Solr与Rails一起使用Lucene。acts_as_ferret
插件可支持Rails。
Xapian是另一个很好的选择,使用acts_as_xapian
插件可在Rails中得到支持。你可以使用 Ferret(它是用 Ruby 编写的 Lucene)。它使用 acts_as_ferret mixin 与 Rails 无缝集成。请查看 "如何将 Ferret 与 Rails 集成"。另一种选择是 Sphinx。
MATCH() AGAINST()
。如果您只是想在表中的几个文本列中进行快速搜索,可以简单地使用这些列的全文索引,并在查询中使用MATCH() AGAINST()
。
Create the full text index in a migration file:
add_index :table, :column, type: :fulltext
Query using that index:
where( "MATCH( column ) AGAINST( ? )", term )
如果你正在寻找一个全面的搜索索引解决方案,可以让你在任何记录中搜索任何列,同时还能保持极快的速度,请看一下ElasticSearch和Searchkick。
ElasticSearch是索引和搜索引擎。
Searchkick是与Rails集成的库,使索引记录和搜索变得非常容易。
Searchkick的README很好地解释了如何启动和微调设置,但这里是一个小片段:
Install and start ElasticSearch.
brew install elasticsearch
brew services start elasticsearch
Add searchkick
gem to your bundle:
bundle add searchkick --strict
The --strict
option just tells Bundler to use an exact version in your Gemfile, which I highly recommend.
Add searchkick
to a model you want to index:
class MyModel < ApplicationRecord
searchkick
end
Index your records.
MyModel.reindex
Search your index.
matching_records = MyModel.search( "term" )
仅供参考:Ultra Sphinx已不再维护。Thinking Sphinx是它的替代品。虽然目前它缺少一些像Ultra Sphinx那样的摘录功能,但在其他方面弥补了这一点。
我一直在编译另一个问题中各种Ruby on Rails搜索选项的列表。 我不确定如何或是否合并我们的问题。