Pinyin search function of gem

Pinyin search function

The use of gem:

Method of use

Configuration file path: config/initializers/redis_search.rb

# -*- coding: utf-8 -*-
require "redis"
require "redis-namespace"
require "redis-search"

  redis = => "",:port => "6379")

  redis ="you_app_name:redis_search", :redis => redis)
  Redis::Search.configure do |config|
    config.redis = redis
    config.complete_max_length = 100
    config.pinyin_match = true
    # Whether or not to close Chinese segmentation, true is closed, can save memory usage
    config.disable_rmmseg = true
  p "redis server not running"

Index for the module

class User <ActiveRecord::Base
  include Redis::Search

  def hits
    # To calculate the weight

  redis_search_index(:title_field => :name, #The main index domain
                     :alias_field => :login,  #Secondary index domain
                     :score_field => :hits, #Introduction of a method, to determine the weights according to the return value of the method
                     :prefix_index_enable => true, #Word for word indexing, open the word search
                     :condition_fields => [:role], #Conditions, the incoming is a set of fields, search to          
                                                   :conditions => {:role => :admin}) Method of use. 
                     :ext_fields => [:email, :created_at]) #The rest of the domain index

# Re build index
rake redis_search:index


# Word search
Redis::Search.complete("User", params[:query], :conditions => {:role => :admin})

# Full text search
Redis::Search.query("User", params[:query], :conditions => {:role => :admin})

Posted by Bartholomew at January 09, 2014 - 8:31 AM