百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

fuzzily

> 编程语言
开源

针对 Rails 的快速模糊字符串搜索/匹配

160 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

针对 Rails 的快速模糊字符串搜索/匹配

Fuzzily - fuzzy string matching for ActiveRecord

Show me photos of Marakech !

Here aresome photos of Marrakesh, Morroco. Did you mean Martanesh, Albania, Marakkanam, India, or Marasheshty, Romania?

Fuzzily finds misspelled, prefix, or partial needles in a haystack of strings. It's a fast, trigram-based, database-backed fuzzy string search/match engine for Rails. Loosely inspired from an old blog post.

Tested with ActiveRecord (2.3, 3.0, 3.1, 3.2, 4.0) on various Rubies (1.8.7, 1.9.3, 2.0.0, 2.1.0) and the most common adapters (SQLite3, MySQL, and PostgreSQL).

If your dateset is big, if you need yet more speed, or do not use ActiveRecord, check out blurrily, another gem (backed with a C extension) with the same intent.

Installation

Add this line to your application's Gemfile:

gem 'fuzzily'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fuzzily

Usage

You'll need to setup 2 things:

  • a trigram model (your search index) and its migration
  • the model you want to search for

Create an ActiveRecord model in your app (this will be used to store a "fuzzy index" of all the models and fields you will be indexing):

ruby
class Trigram  10)
# => records

You can force an update on a specific record with

ruby
MyStuff.find(123).update_fuzzy_name!

Indexing more than one field

Just list all the field you want to index, or call fuzzily_searchable more than once:

ruby
class MyStuff  'CustomTrigram'
end

Speeding things up

For large data sets (millions of rows to index), the "compatible" storage used by default will typically no longer be enough to keep the index small enough.

Users have reported major improvements (2 order of magnitude) when turning the owner_type and fuzzy_field columns of the trigrams table from VARCHAR (the default) into ENUM. This is particularly efficient with MySQL and pgSQL.

This is not the default in the gem as ActiveRecord does not suport ENUM columns in any version.

UUID's

When using Rails 4 with UUID's, you will need to change the owner_id column type to UUID.

ruby
class AddTrigramsModel < ActiveRecord::Migration
  extend Fuzzily::Migration
  trigrams_owner_id_column_type = :uuid
end

Model primary key (id) is VARCHAR

If you set your Model primary key (id) AS VARCHAR instead of INT, you will need to change the owner_id column type from INT to VARCHAR in the trigrams table.

Searching virtual attributes

Your searchable fields do not have to be stored, they can be dynamic methods too. Just remember to add a virtual change method as well. For instance, if you model has first_name and last_name attributes, and you want to index a compound name dynamic attribute:

ruby
class Employee < ActiveRecord::Base
  fuzzily_searchable :name
  def name
    "#{first_name} #{last_name}"
  end

  def name_changed?
    first_name_changed? || last_name_changed?
  end
end

Update Trigram index using sidekiq-delay

For larger text, it takes time to build the index. Thus it can be moved into delay task using sidekiq + sidekiq-delay or delayed_job gem, both of them provide the method delay to move the execution to background thread by adding option async:

ruby
class Employee < ActiveRecord::Base
  fuzzily_searchable :name, async: true

end

License

MIT licence. Quite permissive if you ask me.

Copyright (c) 2013 HouseTrip Ltd.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Thanks to @bclennox, @fdegiuli, @nickbender, @Shanison, @rickbutton for pointing out and/or helping on various issues.

Issues· 19 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言