Class: Elastictastic::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/elastictastic/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elastictastic/client.rb', line 5

def initialize(config)
  adapter_options = {
    :request_timeout => config.request_timeout,
    :connect_timeout => config.connect_timeout
  }
  if config.hosts.length == 1
    connection = Adapter[config.adapter].
      new(config.hosts.first, adapter_options)
  else
    connection = Rotor.new(
      config.hosts,
      adapter_options.merge(
        :adapter => config.adapter,
        :backoff_threshold => config.backoff_threshold,
        :backoff_start => config.backoff_start,
        :backoff_max => config.backoff_max
      )
    )
  end
  if config.logger
    connection = Middleware::LogRequests.new(connection, config.logger)
  end
  connection = Middleware::JsonDecodeResponse.new(connection)
  connection = Middleware::JsonEncodeBody.new(connection)
  connection = Middleware::RaiseServerErrors.new(connection)
  config.extra_middlewares.each do |middleware_class, *args|
    connection = middleware_class.new(connection, *args)
  end
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection


3
4
5
# File 'lib/elastictastic/client.rb', line 3

def connection
  @connection
end

Instance Method Details

#bulk(commands, params = {}) ⇒ Object


57
58
59
# File 'lib/elastictastic/client.rb', line 57

def bulk(commands, params = {})
  @connection.post(path_with_query('/_bulk', params), commands).body
end

#create(index, type, id, doc, params = {}) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/elastictastic/client.rb', line 36

def create(index, type, id, doc, params = {})
  if id
    @connection.put(
      path_with_query("/#{index}/#{type}/#{id}/_create", params),
      doc
    )
  else
    @connection.post(
      path_with_query("/#{index}/#{type}", params),
      doc
    )
  end.body
end

#delete(index = nil, type = nil, id = nil, params = {}) ⇒ Object


107
108
109
110
111
112
113
114
115
# File 'lib/elastictastic/client.rb', line 107

def delete(index = nil, type = nil, id = nil, params = {})
  path =
    if id then "/#{index}/#{type}/#{id}"
    elsif type then "/#{index}/#{type}"
    elsif index then "/#{index}"
    else "/"
    end
  @connection.delete(path_with_query(path, params)).body
end

#exists?(index, type, id, params = {}) ⇒ Boolean

Returns:

  • (Boolean)

61
62
63
64
65
# File 'lib/elastictastic/client.rb', line 61

def exists?(index, type, id, params = {})
  @connection.head(
    path_with_query("/#{index}/#{type}/#{id}", params)
  ).status == 200
end

#get(index, type, id, params = {}) ⇒ Object


67
68
69
70
71
# File 'lib/elastictastic/client.rb', line 67

def get(index, type, id, params = {})
  @connection.get(
    path_with_query("/#{index}/#{type}/#{id}", params)
  ).body
end

#mget(docspec, index = nil, type = nil) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/elastictastic/client.rb', line 73

def mget(docspec, index = nil, type = nil)
  path =
    if index.present?
      if type.present?
        "/#{index}/#{type}/_mget"
      else index.present?
        "#{index}/_mget"
      end
    else
      "/_mget"
    end
  @connection.post(path, 'docs' => docspec).body
end

#msearch(search_bodies) ⇒ Object


95
96
97
# File 'lib/elastictastic/client.rb', line 95

def msearch(search_bodies)
  @connection.post('/_msearch', search_bodies).body
end

#put_mapping(index, type, mapping) ⇒ Object


103
104
105
# File 'lib/elastictastic/client.rb', line 103

def put_mapping(index, type, mapping)
  @connection.put("/#{index}/#{type}/_mapping", mapping).body
end

#scroll(id, options = {}) ⇒ Object


99
100
101
# File 'lib/elastictastic/client.rb', line 99

def scroll(id, options = {})
  @connection.post("/_search/scroll?#{options.to_query}", id).body
end

#search(index, type, search, options = {}) ⇒ Object


87
88
89
90
91
92
93
# File 'lib/elastictastic/client.rb', line 87

def search(index, type, search, options = {})
  path = "/#{index}/#{type}/_search"
  @connection.post(
    "#{path}?#{options.to_query}",
    search
  ).body
end

#update(index, type, id, doc, params = {}) ⇒ Object


50
51
52
53
54
55
# File 'lib/elastictastic/client.rb', line 50

def update(index, type, id, doc, params = {})
  @connection.put(
    path_with_query("/#{index}/#{type}/#{id}", params),
    doc
  ).body
end