Class: Stretchy::Results::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stretchy/results/base.rb

Direct Known Subclasses

NullResults

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/stretchy/results/base.rb', line 12

def initialize(base)
  @base       = base
  @index_name = base.index || Stretchy.index_name
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



7
8
9
# File 'lib/stretchy/results/base.rb', line 7

def base
  @base
end

#index_nameObject (readonly)

Returns the value of attribute index_name.



7
8
9
# File 'lib/stretchy/results/base.rb', line 7

def index_name
  @index_name
end

Instance Method Details

#aggregationsObject



80
81
82
# File 'lib/stretchy/results/base.rb', line 80

def aggregations
  @aggregations ||= response['aggregations']
end

#bodyObject



24
25
26
27
28
29
# File 'lib/stretchy/results/base.rb', line 24

def body
  return @body if @body
  @body        = {query: base.to_search}
  @body[:aggs] = base.aggregate_builder if base.aggregate_builder.any?
  @body
end

#explanationsObject



74
75
76
77
78
# File 'lib/stretchy/results/base.rb', line 74

def explanations
  @scores ||= Hash[response['hits']['hits'].map do |hit|
    [hit['_id'], hit['_explanation']]
  end]
end

#hitsObject Also known as: results



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/stretchy/results/base.rb', line 52

def hits
  @hits ||= response['hits']['hits'].map do |hit|
    merge_fields = hit.reject{|field, _| ['_source', 'fields'].include?(field) }
    
    source_fields = {}
    if hit['fields']
      source_fields = Stretchy::Utils::DotHandler.convert_from_dotted_keys(hit['fields'])
    elsif hit['_source']
      source_fields = hit['_source']
    end
    
    source_fields.merge(merge_fields)
  end
end

#idsObject



48
49
50
# File 'lib/stretchy/results/base.rb', line 48

def ids
  @ids ||= response['hits']['hits'].map{|h| h['_id'] =~ /\d+(\.\d+)?/ ? h['_id'].to_i : h['_id'] }
end

#max_scoreObject



96
97
98
# File 'lib/stretchy/results/base.rb', line 96

def max_score
  @max_score ||= response['hits']['max_score']
end

#requestObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stretchy/results/base.rb', line 31

def request
  return @request if @request
  @request = {
    type: type, 
    body: body,
    from: offset,
    size: limit
  }
  @request[:fields]  = fields   if fields
  @request[:explain] = true     if base.explain
  @request
end

#responseObject



44
45
46
# File 'lib/stretchy/results/base.rb', line 44

def response
  @response ||= Stretchy.search(request)
end

#scoresObject



68
69
70
71
72
# File 'lib/stretchy/results/base.rb', line 68

def scores
  @scores ||= Hash[response['hits']['hits'].map do |hit|
    [hit['_id'], hit['_score']]
  end]
end

#shardsObject



88
89
90
# File 'lib/stretchy/results/base.rb', line 88

def shards
  @shards ||= response['_shards']
end

#tookObject



84
85
86
# File 'lib/stretchy/results/base.rb', line 84

def took
  @took ||= response['took']
end

#totalObject



92
93
94
# File 'lib/stretchy/results/base.rb', line 92

def total
  @total ||= response['hits']['total']
end

#total_pagesObject



20
21
22
# File 'lib/stretchy/results/base.rb', line 20

def total_pages
  [(total.to_f / limit).ceil, 1].max
end