Class: ElasticSearch::Client

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

Defined Under Namespace

Classes: ConfigurationError, Error

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 :node_name => “node_name” - the node name to use when joining the cluster



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jruby-elasticsearch/client.rb', line 20

def initialize(options={})
  builder = org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder
  builder.put("node.client", true)

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

  # Use unicast discovery a host is given
  if !options[:host].nil?
    port = (options[:port] or "9300")
    builder.put("discovery.zen.ping.multicast.enabled", false)
    if port =~ /^\d+-\d+$/
      # port ranges are 'host[port1-port2]' according to 
      # http://www.elasticsearch.org/guide/reference/modules/discovery/zen/
      # However, it seems to only query the first port.
      # So generate our own list of unicast hosts to scan.
      range = Range.new(*port.split("-"))
      hosts = range.collect { |p| "#{options[:host]}:#{p}" }.join(",")
      builder.put("discovery.zen.ping.unicast.hosts", hosts)
    else
      # only one port, not a range.
      puts "PORT SETTINGS #{options[:host]}:#{port}"
      builder.put("discovery.zen.ping.unicast.hosts",
                           "#{options[:host]}:#{port}")
    end
  end

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

  if options[:bind_port]
    builder.put('transport.tcp.port', options[:bind_port])
  end

  if options[:node_name]
    builder.put('node.name', options[:node_name])
  end

  if !options[:cluster].nil?
    builder.put('cluster.name', options[:cluster])
  end

  case options[:type]
    when :transport 
      @client = org.elasticsearch.client.transport.TransportClient.new(builder.build)
      if options[:host]
        @client.addTransportAddress(
          org.elasticsearch.common.transport.InetSocketTransportAddress.new(
            options[:host], options[:port] || 9300
          )
        )
      else
        raise ConfigurationError, "When using a transport client, you must give a :host setting to ElasticSearch::Client.new. Otherwise, I don't know what elasticsearch servers talk to."
      end
    else
      nodebuilder = org.elasticsearch.node.NodeBuilder.nodeBuilder
      @client = nodebuilder.settings(builder).node.client
  end
end

Instance Method Details

#bulkObject



84
85
86
# File 'lib/jruby-elasticsearch/client.rb', line 84

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

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



89
90
91
# File 'lib/jruby-elasticsearch/client.rb', line 89

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

#clusterObject

def search



147
148
149
# File 'lib/jruby-elasticsearch/client.rb', line 147

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!


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jruby-elasticsearch/client.rb', line 113

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



151
152
153
# File 'lib/jruby-elasticsearch/client.rb', line 151

def node
  return @client.admin.cluster
end

#search(&block) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/jruby-elasticsearch/client.rb', line 139

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