Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

redis-search

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

redis-search

  • 1.0.3
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

High performance real-time prefix search, indexes store in Redis for Rails application.

中文介绍和使用说明

Master Status

Gem Version CI Status CodeCov

Features

  • Real-time search
  • High performance
  • Prefix match search
  • Support match with alias
  • Support ActiveRecord and Mongoid
  • Sort results by one field
  • Homophone search, pinyin search
  • Search as pinyin first chars
  • Conditions support

Requirements

  • Redis 2.2+

Install

gem 'redis-search'
$ 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 cleanup.
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
end

Usage

  • Bind Redis::Search callback event, it will to rebuild search indices when data create or update.
class Post < ActiveRecord::Base
  include Redis::Search

  belongs_to :user
  belongs_to :category

  redis_search 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 < ActiveRecord::Base
  include Redis::Search

  serialize :alias_names, Array

  redis_search title_field: :name,
               alias_field: :alias_names,
               score_field: :followers_count,
               ext_fields: [:email, :tagline]
end
class SearchController < ApplicationController
  # GET /search_users?q=j
  def search_users
    Post.prefix_match(params[:q], conditions: { user_id: 12, category_id: 4 })
  end
end

Index data to Redis

Specify Model

Redis-Search index data to Redis from your model (pass name as CLASS environment variable).

$ rake redis_search:index:model CLASS='User'

Customize the batch size:

$ rake redis_search:index:model CLASS='User' BATCH=100

All Models

Redis-Search all index data to Redis from app/models (or use DIR environment variabl).

$ rake redis_search:index DIR=app/models

Customize the batch size:

$ rake redis_search:index DIR=app/models BATCH=100

Documentation

Benchmark test

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

Demo

Projects used redis-search:

License

  • MIT

FAQs

Package last updated on 26 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc