Class: ElasticSearch::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/jruby-elasticsearch/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a new ElasticSearch client.

options: :type => [:local, :node] - :local will create a process-local

elasticsearch instances

:host => “hostname” - the hostname to connect to. :port => 9200 - the port to connect to :cluster => “clustername” - the cluster name to use



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jruby-elasticsearch/client.rb', line 16

def initialize(options={})
  builder = org.elasticsearch.node.NodeBuilder.nodeBuilder
  builder.client(true)

  # The client doesn't need to serve http
  builder.settings.put("http.enabled", false)

  case options[:type]
  when :local
    builder.local(true)
    @node = builder.node
    @client = @node.client
  when :transport
    # TODO(sissel): Support transport client
  else
    # Use unicast discovery a host is given
    if !options[:host].nil?
      port = (options[:port] or "9300")
      builder.settings.put("discovery.zen.ping.multicast.enabled", false)
      builder.settings.put("discovery.zen.ping.unicast.hosts", "#{options[:host]}:#{port}")
      #builder.settings.put("es.transport.tcp.port", port)
    end

    if options[:bind_host]
      builder.settings.put('network.host', options[:bind_host])
    end

    if !options[:cluster].nil?
      builder.clusterName(options[:cluster])
    end
    @node = builder.node
    @client = @node.client
  end

end

Instance Method Details

#bulkObject



55
56
57
# File 'lib/jruby-elasticsearch/client.rb', line 55

def bulk
  return ElasticSearch::BulkRequest.new(@client)
end

#bulkstream(queue_size = 10, flush_interval = 1) ⇒ Object



60
61
62
# File 'lib/jruby-elasticsearch/client.rb', line 60

def bulkstream(queue_size=10, flush_interval=1)
  return ElasticSearch::BulkStream.new(self, queue_size, flush_interval)
end

#clusterObject

def search



118
119
120
# File 'lib/jruby-elasticsearch/client.rb', line 118

def cluster
  return @client.admin.cluster
end

#index(index, type, id = nil, data = {}, &block) ⇒ Object

Index a new document

args:

index: the index name
type: the type name
id: (optional) the id of the document
data: (optional) the data for this document
&block: (optional) optional block for using the DSL to add data

Returns an ElasticSearch::IndexRequest instance.

Example w/ DSL:

request = client.index("foo", "logs") do
  filename "/var/log/message"
  mesage "hello world"
  timestamp 123456
end

request.execute!


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jruby-elasticsearch/client.rb', line 84

def index(index, type, id=nil, data={}, &block)
  # Permit 'id' being omitted entirely.
  # Thus a call call: index("foo", "bar", somehash) is valid.
  if id.is_a?(Hash)
    data = id
    id = nil
  end

  indexreq = ElasticSearch::IndexRequest.new(@client, index, type, id, data)
  if block_given?
    indexreq.instance_eval(&block)
  end
  return indexreq
end

#nodeObject



122
123
124
# File 'lib/jruby-elasticsearch/client.rb', line 122

def node
  return @client.admin.cluster
end

#search(&block) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/jruby-elasticsearch/client.rb', line 110

def search(&block)
  searchreq = ElasticSearch::SearchRequest.new(@client)
  if block_given?
    searchreq.with(&block)
  end
  return searchreq
end