Class: Indices::Index

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/indices/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Index



7
8
9
10
11
12
# File 'lib/indices/index.rb', line 7

def initialize(name, options={})
  @name = name
  @type = @name.to_s.singularize
  @options = options
  make_indexable
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/indices/index.rb', line 26

def <=>(other)
  if has_parent? && other.has_parent?
    0
  elsif other.has_parent?
    1
  else
    -1
  end
end

#any?(*args) ⇒ Boolean



36
37
38
# File 'lib/indices/index.rb', line 36

def any?(*args)
  search(*args).count > 0
end

#buildObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/indices/index.rb', line 101

def build
  client.indices.put_mapping(
    index: namespace,
    type: type,
    body: mappings
  )
  model.find_in_batches do |records|
    client.bulk(
      index: namespace,
      type: type,
      body: records.map do |record|
        { index: with_parent(record, _id: record.id, data: serialize(record)) }
      end
    )
  end
end

#exists?(record) ⇒ Boolean



56
57
58
59
60
61
62
63
64
65
# File 'lib/indices/index.rb', line 56

def exists?(record)
  client.exists?(
    with_parent(
      record,
      index: namespace,
      type: type,
      id: record.id
    )
  )
end

#has_parent?Boolean



22
23
24
# File 'lib/indices/index.rb', line 22

def has_parent?
  mappings.has_key? :_parent
end

#index(record) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/indices/index.rb', line 67

def index(record)
  client.create(
    with_parent(
      record,
      index: namespace,
      type: type,
      id: record.id,
      body: serialize(record)
    )
  )
end

#mappingsObject



14
15
16
# File 'lib/indices/index.rb', line 14

def mappings
  @mappings ||= Dsl::Mappings.new(&options[:mappings]).to_h
end

#modelObject



18
19
20
# File 'lib/indices/index.rb', line 18

def model
  (options[:class_name] || type.classify).constantize
end

#none?(*args) ⇒ Boolean



40
41
42
# File 'lib/indices/index.rb', line 40

def none?(*args)
  !any?(*args)
end

#raw_search(query) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/indices/index.rb', line 48

def raw_search(query)
  client.search(
    index: namespace,
    type: type,
    body: query
  )
end

#reindex(record) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/indices/index.rb', line 79

def reindex(record)
  client.bulk(
    index: namespace,
    type: type,
    body: [
      { delete: with_parent(record, _id: record.id) },
      { index: with_parent(record, _id: record.id, data: serialize(record)) }
    ]
  )
end

#search(*args) ⇒ Object



44
45
46
# File 'lib/indices/index.rb', line 44

def search(*args)
  Collection.new self, *args, &options[:search]
end

#unindex(record) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/indices/index.rb', line 90

def unindex(record)
  client.delete(
    with_parent(
      record,
      index: namespace,
      type: type,
      id: record.id
    )
  )
end