Class: Tagliani::Search

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

Defined Under Namespace

Classes: Index

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, where: nil) ⇒ Search

Returns a new instance of Search.



17
18
19
20
21
22
23
24
# File 'lib/tagliani/search.rb', line 17

def initialize(body:, where: nil)
  @index = Tagliani.config.elasticsearch.index
  @client = self.class.client
  @where_clause = where
  @body = body

  build_where(@where_clause) if @where_clause
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Class Method Details

.clientObject



9
10
11
12
# File 'lib/tagliani/search.rb', line 9

def client
  Elasticsearch::Client.new host: Tagliani.config.elasticsearch.url,
                            log: Tagliani.config.elasticsearch.log
end

Instance Method Details

#responseObject



26
27
28
# File 'lib/tagliani/search.rb', line 26

def response
  @client.search(index: @index, body: @body)
end

#serialize(type:) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tagliani/search.rb', line 30

def serialize(type:)
  model_kls = "#{type}_kls"
  model_id = "#{type}_id"

  models = {}

  response['hits']['hits'].each do |entry|
    entry_source = entry['_source']
    class_name = entry_source[model_kls]
    models[class_name] ||= []
    models[class_name] << entry_source[model_id]
  end

  models.flat_map do |model, ids|
    model.constantize.where(id: ids)
  end
end