Module: ElasticRecord::Index::Search

Included in:
ElasticRecord::Index
Defined in:
lib/elastic_record/index/search.rb

Instance Method Summary collapse

Instance Method Details

#build_scroll_enumerator(search: nil, scroll_id: nil, batch_size: 100, keep_alive: ElasticRecord::Config.scroll_keep_alive) ⇒ Object



83
84
85
# File 'lib/elastic_record/index/search.rb', line 83

def build_scroll_enumerator(search: nil, scroll_id: nil, batch_size: 100, keep_alive: ElasticRecord::Config.scroll_keep_alive)
  ScrollEnumerator.new(self, search: search, scroll_id: scroll_id, batch_size: batch_size, keep_alive: keep_alive)
end

#delete_scroll(scroll_id) ⇒ Object



98
99
100
# File 'lib/elastic_record/index/search.rb', line 98

def delete_scroll(scroll_id)
  connection.json_delete('/_search/scroll', { scroll_id: scroll_id })
end

#explain(id, elastic_query) ⇒ Object



79
80
81
# File 'lib/elastic_record/index/search.rb', line 79

def explain(id, elastic_query)
  get "_explain", elastic_query
end

#multi_search(headers_and_bodies) ⇒ Object



73
74
75
76
77
# File 'lib/elastic_record/index/search.rb', line 73

def multi_search(headers_and_bodies)
  queries = headers_and_bodies.flat_map { |header, body| [header.to_json, body.to_json] }
  queries = queries.join("\n") + "\n"
  get "_msearch", queries
end

#record_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/elastic_record/index/search.rb', line 62

def record_exists?(id)
  get(id)['found']
end

#scroll(scroll_id, scroll_keep_alive) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/elastic_record/index/search.rb', line 87

def scroll(scroll_id, scroll_keep_alive)
  options = {scroll_id: scroll_id, scroll: scroll_keep_alive}
  connection.json_get("/_search/scroll?#{options.to_query}")
rescue ElasticRecord::ConnectionError => e
  case e.status_code
  when '400' then raise ElasticRecord::InvalidScrollError, e.message
  when '404' then raise ElasticRecord::ExpiredScrollError, e.message
  else raise e
  end
end

#search(elastic_query, options = {}) ⇒ Object



66
67
68
69
70
71
# File 'lib/elastic_record/index/search.rb', line 66

def search(elastic_query, options = {})
  url = "_search"
  url += "?#{options.to_query}" if options.any?

  get url, elastic_query
end