rubberband 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ Maintainer:
2
+ Grant Rodgers <grantr at gmail dot com>
3
+
4
+ Contributors:
5
+ Ernie Makris
6
+ jeroig
7
+ mootpointer
8
+ Ryan Sonnek
9
+ speedmax
@@ -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
- * Multiple transports (HTTP, Thrift, Memcached TODO)
14
- * Multiple encodings (JSON (Yajl), Smile TODO)
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
- set_default_scope!(options)
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(options.delete(:index))
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 set_default_scope!(options)
23
- options[:index] ||= default_index
24
- options[:type] ||= default_type
25
- nil
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
- set_default_scope!(options)
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 => options[:index], :_type => options[:type], :_id => options[:id] }}
17
+ @batch << { :index => { :_index => index, :_type => type, :_id => id }}
18
18
  @batch << document
19
19
  else
20
- result = execute(:index, options[:index], options[:type], options[:id], document, options)
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
- set_default_scope!(options)
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, options[:index], options[:type], id, options)
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
- set_default_scope!(options)
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 => options[:index], :_type => options[:type], :_id => id }}
46
+ @batch << { :delete => { :_index => index, :_type => type, :_id => id }}
49
47
  else
50
- result = execute(:delete, options[:index], options[:type], id, options)
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
- set_default_scope!(options)
66
+ index, type, options = extract_scope(options)
69
67
 
70
- #TODO this doesn't work for facets, because they have a valid query key as element. need a list of valid toplevel keys in the search dsl
71
- #query = {:query => query} if query.is_a?(Hash) && !query[:query] # if there is no query element, wrap query in one
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
- search_options = slice_hash(options, :df, :analyzer, :default_operator, :explain, :fields, :field, :sort, :from, :size, :search_type, :limit, :per_page, :page, :offset, :scroll)
72
+ options[:fields] = "_id" if options[:ids_only]
74
73
 
75
- search_options[:size] ||= (search_options[:per_page] || search_options[:limit] || 10)
76
- search_options[:from] ||= search_options[:size] * (search_options[:page].to_i-1) if search_options[:page] && search_options[:page].to_i > 1
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
- search_options[:fields] = "_id" if options[:ids_only]
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, options[:index], options[:type], query, search_options)
82
- Hits.new(response, slice_hash(options, :per_page, :page, :ids_only)).freeze #ids_only returns array of ids instead of hits
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, slice_hash(options, :ids_only)).freeze
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
- set_default_scope!(options)
94
+ index, type, options = extract_scope(options)
96
95
 
97
- count_options = slice_hash(options, :df, :analyzer, :default_operator)
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}, {}, body)
7
+ response = request(:post, {:index => index, :type => type}, options, body)
8
8
  else
9
- response = request(:put, {:index => index, :type => type, :id => id}, {}, body)
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
@@ -2,9 +2,8 @@ module ElasticSearch
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
- PATCH = 6
6
- BUILD = nil
5
+ PATCH = 7
7
6
 
8
- STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
7
+ STRING = [MAJOR, MINOR, PATCH].compact.join('.')
9
8
  end
10
9
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rubberband}
8
- s.version = "0.0.6"
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-01-07}
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
  ]
@@ -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
@@ -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
@@ -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
- #TODO figure out how to have one setup and one teardown
5
+
6
6
  setup do
7
- @client = ElasticSearch.new('127.0.0.1:9200', :index => "twitter", :type => "tweet")
8
- @client.index({:user => "kimchy"}, :id => 1)
9
- @client.index({:user => "kimchy"}, :id => 2, :type => "grillo")
10
- @client.index({:user => "kimchy"}, :id => 3, :type => "cote")
11
- @client.index({:user => "kimchy"}, :id => 4, :index => "cotes", :type => "cote")
12
- @client.index({:user => "kimchy"}, :id => 5, :index => "menchos", :type => "mencho")
13
- @client.index({:user => "kimchy"}, :id => 6, :index => "menchos", :type => "cote")
14
- @client.refresh("twitter", "cotes", "menchos")
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
- #@client.delete_index("twitter")
19
- #@client.delete_index("cotes")
20
- #@client.delete_index("menchos")
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 "search in all indexes" do
24
- assert_equal @client.count("kimchy",{:index => "", :type => ""}), 6
25
- end
26
-
27
- should "search in all types with index twitter" do
28
- assert_equal @client.count("kimchy",{:index => "twitter", :type => ""}), 3
29
- end
30
-
31
- should "search in index twitter with types tweet,cote" do
32
- assert_equal @client.count("kimchy",{:index => "twitter", :type => "tweet,cote"}), 2
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
- - 6
9
- version: 0.0.6
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-01-07 00:00:00 -08:00
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: -3014120754088484755
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