Pinyin search function of gem

Pinyin search function


The use of gem:

https://github.com/huacnlee/redis-search



Method of use



Configuration file path: config/initializers/redis_search.rb

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


begin
  redis = Redis.new(:host => "127.0.0.1",:port => "6379")
  redis.select(3)


  redis = Redis::Namespace.new("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
  end
rescue
  p "redis server not running"
end



Index for the module

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


  def hits
    # To calculate the weight
  end


  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
end


# Re build index
rake redis_search:index

Search

# 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