Module: Redis::TextSearch::ClassMethods
- Defined in:
- lib/redis/text_search.rb
Overview
These class methods are added to the class when you include Redis::TextSearch.
Instance Attribute Summary collapse
-
#redis ⇒ Object
Returns the value of attribute redis.
-
#text_index_exclude_list ⇒ Object
Words to exclude from text indexing.
-
#text_indexes ⇒ Object
readonly
Returns the value of attribute text_indexes.
Instance Method Summary collapse
-
#delete_text_indexes(id, *fields) ⇒ Object
Delete all text indexes for the given id.
-
#field_key(name, id) ⇒ Object
:nodoc:.
- #merge_text_search_conditions!(ids, options) ⇒ Object
-
#prefix ⇒ Object
:nodoc:.
-
#prefix=(prefix) ⇒ Object
Set the Redis prefix to use.
- #reverse_index_key(id, field) ⇒ Object
-
#text_filter(field) ⇒ Object
Filter and return self.
-
#text_index(*args) ⇒ Object
Define fields to be indexed for text search.
-
#text_indexes_for(id, field) ⇒ Object
:nodoc:.
-
#text_search(*args) ⇒ Object
Perform text search and return results from database.
-
#text_search_find(ids, options) ⇒ Object
This is called when the class is imported, and uses reflection to guess how to retrieve records.
- #text_search_sets_for(field, values) ⇒ Object
Instance Attribute Details
#redis ⇒ Object
Returns the value of attribute redis.
39 40 41 |
# File 'lib/redis/text_search.rb', line 39 def redis @redis end |
#text_index_exclude_list ⇒ Object
Words to exclude from text indexing. By default, includes common English prepositions like “a”, “an”, “the”, “and”, “or”, etc. This is an accessor to an array, so you can use += or << to add to it. See the constant DEFAULT_EXCLUDE_LIST
for the default list.
46 47 48 |
# File 'lib/redis/text_search.rb', line 46 def text_index_exclude_list @text_index_exclude_list end |
#text_indexes ⇒ Object (readonly)
Returns the value of attribute text_indexes.
40 41 42 |
# File 'lib/redis/text_search.rb', line 40 def text_indexes @text_indexes end |
Instance Method Details
#delete_text_indexes(id, *fields) ⇒ Object
Delete all text indexes for the given id.
186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/redis/text_search.rb', line 186 def delete_text_indexes(id, *fields) fields = @text_indexes.keys if fields.empty? fields.each do |field| indexes = text_indexes_for(id, field) redis.pipelined do indexes.each do |key| redis.srem(key, id) end # blow away the entire reverse index, because we deleted the object redis.del reverse_index_key(id, field) end end end |
#field_key(name, id) ⇒ Object
:nodoc:
58 59 60 |
# File 'lib/redis/text_search.rb', line 58 def field_key(name, id) #:nodoc: "#{prefix}:#{id}:#{name}" end |
#merge_text_search_conditions!(ids, options) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/redis/text_search.rb', line 79 def merge_text_search_conditions!(ids, ) pk = "#{table_name}.#{primary_key}" case [:conditions] when Array if [:conditions][1].is_a?(::Hash) [:conditions][0] = "(#{[:conditions][0]}) AND #{pk} IN (:text_search_ids)" [:conditions][1][:text_search_ids] = ids else [:conditions][0] = "(#{[:conditions][0]}) AND #{pk} IN (?)" [:conditions] << ids end when ::Hash if [:conditions].has_key?(primary_key.to_sym) raise BadConditions, "Cannot specify primary key (#{pk}) in :conditions to #{self.name}.text_search" end [:conditions][primary_key.to_sym] = ids when String [:conditions] = ["(#{[:conditions]}) AND #{pk} IN (?)", ids] else .merge!(:conditions => {primary_key => ids}) end end |
#prefix ⇒ Object
:nodoc:
50 51 52 53 54 55 56 |
# File 'lib/redis/text_search.rb', line 50 def prefix #:nodoc: @prefix ||= self.name.to_s. sub(%r{(.*::)}, ''). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). downcase end |
#prefix=(prefix) ⇒ Object
Set the Redis prefix to use. Defaults to model_name
49 |
# File 'lib/redis/text_search.rb', line 49 def prefix=(prefix) @prefix = prefix end |
#reverse_index_key(id, field) ⇒ Object
200 201 202 |
# File 'lib/redis/text_search.rb', line 200 def reverse_index_key(id, field) field_key("#{field}_indexes", id) end |
#text_filter(field) ⇒ Object
Filter and return self. Chainable.
181 182 183 |
# File 'lib/redis/text_search.rb', line 181 def text_filter(field) raise UnimplementedError end |
#text_index(*args) ⇒ Object
Define fields to be indexed for text search. To update the index, you must call update_text_indexes after record save or at the appropriate point.
104 105 106 107 108 109 110 111 112 |
# File 'lib/redis/text_search.rb', line 104 def text_index(*args) = args.last.is_a?(::Hash) ? args.pop : {} [:minlength] ||= 2 [:split] ||= /\s+/ raise ArgumentError, "Must specify fields to index to #{self.name}.text_index" unless args.length > 0 args.each do |name| @text_indexes[name.to_sym] = .merge(:key => field_key(name, 'text_index')) end end |
#text_indexes_for(id, field) ⇒ Object
:nodoc:
204 205 206 207 |
# File 'lib/redis/text_search.rb', line 204 def text_indexes_for(id, field) #:nodoc: v = redis.get reverse_index_key(id, field) (v.nil? || v == '') ? [] : v.split(';') end |
#text_search(*args) ⇒ Object
Perform text search and return results from database. Options:
'string', 'string2'
:fields
:page
:per_page
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/redis/text_search.rb', line 120 def text_search(*args) = args.length > 1 && args.last.is_a?(::Hash) ? args.pop : {} fields = Array(.delete(:fields) || @text_indexes.keys) finder = .delete(:finder) unless finder unless defined?(:text_search_find) raise NoFinderMethod, "Could not detect how to find records; you must def text_search_find()" end finder = :text_search_find end # # Assemble set names for our intersection. # Accept two ways of doing search: either {:field => ['value','value'], :field => 'value'}, # or 'value','value', :fields => [:field, :field]. The first is an AND, the latter an OR. # ids = [] if args.empty? raise ArgumentError, "Must specify search string(s) to #{self.name}.text_search" elsif args.first.is_a?(::Hash) sets = [] args.first.each do |f,v| sets += text_search_sets_for(f,v) end # Execute single intersection (AND) ids = redis.sinter(*sets) else fields.each do |f| sets = text_search_sets_for(f,args) # Execute intersection per loop (OR) ids += redis.sinter(*sets) end end # Assemble our options for our finder conditions (destructive for speed) recalculate_count = .has_key?(:conditions) # Calculate pagination if applicable. Presence of :page indicates we want pagination. # Adapted from will_paginate/finder.rb if .has_key?(:page) page = .delete(:page) || 1 per_page = .delete(:per_page) || self.per_page Redis::TextSearch::Collection.create(page, per_page, nil) do |pager| # Convert page/per_page to limit/offset .merge!(:offset => pager.offset, :limit => pager.per_page) if ids.empty? pager.replace([]) pager.total_entries = 0 else pager.replace(send(finder, ids, ){ |*a| yield(*a) if block_given? }) pager.total_entries = recalculate_count ? wp_count(, [], finder.to_s) : ids.length # hacked into will_paginate for compat end end else # Execute finder directly ids.empty? ? [] : send(finder, ids, ) end end |
#text_search_find(ids, options) ⇒ Object
This is called when the class is imported, and uses reflection to guess how to retrieve records. You can override it by explicitly defining a text_search_find
class method that takes an array of IDs as an argument.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/redis/text_search.rb', line 65 def text_search_find(ids, ) if defined?(ActiveModel) # guess that we're on Rails 3 raise "text_search_find not implemented for Rails 3 (yet) - patches welcome" elsif defined?(ActiveRecord::Base) and ancestors.include?(ActiveRecord::Base) merge_text_search_conditions!(ids, ) all() elsif defined?(Sequel::Model) and ancestors.include?(Sequel::Model) self[primary_key.to_sym => ids].filter() elsif defined?(DataMapper::Resource) and included_modules.include?(DataMapper::Resource) get(.merge(primary_key.to_sym => ids)) end end |
#text_search_sets_for(field, values) ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/redis/text_search.rb', line 209 def text_search_sets_for(field, values) key = @text_indexes[field][:key] Array(values).collect do |val| str = val.downcase.gsub(/[^\w\s]+/,'').gsub(/\s+/, '.') # can't have " " in Redis cmd string "#{key}:#{str}" end end |