stretcher 1.14.0 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -4,7 +4,6 @@ services:
4
4
  - elasticsearch
5
5
 
6
6
  rvm:
7
- - ruby-head
8
7
  - 2.0.0
9
8
  - 1.9.3
10
9
  - 1.8.7
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in stretcher.gemspec
4
4
  gemspec
5
+
data/README.md CHANGED
@@ -109,6 +109,7 @@ Specs may be run with `rake spec`
109
109
  * [@chatgris](https://github.com/chatgris)
110
110
  * [@alpinegizmo](https://github.com/alpinegizmo)
111
111
  * [@mcolyer](https://github.com/mcolyer)
112
+ * [@mulderp](https://github.com/mulderp)
112
113
 
113
114
  ## Contributing
114
115
 
@@ -44,9 +44,10 @@ module Stretcher
44
44
  body = documents.reduce("") {|post_data, d_raw|
45
45
  d = Hashie::Mash.new(d_raw)
46
46
  index_meta = { :_index => name, :_id => (d.delete(:id) || d.delete(:_id)) }
47
-
47
+
48
+ system_fields = %w{_type _parent _routing}
48
49
  d.keys.reduce(index_meta) do |memo, key|
49
- index_meta[key] = d.delete(key) if key.to_s.start_with?('_')
50
+ index_meta[key] = d.delete(key) if system_fields.include?(key.to_s)
50
51
  end
51
52
 
52
53
  post_data << ({action => index_meta}.to_json + "\n")
@@ -56,9 +57,9 @@ module Stretcher
56
57
  @server.bulk body, options
57
58
  end
58
59
 
59
- # Creates the index, with the supplied hash as the optinos body (usually mappings: and settings:))
60
- def create(options={})
61
- request(:put, "/", {}, options)
60
+ # Creates the index, with the supplied hash as the options body (usually mappings: and settings:))
61
+ def create(options=nil)
62
+ request(:put, nil, nil, options)
62
63
  end
63
64
 
64
65
  # Deletes the index
@@ -94,7 +95,7 @@ module Stretcher
94
95
  rescue Stretcher::RequestError::NotFound
95
96
  false
96
97
  end
97
-
98
+
98
99
  # Delete documents by a given query.
99
100
  # Per: http://www.elasticsearch.org/guide/reference/api/delete-by-query.html
100
101
  def delete_query(query)
@@ -30,26 +30,20 @@ module Stretcher
30
30
  if uri_components.user || uri_components.password
31
31
  http.basic_auth(uri_components.user, uri_components.password)
32
32
  end
33
-
33
+
34
34
  http
35
35
  end
36
-
36
+
37
37
  # Internal use only.
38
38
  # Builds a logger when initializing an instance
39
39
  def self.build_logger(options)
40
- logger = nil
41
-
42
- if options[:logger]
43
- logger = options[:logger]
44
- else
45
- logger = Logger.new(STDOUT)
46
- logger.level = Logger::WARN
47
- end
40
+ logger = options[:logger] || Logger.new(STDOUT)
41
+ log_level = options[:log_level] || :debug
42
+ logger.level = Logger.const_get(log_level.to_s.upcase)
48
43
 
49
44
  logger.formatter = proc do |severity, datetime, progname, msg|
50
45
  "[Stretcher][#{severity}]: #{msg}\n"
51
46
  end
52
-
53
47
  logger
54
48
  end
55
49
 
@@ -66,7 +60,7 @@ module Stretcher
66
60
  # The options hash takes an optional instance of Logger under :logger.
67
61
  #
68
62
  # server = Stretcher::Server.new('http://localhost:9200')
69
- #
63
+ #
70
64
  # The default implementation here uses the net_http_persistent adapter
71
65
  # for faraday. If you would like to use a different HTTP library, or alter
72
66
  # other faraday config settings you may specify an optional :faraday_configurator
@@ -92,7 +86,7 @@ module Stretcher
92
86
  idx = Index.new(self, name, :logger => logger)
93
87
  block ? block.call(idx) : idx
94
88
  end
95
-
89
+
96
90
  # Perform a raw bulk operation. You probably want to use Stretcher::Index#bulk_index
97
91
  # which properly formats a bulk index request.
98
92
  def bulk(data, options={})
@@ -128,7 +122,7 @@ module Stretcher
128
122
  def msearch(body=[])
129
123
  raise ArgumentError, "msearch takes an array!" unless body.is_a?(Array)
130
124
  fmt_body = body.map(&:to_json).join("\n") + "\n"
131
-
125
+
132
126
  res = request(:get, path_uri("/_msearch"), {}, fmt_body)
133
127
 
134
128
  errors = res.responses.map(&:error).compact
@@ -181,12 +175,12 @@ module Stretcher
181
175
  end
182
176
 
183
177
  # Handy way to query the server, returning *only* the body
184
- # Will raise an exception when the status is not in the 2xx range
178
+ # Will raise an exception when the status is not in the 2xx range
185
179
  def request(method, path, params={}, body=nil, headers={}, &block)
186
180
  req = http.build_request(method)
187
181
  req.path = path
188
182
  req.params.update(Util.clean_params(params)) if params
189
- req.body = body if body
183
+ req.body = body
190
184
  req.headers.update(headers) if headers
191
185
  block.call(req) if block
192
186
  logger.debug { Util.curl_format(req) }
@@ -1,3 +1,3 @@
1
1
  module Stretcher
2
- VERSION = "1.14.0"
2
+ VERSION = "1.15.0"
3
3
  end
@@ -23,10 +23,11 @@ describe Stretcher::Index do
23
23
  i
24
24
  }
25
25
  let(:corpus) {
26
+ # underscore field that are not system fields should make it through to _source
26
27
  [
27
- {:text => "Foo", "_type" => 'tweet', "_id" => 'fooid'},
28
- {:text => "Bar", "_type" => 'tweet', "_id" => 'barid'},
29
- {:text => "Baz", "_type" => 'tweet', "id" => 'bazid'} # Note we support both _id and id
28
+ {:text => "Foo", :_text => '_Foo', "_type" => 'tweet', "_id" => 'fooid'},
29
+ {:text => "Bar", :_text => '_Bar', "_type" => 'tweet', "_id" => 'barid'},
30
+ {:text => "Baz", :_text => '_Baz', "_type" => 'tweet', "id" => 'bazid'} # Note we support both _id and id
30
31
  ]
31
32
  }
32
33
 
@@ -41,6 +42,18 @@ describe Stretcher::Index do
41
42
  index.refresh
42
43
  end
43
44
 
45
+ it 'creates an index with the correct HTTP command' do
46
+ index.delete rescue nil
47
+
48
+ options = { :mappings => { :movie => { :properties => { :category => { :type => 'string' } } } } }
49
+
50
+ server.logger.should_receive(:debug) do |&block|
51
+ block.call.should == %{curl -XPUT http://localhost:9200/foo -d '#{options.to_json}' '-H Accept: application/json' '-H Content-Type: application/json' '-H User-Agent: Stretcher Ruby Gem 1.14.0'}
52
+ end
53
+
54
+ index.create(options)
55
+ end
56
+
44
57
  it "should work on an existential level" do
45
58
  index.delete rescue nil
46
59
  index.exists?.should be_false
@@ -81,6 +94,7 @@ describe Stretcher::Index do
81
94
  corpus.each {|doc|
82
95
  fetched_doc = index.type(doc["_type"]).get(doc["_id"] || doc["id"], {}, true)
83
96
  fetched_doc._source.text.should == doc[:text]
97
+ fetched_doc._source._text.should == doc[:_text]
84
98
  fetched_doc._source._id.should be_nil
85
99
  fetched_doc._source._type.should be_nil
86
100
  }
@@ -7,6 +7,11 @@ describe Stretcher::Server do
7
7
  server.class.should == Stretcher::Server
8
8
  end
9
9
 
10
+ it 'sets log level from options' do
11
+ server = Stretcher::Server.new(ES_URL, :log_level => :info)
12
+ server.logger.level.should == Logger::INFO
13
+ end
14
+
10
15
  it "should support the block friendly 'with_server'" do
11
16
  exposed = nil
12
17
  res = Stretcher::Server.with_server() {|s|
@@ -80,4 +85,5 @@ describe Stretcher::Server do
80
85
  end
81
86
  server.analyze("Candles", :analyzer => :snowball)
82
87
  end
88
+
83
89
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,5 @@ require 'stretcher'
6
6
 
7
7
  File.open("test_logs", 'wb') {|f| f.write("")}
8
8
  DEBUG_LOGGER = Logger.new('test_logs')
9
- DEBUG_LOGGER.level = Logger::DEBUG
10
9
  ES_URL = 'http://localhost:9200'
11
10
  require File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib stretcher]))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stretcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.0
4
+ version: 1.15.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-13 00:00:00.000000000 Z
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday