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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/elastictastic/client.rb', line 7

def initialize(config)
  builder = Faraday::Builder.new do |builder|
    builder.use Middleware::RaiseServerErrors
    builder.use Middleware::JsonEncodeBody
    builder.use Middleware::JsonDecodeResponse
    if config.logger
      builder.use Middleware::LogRequests, config.logger
    end
  end
  if config.hosts.length == 1
    builder.adapter config.adapter
    @connection =
      Faraday.new(:url => config.hosts.first, :builder => builder)
  else
    builder.use Middleware::Rotor, *config.hosts
    builder.adapter config.adapter
    @connection = Faraday.new(:builder => builder)
  end
end

Instance Attribute Details

#connection ⇒ Object (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/elastictastic/client.rb', line 5

def connection
  @connection
end

Instance Method Details

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



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

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

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



27
28
29
30
31
32
33
34
# File 'lib/elastictastic/client.rb', line 27

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



81
82
83
84
85
86
87
88
89
# File 'lib/elastictastic/client.rb', line 81

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

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



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

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

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



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elastictastic/client.rb', line 48

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

#put_mapping(index, type, mapping) ⇒ Object



77
78
79
# File 'lib/elastictastic/client.rb', line 77

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

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



70
71
72
73
74
75
# File 'lib/elastictastic/client.rb', line 70

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

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



62
63
64
65
66
67
68
# File 'lib/elastictastic/client.rb', line 62

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



36
37
38
# File 'lib/elastictastic/client.rb', line 36

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