Class: Esse::Search::Query

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/esse/search/query.rb,
lib/esse/search/query/dsl.rb

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#limit, #limit_value, #offset, #offset_value

Constructor Details

#initialize(transport, *indices, suffix: nil, **definition, &_block) ⇒ Query

Returns a new instance of Query.

Parameters:

  • transport (Esse::Transport)

    The client proxy to use for the query

  • indices (<Array<Esse::Index, String>] The class of the index to search or the index name)

    ndices [<Array<Esse::Index, String>] The class of the index to search or the index name

  • definition (Hash)

    The options to pass to the search.



13
14
15
16
17
# File 'lib/esse/search/query.rb', line 13

def initialize(transport, *indices, suffix: nil, **definition, &_block)
  @transport = transport
  @definition = definition
  @definition[:index] = self.class.normalize_indices(*indices, suffix: suffix) if indices.size > 0
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



8
9
10
# File 'lib/esse/search/query.rb', line 8

def definition
  @definition
end

#transportObject (readonly)

Returns the value of attribute transport.



8
9
10
# File 'lib/esse/search/query.rb', line 8

def transport
  @transport
end

Class Method Details

.normalize_indices(*indices, suffix: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/esse/search/query.rb', line 19

def self.normalize_indices(*indices, suffix: nil)
  indices.map do |index|
    if index.is_a?(Class) && index < Esse::Index
      index.index_name(suffix: suffix)
    elsif index.is_a?(String) || index.is_a?(Symbol)
      [index, suffix].compact.join('_')
    else
      raise ArgumentError, format('Invalid index type: %<index>p. It should be a Esse::Index class or a String index name', index: index)
    end
  end.join(',')
end

Instance Method Details

#responseObject



31
32
33
# File 'lib/esse/search/query.rb', line 31

def response
  @response ||= execute_search_query!
end

#resultsObject



35
36
37
38
39
# File 'lib/esse/search/query.rb', line 35

def results
  return paginated_results if respond_to?(:paginated_results, true)

  response.hits
end

#scroll_hits(batch_size: 1_000, scroll: '1m') ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/esse/search/query.rb', line 41

def scroll_hits(batch_size: 1_000, scroll: '1m')
  response = execute_search_query!(size: batch_size, scroll: scroll)
  scroll_id = nil
  fetched = 0
  total = response.total

  loop do
    fetched += response.hits.size
    yield(response.hits) if response.hits.any?
    break if fetched >= total
    scroll_id = response.raw_response['scroll_id'] || response.raw_response['_scroll_id']
    break unless scroll_id
    response = execute_scroll_query(scroll: scroll, scroll_id: scroll_id)
  end
ensure
  begin
    transport.clear_scroll(body: {scroll_id: scroll_id}) if scroll_id
  rescue Esse::Transport::NotFoundError
  end
end