Class: Metacrunch::Elasticsearch::Searcher
- Inherits:
-
Object
- Object
- Metacrunch::Elasticsearch::Searcher
- Includes:
- Enumerable, ClientFactory, OptionsHelpers
- Defined in:
- lib/metacrunch/elasticsearch/searcher.rb
Constant Summary collapse
- DEFAULT_BODY =
{ query: { match_all: {} } }
- DEFAULT_SCAN_SIZE =
per shard
200- DEFAULT_SCROLL_EXPIRY_TIME =
10.minutes
Instance Attribute Summary collapse
-
#bulk_size ⇒ Object
Returns the value of attribute bulk_size.
-
#index ⇒ Object
Returns the value of attribute index.
-
#scan_size ⇒ Object
Returns the value of attribute scan_size.
-
#scroll_expiry_time ⇒ Object
Returns the value of attribute scroll_expiry_time.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #call(items = []) ⇒ Object
- #each ⇒ Object
-
#initialize(options = {}) ⇒ Searcher
constructor
A new instance of Searcher.
Methods included from OptionsHelpers
#extract_options!, #normalize_options!
Methods included from ClientFactory
Constructor Details
#initialize(options = {}) ⇒ Searcher
Returns a new instance of Searcher.
21 22 23 24 25 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 21 def initialize( = {}) .deep_symbolize_keys! (, :_client_options_, :bulk_size, :index, :scan_size, :scroll_expiry_time, :type) @body = .presence || DEFAULT_BODY end |
Instance Attribute Details
#bulk_size ⇒ Object
Returns the value of attribute bulk_size.
15 16 17 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 15 def bulk_size @bulk_size end |
#index ⇒ Object
Returns the value of attribute index.
16 17 18 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 16 def index @index end |
#scan_size ⇒ Object
Returns the value of attribute scan_size.
17 18 19 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 17 def scan_size @scan_size end |
#scroll_expiry_time ⇒ Object
Returns the value of attribute scroll_expiry_time.
18 19 20 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 18 def scroll_expiry_time @scroll_expiry_time end |
#type ⇒ Object
Returns the value of attribute type.
19 20 21 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 19 def type @type end |
Instance Method Details
#call(items = []) ⇒ Object
27 28 29 30 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 27 def call(items = []) @docs_enumerator ||= @bulk_size ? each_slice(@bulk_size) : [each.to_a].to_enum items.concat(@docs_enumerator.next) end |
#each ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/metacrunch/elasticsearch/searcher.rb', line 32 def each return enum_for(__method__) unless block_given? client = client_factory search_result = client.search({ body: @body, index: @index, scroll: "#{@scroll_expiry_time || DEFAULT_SCROLL_EXPIRY_TIME}s", search_type: "scan", size: @scan_size || DEFAULT_SCAN_SIZE }) while ( search_result = client.scroll( scroll: "#{DEFAULT_SCROLL_EXPIRY_TIME}s", scroll_id: search_result["_scroll_id"] ) and # don't use &&, the semantic of and is important here search_result["hits"]["hits"].present? ) do search_result["hits"]["hits"].each do |_hit| yield _hit end end end |