jruby-elasticsearch 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/elasticsearch/actionlistener.rb +46 -0
- data/lib/elasticsearch/bulkrequest.rb +31 -0
- data/lib/elasticsearch/bulkstream.rb +7 -0
- data/lib/elasticsearch/client.rb +54 -0
- data/lib/elasticsearch/indexrequest.rb +42 -0
- data/lib/elasticsearch/namespace.rb +4 -0
- data/lib/elasticsearch/request.rb +25 -0
- data/lib/elasticsearch.rb +8 -0
- metadata +73 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require "java"
|
2
|
+
require "elasticsearch/namespace"
|
3
|
+
|
4
|
+
class ElasticSearch::ActionListener
|
5
|
+
include org.elasticsearch.action.ActionListener
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@failure_callbacks = []
|
9
|
+
@success_callbacks = []
|
10
|
+
end # def initialize
|
11
|
+
|
12
|
+
# Helper for registering callbacks.
|
13
|
+
# 'what' should be either :failure or :success
|
14
|
+
#
|
15
|
+
# You can register multiple callbacks if you wish.
|
16
|
+
# Callbacks are invoked in order of addition.
|
17
|
+
def on(what, &block)
|
18
|
+
case what
|
19
|
+
when :failure
|
20
|
+
@failure_callbacks << block
|
21
|
+
when :success
|
22
|
+
@success_callbacks << block
|
23
|
+
end
|
24
|
+
return self
|
25
|
+
end # def on
|
26
|
+
|
27
|
+
# Conforming to Interface org.elasticsearch.action.ActionListener
|
28
|
+
def onFailure(exception)
|
29
|
+
if !@failure_callbacks.empty?
|
30
|
+
@failure_callbacks.each { |c| c.call(exception) }
|
31
|
+
else
|
32
|
+
# Default is no failure callbacks
|
33
|
+
raise exception
|
34
|
+
end
|
35
|
+
end # def onFailure
|
36
|
+
|
37
|
+
# Conforming to Interface org.elasticsearch.action.ActionListener
|
38
|
+
def onResponse(response)
|
39
|
+
if !@success_callbacks.empty?
|
40
|
+
@success_callbacks.each { |c| c.call(response) }
|
41
|
+
else
|
42
|
+
# Default if no success callbacks
|
43
|
+
puts "#{self.class.name}#onResponse => #{response.inspect}"
|
44
|
+
end
|
45
|
+
end # def onResponse
|
46
|
+
end # class ElasticSearch::ActionListener
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "elasticsearch/namespace"
|
2
|
+
require "elasticsearch/request"
|
3
|
+
|
4
|
+
class ElasticSearch::BulkRequest < ElasticSearch::Request
|
5
|
+
# Create a new index request.
|
6
|
+
def initialize(client)
|
7
|
+
@client = client
|
8
|
+
|
9
|
+
@prep = @client.prepareBulk(index, type, id)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
# Execute this index request.
|
14
|
+
# This call is asynchronous.
|
15
|
+
#
|
16
|
+
# If a block is given, register it for both failure and success.
|
17
|
+
def execute(&block)
|
18
|
+
use_callback(&block) if block_given
|
19
|
+
action = @prep.execute(@handler)
|
20
|
+
return action
|
21
|
+
end
|
22
|
+
|
23
|
+
# Execute this index request synchronously
|
24
|
+
def execute!
|
25
|
+
return @prep.execute.actionGet()
|
26
|
+
end
|
27
|
+
|
28
|
+
def <<(request)
|
29
|
+
@prep.add(request)
|
30
|
+
end # def <<
|
31
|
+
end # def ElasticSearch::BulkRequest
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require "java"
|
2
|
+
require "elasticsearch/namespace"
|
3
|
+
require "elasticsearch/indexrequest"
|
4
|
+
|
5
|
+
class ElasticSearch::Client
|
6
|
+
def initialize
|
7
|
+
@node = org.elasticsearch.node.NodeBuilder.nodeBuilder.client(true).node
|
8
|
+
@client = @node.client
|
9
|
+
end # def initialize
|
10
|
+
|
11
|
+
# Index a new document
|
12
|
+
#
|
13
|
+
# args:
|
14
|
+
# index: the index name
|
15
|
+
# type: the type name
|
16
|
+
# id: (optional) the id of the document
|
17
|
+
# data: (optional) the data for this document
|
18
|
+
# &block: (optional) optional block for using the DSL to add data
|
19
|
+
#
|
20
|
+
# Returns an ElasticSearch::IndexRequest instance.
|
21
|
+
#
|
22
|
+
# Example w/ DSL:
|
23
|
+
#
|
24
|
+
# request = client.index("foo", "logs") do
|
25
|
+
# filename "/var/log/message"
|
26
|
+
# mesage "hello world"
|
27
|
+
# timestamp 123456
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# request.execute!
|
31
|
+
def index(index, type, id=nil, data={}, &block)
|
32
|
+
# Permit 'id' being omitted entirely.
|
33
|
+
# Thus a call call: index("foo", "bar", somehash) is valid.
|
34
|
+
if id.is_a?(Hash)
|
35
|
+
data = id
|
36
|
+
id = nil
|
37
|
+
end
|
38
|
+
|
39
|
+
indexreq = ElasticSearch::IndexRequest.new(@client, index, type, id, data)
|
40
|
+
if block_given?
|
41
|
+
indexreq.instance_eval(&block)
|
42
|
+
end
|
43
|
+
return indexreq
|
44
|
+
end # def index
|
45
|
+
|
46
|
+
def cluster
|
47
|
+
return @client.admin.cluster
|
48
|
+
end
|
49
|
+
|
50
|
+
def node
|
51
|
+
return @client.admin.cluster
|
52
|
+
end
|
53
|
+
end # class ElasticSearch::Client
|
54
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "elasticsearch/namespace"
|
2
|
+
require "elasticsearch/request"
|
3
|
+
|
4
|
+
class ElasticSearch::IndexRequest < ElasticSearch::Request
|
5
|
+
# Create a new index request.
|
6
|
+
def initialize(client, index, type, id=nil, data={})
|
7
|
+
@client = client
|
8
|
+
@index = index
|
9
|
+
@type = type
|
10
|
+
@id = id
|
11
|
+
@data = data
|
12
|
+
|
13
|
+
@prep = @client.prepareIndex(index, type, id)
|
14
|
+
super
|
15
|
+
end
|
16
|
+
|
17
|
+
# Execute this index request.
|
18
|
+
# This call is asynchronous.
|
19
|
+
#
|
20
|
+
# If a block is given, register it for both failure and success.
|
21
|
+
def execute(&block)
|
22
|
+
@prep.setSource(@data)
|
23
|
+
use_callback(&block) if block_given?
|
24
|
+
|
25
|
+
action = @prep.execute(@handler)
|
26
|
+
return action
|
27
|
+
end
|
28
|
+
|
29
|
+
# Execute this index request synchronously
|
30
|
+
def execute!
|
31
|
+
@prep.setSource(@data)
|
32
|
+
return @prep.execute.actionGet()
|
33
|
+
end
|
34
|
+
|
35
|
+
# DSL helper.
|
36
|
+
# TODO(sissel): Move this away to a DSL module.
|
37
|
+
def method_missing(*args)
|
38
|
+
key, value = args
|
39
|
+
puts "Adding: #{key}: #{value.inspect}"
|
40
|
+
@data[key.to_s] = value
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "elasticsearch/namespace"
|
2
|
+
require "elasticsearch/actionlistener"
|
3
|
+
|
4
|
+
class ElasticSearch::Request
|
5
|
+
# Create a new index request.
|
6
|
+
def initialize()
|
7
|
+
@handler = ElasticSearch::ActionListener.new
|
8
|
+
end
|
9
|
+
|
10
|
+
# See ElasticSearch::ActionListener#on
|
11
|
+
def on(event, &block)
|
12
|
+
return @handler.on(*args, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Execute this index request.
|
16
|
+
# This call is asynchronous.
|
17
|
+
#
|
18
|
+
# If a block is given, register it for both failure and success.
|
19
|
+
def use_callback(&block)
|
20
|
+
if block_given?
|
21
|
+
on(:failure, &block)
|
22
|
+
on(:success, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jruby-elasticsearch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jordan Sissel
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-27 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: ...
|
22
|
+
email:
|
23
|
+
- jls@semicomplete.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/elasticsearch.rb
|
32
|
+
- lib/elasticsearch/namespace.rb
|
33
|
+
- lib/elasticsearch/bulkrequest.rb
|
34
|
+
- lib/elasticsearch/actionlistener.rb
|
35
|
+
- lib/elasticsearch/request.rb
|
36
|
+
- lib/elasticsearch/indexrequest.rb
|
37
|
+
- lib/elasticsearch/client.rb
|
38
|
+
- lib/elasticsearch/bulkstream.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: https://github.com/jordansissel/jruby-elasticsearch
|
41
|
+
licenses:
|
42
|
+
- Apache License (2.0)
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: JRuby API for ElasticSearch using the native ES Java API
|
72
|
+
test_files: []
|
73
|
+
|