Module: Stretchy

Defined in:
lib/stretchy.rb,
lib/stretchy/api.rb,
lib/stretchy/node.rb,
lib/stretchy/utils.rb,
lib/stretchy/errors.rb,
lib/stretchy/scopes.rb,
lib/stretchy/factory.rb,
lib/stretchy/results.rb,
lib/stretchy/version.rb,
lib/stretchy/and_collector.rb

Overview

Defined Under Namespace

Modules: Errors, Factory, Scopes, Utils Classes: API, AndCollector, Node, Results

Constant Summary collapse

VERSION =
"0.7.0"

Class Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



78
79
80
81
82
83
84
# File 'lib/stretchy.rb', line 78

def method_missing(method, *args, &block)
  if client.respond_to?(method)
    client.send(method, *args, &block)
  else
    super
  end
end

Class Method Details

.clientObject



23
24
25
# File 'lib/stretchy.rb', line 23

def client
  @client ||= Elasticsearch::Client.new
end

.client=(client) ⇒ Object



27
28
29
# File 'lib/stretchy.rb', line 27

def client=(client)
  @client = client
end

.count_index(name, params = {}) ⇒ Object



56
57
58
# File 'lib/stretchy.rb', line 56

def count_index(name, params = {})
  client.count({index: name}.merge(params))['count']
end

.create_index(name, params = {}) ⇒ Object



48
49
50
# File 'lib/stretchy.rb', line 48

def create_index(name, params = {})
  client.indices.create({index: name}.merge(params)) unless index_exists? name
end

.delete_index(name) ⇒ Object



44
45
46
# File 'lib/stretchy.rb', line 44

def delete_index(name)
  client.indices.delete(index: name) if index_exists? name
end

.fake_resultsObject



74
75
76
# File 'lib/stretchy.rb', line 74

def fake_results
  Results.fake
end

.index_document(params = {}) ⇒ Object

Raises:

  • (IndexDoesNotExistError)


60
61
62
63
64
65
66
67
68
# File 'lib/stretchy.rb', line 60

def index_document(params = {})
  Utils.require_params!(:index_document, params, :index, :type, :body)

  raise IndexDoesNotExistError.new(
    "index #{params[:index]} does not exist"
  ) unless index_exists? params[:index]

  client.index(params)
end

.index_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/stretchy.rb', line 40

def index_exists?(name)
  client.indices.exists? index: name
end

.method_missing(method, *args, &block) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/stretchy.rb', line 78

def method_missing(method, *args, &block)
  if client.respond_to?(method)
    client.send(method, *args, &block)
  else
    super
  end
end

.query(options = {}) ⇒ Object



70
71
72
# File 'lib/stretchy.rb', line 70

def query(options = {})
  API.new(root: options)
end

.refresh_index(name, params = {}) ⇒ Object



52
53
54
# File 'lib/stretchy.rb', line 52

def refresh_index(name, params = {})
  client.indices.refresh({index: name}.merge(params)) if index_exists? name
end

.search(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/stretchy.rb', line 31

def search(options = {})
  client.search(options)
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => bre
  msg = bre.message[-150..-1]
  msg << "\n\n"
  msg << JSON.pretty_generate(options)
  raise msg
end