Class: Tagliani::Search::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/tagliani/search/index.rb,
lib/tagliani/search/index/object.rb

Defined Under Namespace

Classes: Object

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, schema: nil, queue: nil, length: nil) ⇒ Index

Returns a new instance of Index.



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

def initialize(name: nil, schema: nil, queue: nil, length: nil)
  @name = name || Tagliani.config.elasticsearch.index
  @schema = schema || Tagliani.config.schema.default
  @queue = queue || Tagliani.config.redis.queue
  @length = length || Tagliani.config.redis.length
  @search = Tagliani::Search.new(body: {})
end

Class Method Details

.bulk!(name: nil, queue: nil, length: nil) ⇒ Object



82
83
84
# File 'lib/tagliani/search/index.rb', line 82

def bulk!(name: nil, queue: nil, length: nil)
  new(name: name, queue: queue, length: length).bulk!
end

.create!(name: nil, schema: nil) ⇒ Object



67
68
69
# File 'lib/tagliani/search/index.rb', line 67

def create!(name: nil, schema: nil)
  new(name: name, schema: schema).create!
end

.delete!(name: nil) ⇒ Object



71
72
73
# File 'lib/tagliani/search/index.rb', line 71

def delete!(name: nil)
  new(name: name).delete!
end

.move!(from:, to:) ⇒ Object



75
76
77
78
79
80
# File 'lib/tagliani/search/index.rb', line 75

def move!(from:, to:)
  create!(name: to)
  Tagliani::Search.client.reindex({
    body: { source: { index: from }, dest: { index: to } }
  })
end

Instance Method Details

#add!(schema, async: false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tagliani/search/index.rb', line 50

def add!(schema, async: false)
  object = Object.new(schema)

  if async
    Tagliani.redis.sadd(@queue, object.to_json)
  else
    @search.client.index({
      index: @name,
      type: "doc",
      id: object.id,
      body: object.to_h,
      refresh: true
    })
  end
end

#bulk!Object



41
42
43
44
45
46
47
48
# File 'lib/tagliani/search/index.rb', line 41

def bulk!
  indices = Tagliani.redis.spop(@queue, @length)
  data = parse_data(indices, index_name: @name)
  
  return false if data.empty?

  @search.client.bulk(body: data)
end

#create!Object



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

def create!
  @search.client.indices.create index: @name, body: @schema
end

#delete!Object



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

def delete!
  @search.client.indices.delete index: @name, ignore: 404
end

#parse_data(indices, index_name:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tagliani/search/index.rb', line 26

def parse_data(indices, index_name:)
  [indices].flatten.flat_map do |text|
    object = Object.new(text)

    {
      index: {
        _index: index_name,
        _id: object.id,
        _type: "doc",
        data: object.to_h
      }
    }
  end
end

#refreshObject



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

def refresh
  @search.client.indices.refresh
end