supernova 0.4.14 → 0.4.15

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.14
1
+ 0.4.15
@@ -73,7 +73,21 @@ class Supernova::SolrCriteria < Supernova::Criteria
73
73
  end
74
74
 
75
75
  def fq_filter_for_key_and_value(key, value)
76
- "#{key}:#{value.is_a?(Range) ? "[#{value.first} TO #{value.last}]" : value}"
76
+ if value.nil?
77
+ "!#{key}:[* TO *]"
78
+ elsif value.is_a?(Range)
79
+ "#{key}:[#{value_for_fq_filter(value.first)} TO #{value_for_fq_filter(value.last)}]"
80
+ else
81
+ "#{key}:#{value_for_fq_filter(value)}"
82
+ end
83
+ end
84
+
85
+ def value_for_fq_filter(value)
86
+ if value.is_a?(Date)
87
+ Time.utc(value.year, value.month, value.day).iso8601
88
+ else
89
+ value
90
+ end
77
91
  end
78
92
 
79
93
  def build_docs(docs)
@@ -1,9 +1,10 @@
1
1
  require "json"
2
2
  require "fileutils"
3
+ require "time"
3
4
 
4
5
  class Supernova::SolrIndexer
5
6
  attr_accessor :options, :db, :ids, :max_rows_to_direct_index, :local_solr
6
- attr_writer :index_file_path
7
+ attr_writer :index_file_path, :debug
7
8
 
8
9
  MAX_ROWS_TO_DIRECT_INDEX = 100
9
10
 
@@ -70,6 +71,17 @@ class Supernova::SolrIndexer
70
71
  self.ids ||= :all
71
72
  end
72
73
 
74
+ def debug(message)
75
+ response = true
76
+ time = Benchmark.realtime do
77
+ response = yield if block_given?
78
+ end
79
+ if @debug == true
80
+ puts "%s: %s" % [Time.now.iso8601(3), message.gsub("%TIME%", "%.3f" % time)]
81
+ end
82
+ response
83
+ end
84
+
73
85
  def index!
74
86
  index_query(query_to_index)
75
87
  end
@@ -99,7 +111,7 @@ class Supernova::SolrIndexer
99
111
  value = hash.delete(field.to_s)
100
112
  if options[:type] == :date
101
113
  value = Time.utc(value.year, value.month, value.day) if value.is_a?(Date)
102
- value = value.utc.iso8601
114
+ value = value.utc.iso8601 if value
103
115
  end
104
116
  hash["#{field}_#{self.class.suffix_from_type(options[:type])}"] = value
105
117
  end
@@ -171,7 +183,15 @@ class Supernova::SolrIndexer
171
183
  end
172
184
 
173
185
  def query_db(query)
174
- db.send(db.respond_to?(:query) ? :query : :select_all, query)
186
+ if db.respond_to?(:query)
187
+ db.query(query, :as => :hash)
188
+ else
189
+ db.select_all(query)
190
+ end
191
+ end
192
+
193
+ def rows(query = nil)
194
+ query_db(query || query_to_index)
175
195
  end
176
196
 
177
197
  def solr_rows_to_index_for_query(query)
@@ -181,24 +201,33 @@ class Supernova::SolrIndexer
181
201
  end
182
202
 
183
203
  def index_query(query)
184
- rows = solr_rows_to_index_for_query(query)
204
+ debug "getting rows for #{query[0,100]}"
205
+ rows = debug "got all rows in %TIME%" do
206
+ solr_rows_to_index_for_query(query)
207
+ end
185
208
  if self.max_rows_to_direct_index < rows.count
186
- index_with_json_file(rows)
209
+ debug "indexed #{rows.length} rows with json in %TIME%" do
210
+ index_with_json_file(rows)
211
+ end
187
212
  else
188
- index_directly(rows)
213
+ debug "indexed #{rows.length} rows directly in %TIME%" do
214
+ index_directly(rows)
215
+ end
189
216
  end
190
217
  end
191
218
 
192
- def index_directly(rows, &block)
219
+ def index_directly(rows)
193
220
  rows.each do |row|
194
221
  row = Supernova::Solr.connection.add(row)
195
222
  end
196
223
  Supernova::Solr.connection.commit if rows.any?
197
224
  end
198
225
 
199
- def index_with_json_file(rows, &block)
200
- rows.each do |row|
201
- write_to_file(row)
226
+ def index_with_json_file(rows)
227
+ debug "wrote #{rows.count} rows to the json file in %TIME%" do
228
+ rows.each do |row|
229
+ write_to_file(row)
230
+ end
202
231
  end
203
232
  finish
204
233
  end
@@ -30,6 +30,31 @@ describe Supernova::SolrCriteria do
30
30
  end
31
31
  end
32
32
 
33
+ describe "#fq_filter_for_key_and_value" do
34
+ it "returns the correct statment for nil" do
35
+ criteria.fq_filter_for_key_and_value(:user_id, nil).should == "!user_id:[* TO *]"
36
+ end
37
+
38
+ it "returns the correct conditon for single values" do
39
+ criteria.fq_filter_for_key_and_value(:user_id, 1).should == "user_id:1"
40
+ end
41
+
42
+ it "returns the correct condition for ranges" do
43
+ criteria.fq_filter_for_key_and_value(:user_id, 1..10).should == "user_id:[1 TO 10]"
44
+ end
45
+
46
+ it "returns the correct conditon for single dates" do
47
+ criteria.fq_filter_for_key_and_value(:released_on, Date.new(1999, 1, 2)).should == "released_on:1999-01-02T00:00:00Z"
48
+ end
49
+
50
+ it "returns the correct condition for date ranges" do
51
+ date1 = Date.new(1999, 1, 2)
52
+ date2 = Date.new(1999, 2, 5)
53
+ range = Range.new(date1, date2)
54
+ criteria.fq_filter_for_key_and_value(:open_at, range).should == "open_at:[1999-01-02T00:00:00Z TO 1999-02-05T00:00:00Z]"
55
+ end
56
+ end
57
+
33
58
  describe "#to_params" do
34
59
  it "returns a Hash" do
35
60
  criteria.to_params.should be_an_instance_of(Hash)
@@ -183,7 +183,7 @@ describe Supernova::SolrIndexer do
183
183
 
184
184
  describe "#query_db" do
185
185
  it "executes the query" do
186
- db.should_receive(:query).with("query").and_return [to_index]
186
+ db.should_receive(:query).with("query", :as => :hash).and_return [to_index]
187
187
  indexer.query_db("query")
188
188
  end
189
189
 
@@ -195,6 +195,38 @@ describe Supernova::SolrIndexer do
195
195
  end
196
196
  end
197
197
 
198
+ describe "#debug" do
199
+ it "prints a line when debug is enabled" do
200
+ index = CustomSolrIndex.new(:debug => true)
201
+ index.should_receive(:puts).with(/hello world/)
202
+ index.debug "hello world"
203
+ end
204
+
205
+ it "does not print print a line when debug is not enabled" do
206
+ index = CustomSolrIndex.new(:debug => false)
207
+ index.should_not_receive(:puts)
208
+ index.debug "hello world"
209
+ end
210
+
211
+ it "can be called with block and still returns the response" do
212
+ index = CustomSolrIndex.new(:debug => true)
213
+ index.should_receive(:puts).with(/some message/)
214
+ res = index.debug "some message" do
215
+ 112
216
+ end
217
+ res.should == 112
218
+ end
219
+
220
+ it "includes the time in the debug output when placeholder found" do
221
+ index = CustomSolrIndex.new(:debug => true)
222
+ Benchmark.stub(:realtime).and_return 0.12345
223
+ index.should_receive(:puts).with(/indexed in 0.123/)
224
+ index.debug "indexed in %TIME%" do
225
+ 112
226
+ end
227
+ end
228
+ end
229
+
198
230
  describe "#map_hash_keys_to_solr" do
199
231
  class CustomSolrIndex < Supernova::SolrIndexer
200
232
  has :offer_id, :type => :integer
@@ -205,6 +237,10 @@ describe Supernova::SolrIndexer do
205
237
  has :indexed, :type => :boolean, :virtual => true
206
238
  end
207
239
 
240
+ it "sets empty dates to nil" do
241
+ CustomSolrIndex.new.map_hash_keys_to_solr("checkin_date" => nil)["checkin_date_dt"].should == nil
242
+ end
243
+
208
244
  it "maps virtual fields" do
209
245
  CustomSolrIndex.new.map_hash_keys_to_solr("indexed" => true)["indexed_b"].should == true
210
246
  end
@@ -254,6 +290,25 @@ describe Supernova::SolrIndexer do
254
290
  end
255
291
  end
256
292
 
293
+ describe "#rows" do
294
+ let(:res) { double("result") }
295
+
296
+ before(:each) do
297
+ custom_indexer.stub(:query_db).and_return([])
298
+ end
299
+
300
+ it "calls query_db with correct query" do
301
+ custom_indexer.should_receive(:query_db).with("some query").and_return res
302
+ custom_indexer.rows("some query").should == res
303
+ end
304
+
305
+ it "uses the query_to_index when query is blank" do
306
+ custom_indexer.should_receive(:query_to_index).and_return "some other query"
307
+ custom_indexer.should_receive(:query_db).with("some other query").and_return res
308
+ custom_indexer.rows.should == res
309
+ end
310
+ end
311
+
257
312
  describe "#solr_rows_to_index_for_query" do
258
313
  let(:result) {
259
314
  [
data/supernova.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{supernova}
8
- s.version = "0.4.14"
8
+ s.version = "0.4.15"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tobias Schwab"]
12
- s.date = %q{2011-07-27}
12
+ s.date = %q{2011-07-28}
13
13
  s.description = %q{Unified search scopes}
14
14
  s.email = %q{tobias.schwab@dynport.de}
15
15
  s.executables = ["start_solr"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: supernova
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 14
10
- version: 0.4.14
9
+ - 15
10
+ version: 0.4.15
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tobias Schwab
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-27 00:00:00 Z
18
+ date: 2011-07-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  requirement: &id001 !ruby/object:Gem::Requirement