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



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jruby-elasticsearch/client.rb', line 17

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)
      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.settings.put("discovery.zen.ping.unicast.hosts", hosts)
      else
        # only one port, not a range.
        puts "PORT SETTINGS #{options[:host]}:#{port}"
        builder.settings.put("discovery.zen.ping.unicast.hosts",
                             "#{options[:host]}:#{port}")
      end
    end

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

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

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

end

Instance Method Details

#bulkObject



72
73
74
# File 'lib/jruby-elasticsearch/client.rb', line 72

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

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



77
78
79
# File 'lib/jruby-elasticsearch/client.rb', line 77

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

#clusterObject

def search



135
136
137
# File 'lib/jruby-elasticsearch/client.rb', line 135

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!


101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jruby-elasticsearch/client.rb', line 101

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



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

def node
  return @client.admin.cluster
end

#search(&block) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/jruby-elasticsearch/client.rb', line 127

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