• Stars
    star
    163
  • Rank 231,141 (Top 5 %)
  • Language
    Ruby
  • Created about 15 years ago
  • Updated over 14 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Full text search with any type of class or data store using Redis

Redis::TextSearch - Use Redis to perform text search from any type of class

This gem implements an extremely fast text search using Redis, based on the patterns from James Gray’s lists and sets in Redis post as well as Antirez’s text search gist. You can use it with any type of class, whether it be ActiveRecord, DataMapper, MongoRecord, or even a class having nothing to do with an ORM.

This is not intended to be the most full-featured text search available. For that, look into Sphinx, Solr, or other alternatives. This gem is designed to (a) be extremely fast and (b) handle any (or multiple) data stores.

The only requirement this gem has is that your class must provide an id instance method which returns the ID for that instance. ActiveRecord, DataMapper, and MongoRecord all have id methods which are known to be suitable. Since “ID” can be any data type, you can even write an id method of your own that just returns a string, or an MD5 of a filename, or something else unique.

Installation

gem install redis-textsearch

Initialization

If you’re using Rails, config/initializers/redis.rb is a good place for this:

require 'redis'
require 'redis/text_search'
Redis::TextSearch.redis = Redis.new(:host => 'localhost', :port => 6379)

Example

Model class:

class Post < ActiveRecord::Base
  include Redis::TextSearch

  text_index :title, :minlength => 2
  text_index :tags, :exact => true
  text_index :description, :full => true  # allow full-phrase "Search With Spaces"

  # Using AR callback (you can call update_text_indexes anywhere on your instance)
  after_save do |r|
    r.update_text_indexes
  end
  after_destroy do |r|
    r.delete_text_indexes
  end
end

Create posts:

Post.create(:title => "All About Bacon", :tags => "chef nontechnical")
Post.create(:title => "All About Bacon - Part 2", :tags => "chef nontechnical")
Post.create(:title => "Homemade Belgian Waffles", :tags => "chef nontechnical")
Post.create(:title => "Using Redis with Ruby", :tags => "technical ruby redis")
Post.create(:title => "Installing Redis on Linux", :tags => "technical redis linux")
Post.create(:title => "Chef Deployment Recipes", :tags => "ruby howto chef")

Then search for them:

@posts = Post.text_search('bacon')   # 2 results
@posts = Post.text_search('chef')    # 4 results (tags and title)
@posts = Post.text_search('technical', 'ruby')   # AND search (1 result)
@posts = Post.text_search('chef', :fields => :tags)   # 3 results (only search tags)

You can pass options through to the find method:

@posts = Post.text_search('redis', :order => 'updated_at desc')
@posts = Post.text_search('redis', :select => 'id,title', :limit => 50)

And do pagination (adapted from will_paginate):

@posts = Post.text_search('redis', :page => 1, :per_page => 10)
@posts = Post.text_search('redis', :page => 1)  # uses class.per_page like will_paginate

You can also specify specific fields to search as a hash:

@posts = Post.text_search(:tags => 'chef')  # 4 results
@posts = Post.text_search(:tags => ['technical','ruby'])  # AND (1 result)
@posts = Post.text_search(:tags => 'redis', :title => 'linux')  # 1 result
@posts = Post.text_search(:title => 'chef') # 1 result

Note that if you need to pass options to find AND search specific fields, the first hash must be in brackets:

@posts = Post.text_search({:tags => 'chef', :title => 'deployment'},
                          :order => 'updated_at desc')

Author

Copyright © 2009-2010 Nate Wiger. All Rights Reserved. Released under the Artistic License.

More Repositories

1

redis-objects

Map Redis types directly to Ruby objects
Ruby
2,090
star
2

fast-aes

Simple but LOW security AES EBC implementation for Ruby
C
53
star
3

sinatra-resources

Simple nested resources to DRY up Sinatra code
Ruby
27
star
4

sinatra-accept-params

Parameter whitelisting with defaults and type casting for Sinatra
Ruby
18
star
5

sql_session_store

Git(hub) mirror of Dr.-Ing. Stefan Kaes' SqlSessionStore plugin. Copyright (c) Dr.-Ing. Stefan Kaes
Ruby
12
star
6

launch-daemon

C program to properly daemonize naughty programs
C
11
star
7

sinatra_from_rails

Rails plugin that provides rake tasks to generate a Sinatra app from your existing Rails app
Ruby
8
star
8

sinatra-controller

Lightweight, fast controller implementation for Sinatra
Ruby
7
star
9

icon_links

Rails view helpers to change boring text links to sexy icon_to links
Ruby
4
star
10

sinatra-hashfix

Sinatra::Hashfix - Use HashWithIndifferentAccess for Sinatra params
Ruby
4
star
11

mongo-activerecord-ruby

Enhanced MongoRecord adding templates and dynamic fields
Ruby
2
star
12

cpp

cpp
C++
2
star
13

auto_migrations_by_default

Replace standard Rails migrations with auto_migrations
Ruby
2
star
14

accept_params

Rails parameter whitelisting for controllers to increase security
Ruby
2
star
15

simple_search_like

Easy ActiveRecord searching with hashes
Ruby
1
star
16

exception_handler

ActionController plugin that handles Ruby exceptions in useful ways for APIs (xml/json/etc)
Ruby
1
star
17

sequel_auto_migrations

Auto migrations and DB upgrades for the Sequel ORM
Ruby
1
star
18

table_builder

Rails view helper to build tables
Ruby
1
star
19

dotfiles

Tilde
Shell
1
star
20

to_mime_type_options

Rails plugin to enable ActiveRecord models to set options to to_xml, to_json, etc
Ruby
1
star