Class: RediSearch::Index
- Inherits:
-
Object
- Object
- RediSearch::Index
- Defined in:
- lib/redi_search/index.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #add(document) ⇒ Object
- #add!(document) ⇒ Object
-
#add_field(name, type, **options, &block) ⇒ Object
rubocop:disable Style/ArgumentsForwarding.
- #add_multiple(documents) ⇒ Object
- #aggregate(term = nil, **term_options) ⇒ Object
- #create(**options) ⇒ Object
- #create!(**options) ⇒ Object
- #del(document) ⇒ Object
- #document_count ⇒ Object
- #drop(keep_docs: false) ⇒ Object
- #drop!(keep_docs: false) ⇒ Object
- #exist? ⇒ Boolean
- #fields ⇒ Object
- #info ⇒ Object
-
#initialize(name, model = nil, &schema) ⇒ Index
constructor
A new instance of Index.
- #reindex(documents, recreate: false) ⇒ Object
- #search(term = nil, **term_options) ⇒ Object
- #spellcheck(query, distance: 1) ⇒ Object
Constructor Details
#initialize(name, model = nil, &schema) ⇒ Index
Returns a new instance of Index.
7 8 9 10 11 |
# File 'lib/redi_search/index.rb', line 7 def initialize(name, model = nil, &schema) @name = name.to_s @schema = Schema.new(&schema) @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/redi_search/index.rb', line 5 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/redi_search/index.rb', line 5 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
5 6 7 |
# File 'lib/redi_search/index.rb', line 5 def schema @schema end |
Instance Method Details
#add(document) ⇒ Object
45 46 47 |
# File 'lib/redi_search/index.rb', line 45 def add(document) Hset.new(self, document).call end |
#add!(document) ⇒ Object
49 50 51 |
# File 'lib/redi_search/index.rb', line 49 def add!(document) Hset.new(self, document).call! end |
#add_field(name, type, **options, &block) ⇒ Object
rubocop:disable Style/ArgumentsForwarding
95 96 97 |
# File 'lib/redi_search/index.rb', line 95 def add_field(name, type, **, &block) AddField.new(self, name, type, **, &block).call! end |
#add_multiple(documents) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/redi_search/index.rb', line 53 def add_multiple(documents) client.multi do documents.each do |document| add(document) end end.all? { |response| response >= 0 } end |
#aggregate(term = nil, **term_options) ⇒ Object
17 18 19 |
# File 'lib/redi_search/index.rb', line 17 def aggregate(term = nil, **) Aggregate.new(self, term, **) end |
#create(**options) ⇒ Object
25 26 27 |
# File 'lib/redi_search/index.rb', line 25 def create(**) Create.new(self, schema, ).call end |
#create!(**options) ⇒ Object
29 30 31 |
# File 'lib/redi_search/index.rb', line 29 def create!(**) Create.new(self, schema, ).call! end |
#del(document) ⇒ Object
61 62 63 |
# File 'lib/redi_search/index.rb', line 61 def del(document) document.del end |
#document_count ⇒ Object
90 91 92 |
# File 'lib/redi_search/index.rb', line 90 def document_count info.num_docs.to_i end |
#drop(keep_docs: false) ⇒ Object
33 34 35 36 37 |
# File 'lib/redi_search/index.rb', line 33 def drop(keep_docs: false) drop!(keep_docs: keep_docs) rescue Redis::CommandError false end |
#drop!(keep_docs: false) ⇒ Object
39 40 41 42 43 |
# File 'lib/redi_search/index.rb', line 39 def drop!(keep_docs: false) command = ["DROPINDEX", name] command << "DD" unless keep_docs client.call!(*command.compact).ok? end |
#exist? ⇒ Boolean
65 66 67 68 69 |
# File 'lib/redi_search/index.rb', line 65 def exist? !client.call!("INFO", name).empty? rescue Redis::CommandError false end |
#fields ⇒ Object
79 80 81 |
# File 'lib/redi_search/index.rb', line 79 def fields schema.fields.map { |field| field.name.to_s } end |
#info ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/redi_search/index.rb', line 71 def info hash = Hash[*client.call!("INFO", name)] info_struct = Struct.new(*hash.keys.map(&:to_sym)) info_struct.new(*hash.values) rescue Redis::CommandError nil end |
#reindex(documents, recreate: false) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/redi_search/index.rb', line 83 def reindex(documents, recreate: false) drop if recreate create unless exist? add_multiple documents end |
#search(term = nil, **term_options) ⇒ Object
13 14 15 |
# File 'lib/redi_search/index.rb', line 13 def search(term = nil, **) Search.new(self, term, **) end |
#spellcheck(query, distance: 1) ⇒ Object
21 22 23 |
# File 'lib/redi_search/index.rb', line 21 def spellcheck(query, distance: 1) Spellcheck.new(self, query, distance: distance) end |