Class: RediSearch::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/redi_search/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/redi_search/index.rb', line 5

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/redi_search/index.rb', line 5

def name
  @name
end

#schemaObject (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, **options, &block)
  AddField.new(self, name, type, **options, &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, **term_options)
  Aggregate.new(self, term, **term_options)
end

#create(**options) ⇒ Object



25
26
27
# File 'lib/redi_search/index.rb', line 25

def create(**options)
  Create.new(self, schema, options).call
end

#create!(**options) ⇒ Object



29
30
31
# File 'lib/redi_search/index.rb', line 29

def create!(**options)
  Create.new(self, schema, options).call!
end

#del(document) ⇒ Object



61
62
63
# File 'lib/redi_search/index.rb', line 61

def del(document)
  document.del
end

#document_countObject



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

Returns:

  • (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

#fieldsObject



79
80
81
# File 'lib/redi_search/index.rb', line 79

def fields
  schema.fields.map { |field| field.name.to_s }
end

#infoObject



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, **term_options)
  Search.new(self, term, **term_options)
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