Class: ActiveKit::Search::Index
- Inherits:
-
Object
- Object
- ActiveKit::Search::Index
- Defined in:
- lib/active_kit/search/index.rb
Instance Attribute Summary collapse
-
#attribute_value_parser ⇒ Object
readonly
Returns the value of attribute attribute_value_parser.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #add_attribute_to_schema(name:, options:) ⇒ Object
- #drop ⇒ Object
-
#escape_separators(value, include_space: false) ⇒ Object
List of characters from oss.redislabs.com/redisearch/Escaping/ ,.<>{}[]“‘:;!@#$%^&*()-+=~.
-
#fetch(term: nil, matching: "*", tags: {}, modifiers: {}, offset: nil, limit: nil, order: nil, page: nil, **options) ⇒ Object
Redis returns the results in the following form.
-
#initialize(current_class:, describer:) ⇒ Index
constructor
A new instance of Index.
- #reload ⇒ Object
Constructor Details
#initialize(current_class:, describer:) ⇒ Index
Returns a new instance of Index.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/active_kit/search/index.rb', line 6 def initialize(current_class:, describer:) @redis = ActiveKit::Search.redis @current_class = current_class @describer = describer current_class_name = current_class.to_s.parameterize.pluralize describer_name = describer.name.to_s @name = "activekit:search:index:#{current_class_name}:#{describer_name}" @prefix = "activekit:search:#{current_class_name}:#{describer_name}" @schema = {} @attribute_value_parser = {} end |
Instance Attribute Details
#attribute_value_parser ⇒ Object (readonly)
Returns the value of attribute attribute_value_parser.
4 5 6 |
# File 'lib/active_kit/search/index.rb', line 4 def attribute_value_parser @attribute_value_parser end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
4 5 6 |
# File 'lib/active_kit/search/index.rb', line 4 def prefix @prefix end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
4 5 6 |
# File 'lib/active_kit/search/index.rb', line 4 def schema @schema end |
Instance Method Details
#add_attribute_to_schema(name:, options:) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/active_kit/search/index.rb', line 19 def add_attribute_to_schema(name:, options:) raise "Error: No type specified for the search attribute '#{name}'." unless [:type].present? attribute_schema = [] as = .delete(:as) attribute_schema.push("AS #{as}") unless as.nil? type = .delete(:type) attribute_schema.push(type.to_s.upcase) unless type.nil? .each do |key, value| if key == :value @attribute_value_parser.store(name.to_s, value) elsif key.is_a?(Symbol) if value == true attribute_schema.push(key.to_s.upcase) elsif value != false attribute_schema.push("#{key.to_s.upcase} #{value.to_s}") end else raise "Invalid option provided to search attribute." end end @schema.store(name.to_s, attribute_schema.join(" ")) end |
#drop ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/active_kit/search/index.rb', line 60 def drop if exists? command = "FT.DROPINDEX #{@name}" @redis.call(command.split(' ')) @redis.del("#{@name}:command") Rails.logger.info "ActiveKit::Search | Index Dropped: " + @name end end |
#escape_separators(value, include_space: false) ⇒ Object
List of characters from oss.redislabs.com/redisearch/Escaping/ ,.<>{}[]“‘:;!@#$%^&*()-+=~
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/active_kit/search/index.rb', line 164 def escape_separators(value, include_space: false) value = value.to_s unless include_space pattern = %r{(\'|\"|\.|\,|\;|\<|\>|\{|\}|\[|\]|\"|\'|\=|\~|\*|\:|\#|\+|\^|\$|\@|\%|\!|\&|\)|\(|/|\-|\\)} else pattern = %r{(\'|\"|\.|\,|\;|\<|\>|\{|\}|\[|\]|\"|\'|\=|\~|\*|\:|\#|\+|\^|\$|\@|\%|\!|\&|\)|\(|/|\-|\\|\s)} end value.gsub(pattern) { |match| '\\' + match } end |
#fetch(term: nil, matching: "*", tags: {}, modifiers: {}, offset: nil, limit: nil, order: nil, page: nil, **options) ⇒ Object
Redis returns the results in the following form. Where first value is count of results, then every 2 elements are document_id, attributes respectively.
- 2, “doc:3”, [“name”, “Grape Juice”, “stock_quantity”, “4”, “minimum_stock”, “2”], “doc:4”, [“name”, “Apple Juice”, “stock_quantity”, “4”, “minimum_stock”, “2”]
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/active_kit/search/index.rb', line 71 def fetch(term: nil, matching: "*", tags: {}, modifiers: {}, offset: nil, limit: nil, order: nil, page: nil, **) original_term = term if term == "" results = nil elsif self.exists? if term.present? term.strip! term = escape_separators(term) case matching when "*" term = "#{term}*" when "%" term = "%#{term}%" when "%%" term = "%%#{term}%%" when "%%%" term = "%%%#{term}%%%" end term = " #{term}" else term = "" end if .present? = .map do |key, value| value = value.join("|") if value.is_a?(Array) "@#{escape_separators(key)}:{#{escape_separators(value, include_space: true).presence || 'nil'}}" end = .join(" ") = " #{}" else = "" end if modifiers.present? modifiers = modifiers.map { |key, value| "@#{escape_separators(key)}:#{escape_separators(value)}" }.join(" ") modifiers = " #{modifiers}" else modifiers = "" end if (offset.present? || limit.present?) && page.present? raise "Error: Cannot specify page and offset/limit at the same time. Please specify one of either page or offset/limit." end if page.present? page = page.to_i.abs case page when 0 page = 1 offset = 0 limit = 15 when 1 offset = 0 limit = 15 when 2 offset = 15 limit = 30 when 3 offset = 45 limit = 50 else limit = 100 offset = 15 + 30 + 50 + (page - 4) * limit end end query = "@database:{#{escape_separators(@describer.database.call, include_space: true)}}#{term}#{}#{modifiers}" command = [ "FT.SEARCH", @name, query, "LIMIT", offset ? offset.to_i : 0, # 0 is the default offset of redisearch in LIMIT 0 10. https://redis.io/commands/ft.search limit ? limit.to_i : 10 # 10 is the default limit of redisearch in LIMIT 0 10. https://redis.io/commands/ft.search ] command.push("SORTBY", *order.split(' ')) if order.present? results = @redis.call(command) Rails.logger.info "ActiveKit::Search | Index Searched: " + command.to_s Rails.logger.debug "=> " + results.to_s else results = nil end SearchResult.new(term: original_term, results: results, offset: offset, limit: limit, page: page, current_class: @current_class) end |
#reload ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_kit/search/index.rb', line 47 def reload current_command = @redis.get("#{@name}:command") schema = { "database" => "TAG SORTABLE", "id" => "NUMERIC SORTABLE" }.merge(@schema) command = "FT.CREATE #{@name} ON HASH PREFIX 1 #{@prefix}: SCHEMA #{schema.to_a.flatten.join(' ')}" unless current_command == command drop @redis.call(command.split(' ')) @redis.set("#{@name}:command", command) Rails.logger.info "ActiveKit::Search | Index Reloaded: " + "#{@name}:command" Rails.logger.debug "=> " + @redis.get("#{@name}:command").to_s end end |