Giter VIP home page Giter VIP logo

redis-search's Introduction

Redis-Search

High performance real-time search (Support Chinese), index in Redis for Rails application

中文介绍和使用说明

Status

CI Status

Features

  • Real-time search
  • High performance
  • Segment words search and prefix match search
  • Support match with alias
  • Support ActiveRecord and Mongoid
  • Sort results by one field
  • Homophone search, pinyin search
  • Conditions support

Requirements

  • Redis 2.2+

Install

in Rails application Gemfile

gem 'redis','>= 2.1.1'
gem 'chinese_pinyin', '0.4.1'
# add rmmseg if you need search by segment words
gem 'rmmseg-cpp-huacnlee', '0.2.9'
gem 'redis-namespace','~> 1.1.0'
gem 'redis-search', '0.8.0'

install bundlers

$ bundle install

Configure

create file in: config/initializers/redis_search.rb

require "redis"
require "redis-namespace"
require "redis-search"
# don't forget change namespace
redis = Redis.new(:host => "127.0.0.1",:port => "6379")
# We suggest you use a special db in Redis, when you need to clear all data, you can use flushdb command to clear them.
redis.select(3)
# Give a special namespace as prefix for Redis key, when your have more than one project used redis-search, this config will make them work fine.
redis = Redis::Namespace.new("your_app_name:redis_search", :redis => redis)
Redis::Search.configure do |config|
  config.redis = redis
  config.complete_max_length = 100
  config.pinyin_match = true
  # use rmmseg, true to disable it, it can save memroy
  config.disable_rmmseg = false
end

Usage

bind Redis::Search callback event, it will to rebuild search indexes when data create or update.

class Post
  include Mongoid::Document
  include Redis::Search

  field :title
  field :body
  field :hits

  belongs_to :user
  belongs_to :category

  redis_search_index(:title_field => :title,
                     :score_field => :hits,
                     :condition_fields => [:user_id, :category_id],
                     :ext_fields => [:category_name])

  def category_name
    self.category.name
  end
end

class User
  include Mongoid::Document
  include Redis::Search
  
  field :name
  field :alias_names, :type => Array
  field :tagline
  field :email
  field :followers_count
  
  redis_search_index(:title_field => :name,
	                 :alias_field => :alias_names,
                     :prefix_index_enable => true,
                     :score_field => :followers_count,
                     :ext_fields => [:email,:tagline])
end

class SearchController < ApplicationController
  # GET /searchs?q=title
  def index
    Redis::Search.query("Post", params[:q], :conditions => {:user_id => 12})
  end
  
  # GET /search_users?q=j
  def search_users
    Redis::Search.complete("Post", params[:q], :conditions => {:user_id => 12, :category_id => 4})
  end
end

Index data to Redis

If you are first install it in you old project, or your Redis cache lose, you can use this command to rebuild indexes.

$ rake redis_search:index

Documentation

see Rdoc.info redis-search

Benchmark test

You can run the rake command (see Rakefile) to make test. There is my performance test result.

Demo

You can try the search feature in zheye.org | ruby-china | shu.im

redis-search's People

Contributors

huacnlee avatar nowa avatar pmq20 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.