Module: Springy

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

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.clientObject



20
21
22
# File 'lib/springy.rb', line 20

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

.client=(client) ⇒ Object



24
25
26
# File 'lib/springy.rb', line 24

def client=(client)
  @client = client
end

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



53
54
55
# File 'lib/springy.rb', line 53

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

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



45
46
47
# File 'lib/springy.rb', line 45

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

.delete_index(name) ⇒ Object



41
42
43
# File 'lib/springy.rb', line 41

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

.index_document(params = {}) ⇒ Object

Raises:

  • (IndexDoesNotExistError)


57
58
59
60
61
62
63
64
65
# File 'lib/springy.rb', line 57

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)


37
38
39
# File 'lib/springy.rb', line 37

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

.query(options = {}) ⇒ Object



67
68
69
# File 'lib/springy.rb', line 67

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

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



49
50
51
# File 'lib/springy.rb', line 49

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

.search(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/springy.rb', line 28

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