Class: Elasticated::Results

Inherits:
Object
  • Object
show all
Includes:
Mixins::Inspectionable
Defined in:
lib/elasticated/results.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Inspectionable

#inspect

Instance Attribute Details

#aggregationsObject

Returns the value of attribute aggregations.



38
39
40
# File 'lib/elasticated/results.rb', line 38

def aggregations
  @aggregations
end

#documentsObject

Returns the value of attribute documents.



38
39
40
# File 'lib/elasticated/results.rb', line 38

def documents
  @documents
end

#hitsObject

methods: total, max_score



37
38
39
# File 'lib/elasticated/results.rb', line 37

def hits
  @hits
end

#scroll_idObject

Returns the value of attribute scroll_id.



34
35
36
# File 'lib/elasticated/results.rb', line 34

def scroll_id
  @scroll_id
end

#shardsObject

methods: total, successful, failed



36
37
38
# File 'lib/elasticated/results.rb', line 36

def shards
  @shards
end

#timed_outObject

Returns the value of attribute timed_out.



35
36
37
# File 'lib/elasticated/results.rb', line 35

def timed_out
  @timed_out
end

#tookObject

Returns the value of attribute took.



35
36
37
# File 'lib/elasticated/results.rb', line 35

def took
  @took
end

Class Method Details

.parse(elasticsearch_response, query = nil) ⇒ Object Also known as: from_elasticsearch_response



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/elasticated/results.rb', line 9

def parse(elasticsearch_response, query=nil)
  documents = elasticsearch_response['hits']['hits'].map{ |hit| Document.parse hit }
  results = new
  results.documents = documents
  # scroll metadata
  results.scroll_id = elasticsearch_response['_scroll_id']
  # cluster metadata
  results.took = elasticsearch_response['took']
  results.timed_out = elasticsearch_response['timed_out']
  # shards metadata
  shards = elasticsearch_response['_shards']
  results.shards = ShardsInfo.new shards['total'], shards['successful'], shards['failed']
  # search metadata
  hits = elasticsearch_response['hits']
  results.hits = HitsInfo.new hits['total'], hits['max_score']
  # aggregations results
  aggregations = elasticsearch_response['aggregations']
  results.aggregations = query.parse_aggregations aggregations if query && aggregations
  results
end

Instance Method Details

#append(another_results) ⇒ Object



40
41
42
43
44
45
# File 'lib/elasticated/results.rb', line 40

def append(another_results)
  self.documents = documents + another_results.documents
  self.aggregations = another_results.aggregations if another_results.aggregations
  self.scroll_id = another_results.scroll_id if another_results.scroll_id
  self
end

#countObject



59
60
61
# File 'lib/elasticated/results.rb', line 59

def count
  documents.count
end

#idsObject



55
56
57
# File 'lib/elasticated/results.rb', line 55

def ids
  documents.map &:id
end

#sources(with_ids = true) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/elasticated/results.rb', line 47

def sources(with_ids=true)
  documents.map do |d|
    d.source.tap do |s|
      s[:_id] = d.id if with_ids
    end
  end
end

#text_for_inspectObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/elasticated/results.rb', line 63

def text_for_inspect
  text = "#{hits.total} hits"
  text = case documents.count
  when 0; "#{text}, no documents"
  when 1; "#{text}, 1 document"
  else; "#{text}, #{documents.count} documents"
  end
  text = aggregations ? "#{text}, with aggregations" : "#{text}, no aggregations"
  text = "#{text}, including scroll_id" if scroll_id
  text
end