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



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

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

#explanationsObject



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

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

#hitsObject Also known as: results



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stretchy/results/base.rb', line 47

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



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

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

#max_scoreObject



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

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

#requestObject



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

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

#responseObject



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

def response
  params = {
    type: type, 
    body: request,
    from: offset,
    size: limit
  }
  params[:fields]  = fields   if fields
  params[:explain] = true     if base.explain
  @response ||= Stretchy.search(params)
end

#scoresObject



63
64
65
66
67
# File 'lib/stretchy/results/base.rb', line 63

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

#shardsObject



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

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

#tookObject



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

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

#totalObject



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

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