rubberband 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CONTRIBUTORS +9 -0
- data/README.rdoc +2 -2
- data/lib/elasticsearch/client/admin_index.rb +9 -4
- data/lib/elasticsearch/client/default_scope.rb +11 -4
- data/lib/elasticsearch/client/index.rb +23 -33
- data/lib/elasticsearch/transport/base_protocol.rb +8 -4
- data/lib/elasticsearch/version.rb +2 -3
- data/rubberband.gemspec +7 -2
- data/test/admin_test.rb +25 -0
- data/test/index_test.rb +35 -0
- data/test/type_test.rb +34 -31
- metadata +9 -4
data/CONTRIBUTORS
ADDED
data/README.rdoc
CHANGED
@@ -10,8 +10,8 @@ Copyright 2010 Grant Rodgers. See included LICENSE file.
|
|
10
10
|
== Features
|
11
11
|
|
12
12
|
* Automatic failover, retry, and peer discovery
|
13
|
-
*
|
14
|
-
*
|
13
|
+
* Support for multiple transports (HTTP, Thrift, Memcached TODO)
|
14
|
+
* Support for multiple encodings (JSON (Yajl), Smile TODO)
|
15
15
|
|
16
16
|
== Usage
|
17
17
|
|
@@ -11,6 +11,13 @@ module ElasticSearch
|
|
11
11
|
execute(:index_status, indices, options)
|
12
12
|
end
|
13
13
|
|
14
|
+
def index_mapping(*args)
|
15
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
16
|
+
indices = args.empty? ? [(default_index || :all)] : args.flatten
|
17
|
+
indices.collect! { |i| PSEUDO_INDICES.include?(i) ? "_#{i}" : i }
|
18
|
+
execute(:index_mapping, indices, options)
|
19
|
+
end
|
20
|
+
|
14
21
|
# options: number_of_shards, number_of_replicas
|
15
22
|
def create_index(index, create_options={}, options={})
|
16
23
|
unless create_options[:index]
|
@@ -50,12 +57,10 @@ module ElasticSearch
|
|
50
57
|
|
51
58
|
# options: ignore_conflicts
|
52
59
|
def update_mapping(mapping, options={})
|
53
|
-
|
54
|
-
raise "index and type or defaults required" unless options[:index] && options[:type]
|
60
|
+
index, type, options = extract_required_scope(options)
|
55
61
|
|
56
62
|
options = options.dup
|
57
|
-
indices = Array(
|
58
|
-
type = options.delete(:type)
|
63
|
+
indices = Array(index)
|
59
64
|
unless mapping[type]
|
60
65
|
mapping = { type => mapping }
|
61
66
|
end
|
@@ -19,10 +19,17 @@ module ElasticSearch
|
|
19
19
|
|
20
20
|
private
|
21
21
|
|
22
|
-
def
|
23
|
-
options
|
24
|
-
options
|
25
|
-
|
22
|
+
def extract_scope(options)
|
23
|
+
options = options.dup
|
24
|
+
index = options.delete(:index) || default_index
|
25
|
+
type = options.delete(:type) || default_type
|
26
|
+
[index, type, options]
|
27
|
+
end
|
28
|
+
|
29
|
+
def extract_required_scope(options)
|
30
|
+
scope = extract_scope(options)
|
31
|
+
raise "index and type or defaults required" unless scope[0] && scope[1]
|
32
|
+
scope
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
@@ -4,8 +4,7 @@ module ElasticSearch
|
|
4
4
|
module Api
|
5
5
|
module Index
|
6
6
|
def index(document, options={})
|
7
|
-
|
8
|
-
raise "index and type or defaults required" unless options[:index] && options[:type]
|
7
|
+
index, type, options = extract_required_scope(options)
|
9
8
|
# type
|
10
9
|
# index
|
11
10
|
# id (optional)
|
@@ -13,11 +12,12 @@ module ElasticSearch
|
|
13
12
|
# timeout (optional)
|
14
13
|
# document (optional)
|
15
14
|
|
15
|
+
id = options.delete(:id)
|
16
16
|
if @batch
|
17
|
-
@batch << { :index => { :_index =>
|
17
|
+
@batch << { :index => { :_index => index, :_type => type, :_id => id }}
|
18
18
|
@batch << document
|
19
19
|
else
|
20
|
-
result = execute(:index,
|
20
|
+
result = execute(:index, index, type, id, document, options)
|
21
21
|
if result["ok"]
|
22
22
|
result["_id"]
|
23
23
|
else
|
@@ -27,27 +27,25 @@ module ElasticSearch
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def get(id, options={})
|
30
|
-
|
31
|
-
raise "index and type or defaults required" unless options[:index] && options[:type]
|
30
|
+
index, type, options = extract_required_scope(options)
|
32
31
|
# index
|
33
32
|
# type
|
34
33
|
# id
|
35
34
|
# fields
|
36
35
|
|
37
|
-
hit = execute(:get,
|
36
|
+
hit = execute(:get, index, type, id, options)
|
38
37
|
if hit
|
39
38
|
Hit.new(hit).freeze
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
43
42
|
def delete(id, options={})
|
44
|
-
|
45
|
-
raise "index and type or defaults required" unless options[:index] && options[:type]
|
43
|
+
index, type, options = extract_required_scope(options)
|
46
44
|
|
47
45
|
if @batch
|
48
|
-
@batch << { :delete => { :_index =>
|
46
|
+
@batch << { :delete => { :_index => index, :_type => type, :_id => id }}
|
49
47
|
else
|
50
|
-
result = execute(:delete,
|
48
|
+
result = execute(:delete, index, type, id, options)
|
51
49
|
result["ok"]
|
52
50
|
end
|
53
51
|
end
|
@@ -65,37 +63,37 @@ module ElasticSearch
|
|
65
63
|
#scroll Get a scroll id to continue paging through the search results. Value is the time to keep a scroll request around, e.g. 5m
|
66
64
|
#ids_only Return ids instead of hits
|
67
65
|
def search(query, options={})
|
68
|
-
|
66
|
+
index, type, options = extract_scope(options)
|
69
67
|
|
70
|
-
|
71
|
-
|
68
|
+
options[:size] ||= (options[:per_page] || options[:limit] || 10)
|
69
|
+
options[:from] ||= options[:size] * (options[:page].to_i-1) if options[:page] && options[:page].to_i > 1
|
70
|
+
options[:from] ||= options[:offset] if options[:offset]
|
72
71
|
|
73
|
-
|
72
|
+
options[:fields] = "_id" if options[:ids_only]
|
74
73
|
|
75
|
-
|
76
|
-
|
77
|
-
search_options[:from] ||= search_options[:offset] if search_options[:offset]
|
74
|
+
# options that hits take: per_page, page, ids_only
|
75
|
+
hits_options = options.select { |k, v| [:per_page, :page, :ids_only].include?(k) }
|
78
76
|
|
79
|
-
|
77
|
+
# options that elasticsearch doesn't recognize: page, per_page, ids_only, limit, offset
|
78
|
+
options.reject! { |k, v| [:page, :per_page, :ids_only, :limit, :offset].include?(k) }
|
80
79
|
|
81
|
-
response = execute(:search,
|
82
|
-
Hits.new(response,
|
80
|
+
response = execute(:search, index, type, query, options)
|
81
|
+
Hits.new(response, hits_options).freeze #ids_only returns array of ids instead of hits
|
83
82
|
end
|
84
83
|
|
85
84
|
#ids_only Return ids instead of hits
|
86
85
|
def scroll(scroll_id, options={})
|
87
86
|
response = execute(:scroll, scroll_id)
|
88
|
-
Hits.new(response,
|
87
|
+
Hits.new(response, { :ids_only => options[:ids_only] }).freeze
|
89
88
|
end
|
90
89
|
|
91
90
|
#df The default field to use when no field prefix is defined within the query.
|
92
91
|
#analyzer The analyzer name to be used when analyzing the query string.
|
93
92
|
#default_operator The default operator to be used, can be AND or OR. Defaults to OR.
|
94
93
|
def count(query, options={})
|
95
|
-
|
94
|
+
index, type, options = extract_scope(options)
|
96
95
|
|
97
|
-
|
98
|
-
response = execute(:count, options[:index], options[:type], query, count_options)
|
96
|
+
response = execute(:count, index, type, query, options)
|
99
97
|
response["count"].to_i #TODO check if count is nil
|
100
98
|
end
|
101
99
|
|
@@ -109,14 +107,6 @@ module ElasticSearch
|
|
109
107
|
@batch = nil
|
110
108
|
end
|
111
109
|
|
112
|
-
private
|
113
|
-
|
114
|
-
def slice_hash(hash, *keys)
|
115
|
-
h = {}
|
116
|
-
keys.each { |k| h[k] = hash[k] if hash.has_key?(k) }
|
117
|
-
h
|
118
|
-
end
|
119
|
-
|
120
110
|
end
|
121
111
|
end
|
122
112
|
end
|
@@ -4,16 +4,16 @@ module ElasticSearch
|
|
4
4
|
def index(index, type, id, document, options={})
|
5
5
|
body = encoder.is_encoded?(document) ? document : encoder.encode(document)
|
6
6
|
if id.nil?
|
7
|
-
response = request(:post, {:index => index, :type => type},
|
7
|
+
response = request(:post, {:index => index, :type => type}, options, body)
|
8
8
|
else
|
9
|
-
response = request(:put, {:index => index, :type => type, :id => id},
|
9
|
+
response = request(:put, {:index => index, :type => type, :id => id}, options, body)
|
10
10
|
end
|
11
11
|
handle_error(response) unless response.status == 200
|
12
12
|
encoder.decode(response.body)
|
13
13
|
end
|
14
14
|
|
15
15
|
def get(index, type, id, options={})
|
16
|
-
response = request(:get, {:index => index, :type => type, :id => id})
|
16
|
+
response = request(:get, {:index => index, :type => type, :id => id}, options)
|
17
17
|
return nil if response.status == 404
|
18
18
|
|
19
19
|
handle_error(response) unless response.status == 200
|
@@ -24,7 +24,7 @@ module ElasticSearch
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def delete(index, type, id, options={})
|
27
|
-
response = request(:delete,{:index => index, :type => type, :id => id})
|
27
|
+
response = request(:delete,{:index => index, :type => type, :id => id}, options)
|
28
28
|
handle_error(response) unless response.status == 200 # ElasticSearch always returns 200 on delete, even if the object doesn't exist
|
29
29
|
encoder.decode(response.body)
|
30
30
|
end
|
@@ -98,6 +98,10 @@ module ElasticSearch
|
|
98
98
|
standard_request(:put, {:index => index, :type => type, :op => "_mapping"}, options, encoder.encode(mapping))
|
99
99
|
end
|
100
100
|
|
101
|
+
def index_mapping(index_list, options={})
|
102
|
+
standard_request(:get, {:index => index_list, :op => "_mapping"})
|
103
|
+
end
|
104
|
+
|
101
105
|
def flush(index_list, options={})
|
102
106
|
standard_request(:post, {:index => index_list, :op => "_flush"}, options, "")
|
103
107
|
end
|
data/rubberband.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rubberband}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["grantr"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-02-08}
|
13
13
|
s.description = %q{An ElasticSearch client with ThriftClient-like failover handling.}
|
14
14
|
s.email = %q{grantr@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
"TODO"
|
19
19
|
]
|
20
20
|
s.files = [
|
21
|
+
"CONTRIBUTORS",
|
21
22
|
"Gemfile",
|
22
23
|
"Gemfile.lock",
|
23
24
|
"LICENSE",
|
@@ -49,8 +50,10 @@ Gem::Specification.new do |s|
|
|
49
50
|
"lib/elasticsearch/version.rb",
|
50
51
|
"lib/rubberband.rb",
|
51
52
|
"rubberband.gemspec",
|
53
|
+
"test/admin_test.rb",
|
52
54
|
"test/elasticsearch_test.rb",
|
53
55
|
"test/hits_test.rb",
|
56
|
+
"test/index_test.rb",
|
54
57
|
"test/test_helper.rb",
|
55
58
|
"test/type_test.rb",
|
56
59
|
"vendor/elasticsearch/elasticsearch.thrift"
|
@@ -61,8 +64,10 @@ Gem::Specification.new do |s|
|
|
61
64
|
s.rubygems_version = %q{1.3.7}
|
62
65
|
s.summary = %q{An ElasticSearch client.}
|
63
66
|
s.test_files = [
|
67
|
+
"test/admin_test.rb",
|
64
68
|
"test/elasticsearch_test.rb",
|
65
69
|
"test/hits_test.rb",
|
70
|
+
"test/index_test.rb",
|
66
71
|
"test/test_helper.rb",
|
67
72
|
"test/type_test.rb"
|
68
73
|
]
|
data/test/admin_test.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BasicTest < Test::Unit::TestCase
|
4
|
+
context "basic ops" do
|
5
|
+
|
6
|
+
setup do
|
7
|
+
@first_index = 'first-' + Time.now.to_i.to_s
|
8
|
+
@client = ElasticSearch.new('127.0.0.1:9200', :index => @first_index, :type => "tweet")
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
@client.delete_index(@first_index)
|
13
|
+
sleep(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
#TODO this test fails randomly, there's some kind of timing issue here
|
17
|
+
should "get and update mappings" do
|
18
|
+
@client.index({:foo => "bar"}, :id => "1", :refresh => true)
|
19
|
+
|
20
|
+
assert_equal({@first_index => {"tweet" => { "properties" => { "foo" => {"type" => "string" }}}}}, @client.index_mapping(@first_index))
|
21
|
+
@client.update_mapping({"tweet" => {:properties => {:bar => {:type => "string"}}}})
|
22
|
+
assert_equal({@first_index => {"tweet" => { "properties" => { "foo" => {"type" => "string" }, "bar" => { "type" => "string"}}}}}, @client.index_mapping(@first_index))
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/test/index_test.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IndexTest < Test::Unit::TestCase
|
4
|
+
context "index ops" do
|
5
|
+
|
6
|
+
setup do
|
7
|
+
@first_index = 'first-' + Time.now.to_i.to_s
|
8
|
+
@client = ElasticSearch.new('127.0.0.1:9200', :index => @first_index, :type => "tweet")
|
9
|
+
end
|
10
|
+
|
11
|
+
teardown do
|
12
|
+
@client.delete_index(@first_index)
|
13
|
+
sleep(1)
|
14
|
+
end
|
15
|
+
|
16
|
+
should "do basic ops on a document" do
|
17
|
+
@client.index({:foo => "bar"}, :id => "1", :refresh => true)
|
18
|
+
|
19
|
+
assert_equal "bar", @client.get("1").foo
|
20
|
+
assert_equal true, @client.delete("1", :refresh => true)
|
21
|
+
assert_equal nil, @client.get("1")
|
22
|
+
|
23
|
+
@client.index({:foo => "bar"}, :id => "1")
|
24
|
+
@client.index({:foo => "baz"}, :id => "2")
|
25
|
+
@client.index({:foo => "baz also"}, :id => "3")
|
26
|
+
@client.refresh(@first_index)
|
27
|
+
|
28
|
+
assert_equal 1, @client.search("bar").size
|
29
|
+
assert_equal 1, @client.count("bar")
|
30
|
+
|
31
|
+
assert_equal 2, @client.search(:query => { :term => { :foo => 'baz' }}).size
|
32
|
+
assert_equal 2, @client.count(:term => { :foo => 'baz' })
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/test/type_test.rb
CHANGED
@@ -2,43 +2,46 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class TypeTest < Test::Unit::TestCase
|
4
4
|
context "Test with differents Types" do
|
5
|
-
|
5
|
+
|
6
6
|
setup do
|
7
|
-
@
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
11
|
-
@client.
|
12
|
-
@client.index({:user =>
|
13
|
-
@client.index({:user =>
|
14
|
-
@client.
|
7
|
+
@first_index = 'first-' + Time.now.to_i.to_s
|
8
|
+
@second_index = 'second-' + Time.now.to_i.to_s
|
9
|
+
@third_index = 'third-' + Time.now.to_i.to_s
|
10
|
+
@username = 'kimchy' + Time.now.to_i.to_s
|
11
|
+
@client = ElasticSearch.new('127.0.0.1:9200', :index => @first_index, :type => "tweet")
|
12
|
+
@client.index({:user => @username}, :id => 1)
|
13
|
+
@client.index({:user => @username}, :id => 2, :type => "grillo")
|
14
|
+
@client.index({:user => @username}, :id => 3, :type => "cote")
|
15
|
+
@client.index({:user => @username}, :id => 4, :index => @second_index, :type => "cote")
|
16
|
+
@client.index({:user => @username}, :id => 5, :index => @third_index, :type => "mencho")
|
17
|
+
@client.index({:user => @username}, :id => 6, :index => @third_index, :type => "cote")
|
18
|
+
@client.refresh(@first_index, @second_index, @third_index)
|
15
19
|
end
|
16
20
|
|
17
21
|
teardown do
|
18
|
-
|
19
|
-
|
20
|
-
|
22
|
+
@client.delete_index(@first_index)
|
23
|
+
@client.delete_index(@second_index)
|
24
|
+
@client.delete_index(@third_index)
|
25
|
+
sleep(1)
|
21
26
|
end
|
22
27
|
|
23
|
-
should "
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
assert_equal @client.count(
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
should "Test different stages using indexes and types" do
|
29
|
+
# Search in all indexes
|
30
|
+
assert_equal @client.count(@username,{:index => "", :type => ""}), 6
|
31
|
+
|
32
|
+
# Search in all types with index first
|
33
|
+
assert_equal @client.count(@username,{:index => @first_index, :type => ""}), 3
|
34
|
+
|
35
|
+
# Search in first index with types tweet,cote
|
36
|
+
assert_equal @client.count(@username,{:index => @first_index, :type => "tweet,cote"}), 2
|
37
|
+
|
38
|
+
# Search in index first and second
|
39
|
+
@first_and_second = @first_index + ',' + @second_index
|
40
|
+
assert_equal @client.count(@username,{:index => @first_and_second, :type => ""}), 4
|
41
|
+
|
42
|
+
# Search in types grillo,cote of all indexes" do
|
43
|
+
assert_equal @client.count(@username,{:index => "", :type => "grillo,cote"}), 4
|
33
44
|
end
|
34
|
-
|
35
|
-
should "search in index twitter,cotes" do
|
36
|
-
assert_equal @client.count("kimchy",{:index => "twitter,cotes", :type => ""}), 4
|
37
|
-
end
|
38
|
-
|
39
|
-
should "search in types grillo,cote of all indexes" do
|
40
|
-
assert_equal @client.count("kimchy",{:index => "", :type => "grillo,cote"}), 4
|
41
|
-
end
|
42
|
-
|
45
|
+
|
43
46
|
end
|
44
47
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- grantr
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-02-08 00:00:00 -08:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -110,6 +110,7 @@ extra_rdoc_files:
|
|
110
110
|
- README.rdoc
|
111
111
|
- TODO
|
112
112
|
files:
|
113
|
+
- CONTRIBUTORS
|
113
114
|
- Gemfile
|
114
115
|
- Gemfile.lock
|
115
116
|
- LICENSE
|
@@ -141,8 +142,10 @@ files:
|
|
141
142
|
- lib/elasticsearch/version.rb
|
142
143
|
- lib/rubberband.rb
|
143
144
|
- rubberband.gemspec
|
145
|
+
- test/admin_test.rb
|
144
146
|
- test/elasticsearch_test.rb
|
145
147
|
- test/hits_test.rb
|
148
|
+
- test/index_test.rb
|
146
149
|
- test/test_helper.rb
|
147
150
|
- test/type_test.rb
|
148
151
|
- vendor/elasticsearch/elasticsearch.thrift
|
@@ -160,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
160
163
|
requirements:
|
161
164
|
- - ">="
|
162
165
|
- !ruby/object:Gem::Version
|
163
|
-
hash: -
|
166
|
+
hash: -4380067439050607353
|
164
167
|
segments:
|
165
168
|
- 0
|
166
169
|
version: "0"
|
@@ -180,7 +183,9 @@ signing_key:
|
|
180
183
|
specification_version: 3
|
181
184
|
summary: An ElasticSearch client.
|
182
185
|
test_files:
|
186
|
+
- test/admin_test.rb
|
183
187
|
- test/elasticsearch_test.rb
|
184
188
|
- test/hits_test.rb
|
189
|
+
- test/index_test.rb
|
185
190
|
- test/test_helper.rb
|
186
191
|
- test/type_test.rb
|