Module: Caoutsearch::Search::Response

Included in:
Base
Defined in:
lib/caoutsearch/search/response.rb

Instance Method Summary collapse

Instance Method Details

#aggregationsObject



70
71
72
# File 'lib/caoutsearch/search/response.rb', line 70

def aggregations
  @aggregations ||= Caoutsearch::Response::Aggregations.new(response.aggregations, self)
end

#each(&block) ⇒ Object



78
79
80
81
82
# File 'lib/caoutsearch/search/response.rb', line 78

def each(&block)
  return to_enum(:each) { hits.size } unless block

  hits.each(&block)
end

#hitsObject



42
43
44
# File 'lib/caoutsearch/search/response.rb', line 42

def hits
  response.dig("hits", "hits")
end

#idsObject



66
67
68
# File 'lib/caoutsearch/search/response.rb', line 66

def ids
  hits.map { |hit| hit["_id"] }
end

#loadObject



9
10
11
12
13
14
# File 'lib/caoutsearch/search/response.rb', line 9

def load
  @raw_response = perform_search_query(build.to_h)
  @response = Caoutsearch::Response::Response.new(@raw_response)
  @loaded = true
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/caoutsearch/search/response.rb', line 16

def loaded?
  @loaded
end

#max_scoreObject



46
47
48
# File 'lib/caoutsearch/search/response.rb', line 46

def max_score
  response.dig("hits", "max_score")
end

#perform_search_query(query) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/caoutsearch/search/response.rb', line 84

def perform_search_query(query)
  request_payload = {
    index: index_name,
    body: query
  }

  instrument(:search) do |event_payload|
    event_payload[:request] = request_payload
    event_payload[:response] = client.search(request_payload)
  end
end

#raw_responseObject



25
26
27
28
# File 'lib/caoutsearch/search/response.rb', line 25

def raw_response
  load unless loaded?
  @raw_response
end

#responseObject



20
21
22
23
# File 'lib/caoutsearch/search/response.rb', line 20

def response
  load unless loaded?
  @response
end

#shardsObject



38
39
40
# File 'lib/caoutsearch/search/response.rb', line 38

def shards
  response["_shards"]
end

#suggestionsObject



74
75
76
# File 'lib/caoutsearch/search/response.rb', line 74

def suggestions
  @suggestions ||= Caoutsearch::Response::Suggestions.new(response.suggest)
end

#timed_outObject



34
35
36
# File 'lib/caoutsearch/search/response.rb', line 34

def timed_out
  response["timed_out"]
end

#tookObject



30
31
32
# File 'lib/caoutsearch/search/response.rb', line 30

def took
  response["took"]
end

#totalObject



50
51
52
# File 'lib/caoutsearch/search/response.rb', line 50

def total
  response.dig("hits", "total")
end

#total_countObject



54
55
56
57
58
59
60
# File 'lib/caoutsearch/search/response.rb', line 54

def total_count
  if !@track_total_hits && (!loaded? || response.dig("hits", "total", "relation") == "gte")
    @total_count ||= spawn.track_total_hits!(true).source!(false).limit!(0).total_count
  else
    response.dig("hits", "total", "value")
  end
end

#total_pagesObject



62
63
64
# File 'lib/caoutsearch/search/response.rb', line 62

def total_pages
  (total_count.to_f / current_limit).ceil
end