erichummel-sunspot 1.2.1 → 2.0.0.pre.111215

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.gitignore +0 -1
  2. data/Gemfile +2 -1
  3. data/History.txt +30 -0
  4. data/Rakefile +5 -9
  5. data/lib/sunspot.rb +13 -3
  6. data/lib/sunspot/batcher.rb +62 -0
  7. data/lib/sunspot/class_set.rb +23 -0
  8. data/lib/sunspot/configuration.rb +7 -0
  9. data/lib/sunspot/dsl.rb +1 -1
  10. data/lib/sunspot/dsl/field_group.rb +57 -0
  11. data/lib/sunspot/dsl/field_query.rb +48 -0
  12. data/lib/sunspot/dsl/function.rb +13 -0
  13. data/lib/sunspot/dsl/paginatable.rb +5 -1
  14. data/lib/sunspot/dsl/restriction_with_near.rb +39 -0
  15. data/lib/sunspot/dsl/scope.rb +4 -4
  16. data/lib/sunspot/dsl/search.rb +2 -2
  17. data/lib/sunspot/dsl/standard_query.rb +2 -0
  18. data/lib/sunspot/indexer.rb +12 -7
  19. data/lib/sunspot/query.rb +3 -3
  20. data/lib/sunspot/query/bbox.rb +15 -0
  21. data/lib/sunspot/query/common_query.rb +13 -2
  22. data/lib/sunspot/query/dismax.rb +5 -1
  23. data/lib/sunspot/query/field_group.rb +36 -0
  24. data/lib/sunspot/query/geofilt.rb +16 -0
  25. data/lib/sunspot/query/highlighting.rb +8 -1
  26. data/lib/sunspot/query/pagination.rb +8 -4
  27. data/lib/sunspot/query/sort.rb +14 -0
  28. data/lib/sunspot/query/sort_composite.rb +3 -2
  29. data/lib/sunspot/search.rb +1 -1
  30. data/lib/sunspot/search/abstract_search.rb +53 -65
  31. data/lib/sunspot/search/field_group.rb +32 -0
  32. data/lib/sunspot/search/group.rb +50 -0
  33. data/lib/sunspot/search/hit.rb +21 -7
  34. data/lib/sunspot/search/hit_enumerable.rb +72 -0
  35. data/lib/sunspot/search/paginated_collection.rb +5 -3
  36. data/lib/sunspot/session.rb +3 -1
  37. data/lib/sunspot/type.rb +21 -0
  38. data/lib/sunspot/util.rb +9 -0
  39. data/lib/sunspot/version.rb +1 -1
  40. data/spec/api/batcher_spec.rb +112 -0
  41. data/spec/api/class_set_spec.rb +24 -0
  42. data/spec/api/hit_enumerable_spec.rb +47 -0
  43. data/spec/api/indexer/batch_spec.rb +29 -3
  44. data/spec/api/query/function_spec.rb +9 -0
  45. data/spec/api/query/group_spec.rb +32 -0
  46. data/spec/api/query/highlighting_examples.rb +22 -0
  47. data/spec/api/query/ordering_pagination_examples.rb +21 -0
  48. data/spec/api/query/spatial_examples.rb +27 -0
  49. data/spec/api/query/standard_spec.rb +1 -0
  50. data/spec/api/search/hits_spec.rb +11 -0
  51. data/spec/api/search/paginated_collection_spec.rb +10 -0
  52. data/spec/api/search/results_spec.rb +6 -0
  53. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +0 -11
  54. data/spec/api/session_spec.rb +12 -0
  55. data/spec/api/sunspot_spec.rb +11 -0
  56. data/spec/helpers/indexer_helper.rb +0 -12
  57. data/spec/helpers/integration_helper.rb +8 -0
  58. data/spec/helpers/mock_session_helper.rb +13 -0
  59. data/spec/helpers/query_helper.rb +0 -12
  60. data/spec/helpers/search_helper.rb +0 -12
  61. data/spec/integration/dynamic_fields_spec.rb +2 -0
  62. data/spec/integration/faceting_spec.rb +14 -1
  63. data/spec/integration/field_grouping_spec.rb +66 -0
  64. data/spec/integration/geospatial_spec.rb +85 -0
  65. data/spec/integration/highlighting_spec.rb +22 -0
  66. data/spec/integration/indexing_spec.rb +23 -1
  67. data/spec/integration/keyword_search_spec.rb +1 -1
  68. data/spec/integration/local_search_spec.rb +1 -1
  69. data/spec/integration/more_like_this_spec.rb +1 -1
  70. data/spec/integration/scoped_search_spec.rb +1 -1
  71. data/spec/integration/stored_fields_spec.rb +2 -0
  72. data/spec/integration/test_pagination.rb +13 -2
  73. data/spec/integration/unicode_spec.rb +15 -0
  74. data/spec/mocks/connection.rb +4 -4
  75. data/spec/mocks/post.rb +1 -0
  76. data/spec/spec_helper.rb +21 -11
  77. data/sunspot.gemspec +42 -0
  78. data/tasks/rdoc.rake +2 -2
  79. metadata +95 -135
  80. data/VERSION.yml +0 -4
  81. data/bin/sunspot-installer +0 -19
  82. data/bin/sunspot-solr +0 -74
  83. data/installer/config/schema.yml +0 -95
  84. data/lib/sunspot/installer.rb +0 -31
  85. data/lib/sunspot/installer/library_installer.rb +0 -45
  86. data/lib/sunspot/installer/schema_builder.rb +0 -219
  87. data/lib/sunspot/installer/solrconfig_updater.rb +0 -76
  88. data/lib/sunspot/installer/task_helper.rb +0 -18
  89. data/lib/sunspot/server.rb +0 -152
  90. data/solr-1.3/etc/jetty.xml +0 -212
  91. data/solr-1.3/etc/webdefault.xml +0 -379
  92. data/solr-1.3/lib/jetty-6.1.3.jar +0 -0
  93. data/solr-1.3/lib/jetty-util-6.1.3.jar +0 -0
  94. data/solr-1.3/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  95. data/solr-1.3/lib/jsp-2.1/core-3.1.1.jar +0 -0
  96. data/solr-1.3/lib/jsp-2.1/jsp-2.1.jar +0 -0
  97. data/solr-1.3/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  98. data/solr-1.3/lib/servlet-api-2.5-6.1.3.jar +0 -0
  99. data/solr-1.3/solr/conf/elevate.xml +0 -36
  100. data/solr-1.3/solr/conf/protwords.txt +0 -21
  101. data/solr-1.3/solr/conf/schema.xml +0 -64
  102. data/solr-1.3/solr/conf/solrconfig.xml +0 -725
  103. data/solr-1.3/solr/conf/stopwords.txt +0 -57
  104. data/solr-1.3/solr/conf/synonyms.txt +0 -31
  105. data/solr-1.3/solr/lib/geoapi-nogenerics-2.1-M2.jar +0 -0
  106. data/solr-1.3/solr/lib/gt2-referencing-2.3.1.jar +0 -0
  107. data/solr-1.3/solr/lib/jsr108-0.01.jar +0 -0
  108. data/solr-1.3/solr/lib/locallucene.jar +0 -0
  109. data/solr-1.3/solr/lib/localsolr.jar +0 -0
  110. data/solr-1.3/start.jar +0 -0
  111. data/solr-1.3/webapps/solr.war +0 -0
  112. data/solr/README.txt +0 -42
  113. data/solr/etc/jetty.xml +0 -218
  114. data/solr/etc/webdefault.xml +0 -379
  115. data/solr/lib/jetty-6.1.3.jar +0 -0
  116. data/solr/lib/jetty-util-6.1.3.jar +0 -0
  117. data/solr/lib/jsp-2.1/ant-1.6.5.jar +0 -0
  118. data/solr/lib/jsp-2.1/core-3.1.1.jar +0 -0
  119. data/solr/lib/jsp-2.1/jsp-2.1.jar +0 -0
  120. data/solr/lib/jsp-2.1/jsp-api-2.1.jar +0 -0
  121. data/solr/lib/servlet-api-2.5-6.1.3.jar +0 -0
  122. data/solr/solr/.gitignore +0 -1
  123. data/solr/solr/README.txt +0 -54
  124. data/solr/solr/conf/admin-extra.html +0 -31
  125. data/solr/solr/conf/elevate.xml +0 -36
  126. data/solr/solr/conf/mapping-ISOLatin1Accent.txt +0 -246
  127. data/solr/solr/conf/protwords.txt +0 -21
  128. data/solr/solr/conf/schema.xml +0 -238
  129. data/solr/solr/conf/scripts.conf +0 -24
  130. data/solr/solr/conf/solrconfig.xml +0 -934
  131. data/solr/solr/conf/spellings.txt +0 -2
  132. data/solr/solr/conf/stopwords.txt +0 -58
  133. data/solr/solr/conf/synonyms.txt +0 -31
  134. data/solr/solr/conf/xslt/example.xsl +0 -132
  135. data/solr/solr/conf/xslt/example_atom.xsl +0 -67
  136. data/solr/solr/conf/xslt/example_rss.xsl +0 -66
  137. data/solr/solr/conf/xslt/luke.xsl +0 -337
  138. data/solr/start.jar +0 -0
  139. data/solr/webapps/solr.war +0 -0
  140. data/spec/api/server_spec.rb +0 -91
  141. data/spec/integration/spec_helper.rb +0 -7
@@ -0,0 +1,32 @@
1
+ module Sunspot
2
+ module Search
3
+ class FieldGroup
4
+ def initialize(field, search, options) #:nodoc:
5
+ @field, @search, @options = field, search, options
6
+ end
7
+
8
+ def groups
9
+ @groups ||=
10
+ begin
11
+ if solr_response
12
+ solr_response['groups'].map do |group|
13
+ Group.new(group['groupValue'], group['doclist'], @search)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ def matches
20
+ if solr_response
21
+ solr_response['matches'].to_i
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def solr_response
28
+ @search.group_response[@field.indexed_name.to_s]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,50 @@
1
+ require 'sunspot/search/hit_enumerable'
2
+
3
+ module Sunspot
4
+ module Search
5
+ class Group
6
+ attr_reader :value
7
+
8
+ include HitEnumerable
9
+
10
+ def initialize(value, doclist, search)
11
+ @value, @doclist, @search = value, doclist, search
12
+ end
13
+
14
+ def hits(options = {})
15
+ if options[:verify]
16
+ super
17
+ else
18
+ @hits ||= super
19
+ end
20
+ end
21
+
22
+ def verified_hits
23
+ @verified_hits ||= super
24
+ end
25
+
26
+ def results
27
+ @results ||= verified_hits.map { |hit| hit.instance }
28
+ end
29
+
30
+ def highlights_for(doc)
31
+ @search.highlights_for(doc)
32
+ end
33
+
34
+ def solr_docs
35
+ @doclist['docs']
36
+ end
37
+
38
+ #
39
+ # The total number of documents matching the query for this group
40
+ #
41
+ # ==== Returns
42
+ #
43
+ # Integer:: Total matching documents
44
+ #
45
+ def total
46
+ @doclist['numFound']
47
+ end
48
+ end
49
+ end
50
+ end
@@ -96,6 +96,20 @@ module Sunspot
96
96
  "#<Sunspot::Search::Hit:#{@class_name} #{@primary_key}>"
97
97
  end
98
98
 
99
+ #
100
+ # Returns the instance primary key when the Hit is used to generate urls
101
+ # For example, using a search that stores the :name attribute:
102
+ #
103
+ # hits = Sunspot.search(Object) do ...
104
+ #
105
+ # hits.each do |hit|
106
+ # link_to hit.stored(:name), edit_object_path(hit)
107
+ # end
108
+ #
109
+ def to_param
110
+ self.primary_key
111
+ end
112
+
99
113
  private
100
114
 
101
115
  def setup
@@ -120,15 +134,15 @@ module Sunspot
120
134
 
121
135
  def stored_value(field_name, dynamic_field_name)
122
136
  setup.stored_fields(field_name, dynamic_field_name).each do |field|
123
- if value = @stored_values[field.indexed_name]
124
- case value
125
- when Array
126
- return value.map { |item| field.cast(item) }
127
- else
128
- return field.cast(value)
129
- end
137
+ value = @stored_values[field.indexed_name]
138
+
139
+ if Array === value
140
+ return value.map { |item| field.cast(item) }
141
+ elsif !value.nil?
142
+ return field.cast(value)
130
143
  end
131
144
  end
145
+
132
146
  nil
133
147
  end
134
148
  end
@@ -0,0 +1,72 @@
1
+ module Sunspot
2
+ module Search
3
+ module HitEnumerable #:nodoc:
4
+ def hits(options = {})
5
+ if options[:verify]
6
+ verified_hits
7
+ elsif solr_docs
8
+ solr_docs.map { |d| Hit.new(d, highlights_for(d), self) }
9
+ else
10
+ []
11
+ end
12
+ end
13
+
14
+ def verified_hits
15
+ hits.select { |h| h.result }
16
+ end
17
+
18
+ #
19
+ # Populate the Hit objects with their instances. This is invoked the first
20
+ # time any hit has its instance requested, and all hits are loaded as a
21
+ # batch.
22
+ #
23
+ def populate_hits #:nodoc:
24
+ id_hit_hash = Hash.new { |h, k| h[k] = {} }
25
+ hits.each do |hit|
26
+ id_hit_hash[hit.class_name][hit.primary_key] = hit
27
+ end
28
+ id_hit_hash.each_pair do |class_name, hits|
29
+ ids = hits.map { |id, hit| hit.primary_key }
30
+ data_accessor = data_accessor_for(Util.full_const_get(class_name))
31
+ hits_for_class = id_hit_hash[class_name]
32
+ data_accessor.load_all(ids).each do |result|
33
+ hit = hits_for_class.delete(Adapters::InstanceAdapter.adapt(result).id.to_s)
34
+ hit.result = result
35
+ end
36
+ hits_for_class.values.each { |hit| hit.result = nil }
37
+ end
38
+ end
39
+
40
+ #
41
+ # Convenience method to iterate over hit and result objects. Block is
42
+ # yielded a Sunspot::Server::Hit instance and a Sunspot::Server::Result
43
+ # instance.
44
+ #
45
+ # Note that this method iterates over verified hits (see #hits method
46
+ # for more information).
47
+ #
48
+ def each_hit_with_result
49
+ verified_hits.each do |hit|
50
+ yield(hit, hit.result)
51
+ end
52
+ end
53
+
54
+ #
55
+ # Get the data accessor that will be used to load a particular class out of
56
+ # persistent storage. Data accessors can implement any methods that may be
57
+ # useful for refining how data is loaded out of storage. When building a
58
+ # search manually (e.g., using the Sunspot#new_search method), this should
59
+ # be used before calling #execute(). Use the
60
+ # Sunspot::DSL::Search#data_accessor_for method when building searches using
61
+ # the block DSL.
62
+ #
63
+ def data_accessor_for(clazz) #:nodoc:
64
+ # FIXME: This method does not belong here, but I was getting bogged
65
+ # down trying to figure out where it should go. Punted for now.
66
+ # - AL 27 Nov 2011
67
+ (@data_accessors ||= {})[clazz.name.to_sym] ||=
68
+ Adapters::DataAccessor.create(clazz)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -4,8 +4,10 @@ module Sunspot
4
4
  class PaginatedCollection
5
5
  instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval|object_id/ }
6
6
 
7
- attr_reader :total_count, :current_page, :per_page
7
+ attr_reader :current_page, :per_page
8
+ attr_accessor :total_count
8
9
  alias :total_entries :total_count
10
+ alias :total_entries= :total_count=
9
11
  alias :limit_value :per_page
10
12
 
11
13
  def initialize(collection, page, per_page, total)
@@ -34,12 +36,12 @@ module Sunspot
34
36
 
35
37
  def next_page
36
38
  current_page < total_pages ? (current_page + 1) : nil
37
- end
39
+ end
38
40
 
39
41
  def out_of_bounds?
40
42
  current_page > total_pages
41
43
  end
42
-
44
+
43
45
  def offset
44
46
  (current_page - 1) * per_page
45
47
  end
@@ -239,7 +239,9 @@ module Sunspot
239
239
  #
240
240
  def connection
241
241
  @connection ||=
242
- self.class.connection_class.connect(:url => config.solr.url)
242
+ self.class.connection_class.connect(:url => config.solr.url,
243
+ :read_timeout => config.solr.read_timeout,
244
+ :open_timeout => config.solr.open_timeout)
243
245
  end
244
246
 
245
247
  def indexer
@@ -354,6 +354,27 @@ module Sunspot
354
354
  end
355
355
  end
356
356
 
357
+ #
358
+ # The Latlon type encodes geographical coordinates in the native
359
+ # Solr LatLonType.
360
+ #
361
+ # The data for this type must respond to the `lat` and `lng` methods; you
362
+ # can use Sunspot::Util::Coordinates as a wrapper if your source data does
363
+ # not follow this API.
364
+ #
365
+ # Location fields can be used with the geospatial DSL. See the
366
+ # Geospatial section of the README for examples.
367
+ #
368
+ class LatlonType < AbstractType
369
+ def indexed_name(name)
370
+ "#{name}_ll"
371
+ end
372
+
373
+ def to_indexed(value)
374
+ "#{value.lat.to_f},#{value.lng.to_f}"
375
+ end
376
+ end
377
+
357
378
  class ClassType < AbstractType
358
379
  def indexed_name(name) #:nodoc:
359
380
  'class_name'
@@ -227,7 +227,16 @@ module Sunspot
227
227
  @__calling_context__.__send__(:id)
228
228
  end
229
229
 
230
+ # Special case due to `Kernel#sub`'s existence
231
+ def sub(*args, &block)
232
+ __proxy_method__(:sub, *args, &block)
233
+ end
234
+
230
235
  def method_missing(method, *args, &block)
236
+ __proxy_method__(method, *args, &block)
237
+ end
238
+
239
+ def __proxy_method__(method, *args, &block)
231
240
  begin
232
241
  @__receiver__.__send__(method.to_sym, *args, &block)
233
242
  rescue ::NoMethodError => e
@@ -1,3 +1,3 @@
1
1
  module Sunspot
2
- VERSION = '1.2.1'
2
+ VERSION = '2.0.0.pre.111215'
3
3
  end
@@ -0,0 +1,112 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Sunspot::Batcher do
4
+ it "includes Enumerable" do
5
+ described_class.should include Enumerable
6
+ end
7
+
8
+ describe "#each" do
9
+ let(:current) { [:foo, :bar] }
10
+ before { subject.stub(:current).and_return current }
11
+
12
+ it "iterates over current" do
13
+ yielded_values = []
14
+
15
+ subject.each do |value|
16
+ yielded_values << value
17
+ end
18
+
19
+ yielded_values.should eq current
20
+ end
21
+ end
22
+
23
+ describe "adding to current batch" do
24
+ it "#push pushes to current" do
25
+ subject.push :foo
26
+ subject.current.should include :foo
27
+ end
28
+
29
+ it "#<< pushes to current" do
30
+ subject.push :foo
31
+ subject.current.should include :foo
32
+ end
33
+
34
+ it "#concat concatinates on current batch" do
35
+ subject << :foo
36
+ subject.concat [:bar, :mix]
37
+ should include :foo, :bar, :mix
38
+ end
39
+ end
40
+
41
+
42
+ describe "#current" do
43
+ context "no current" do
44
+ it "starts a new" do
45
+ expect { subject.current }.to change(subject, :depth).by 1
46
+ end
47
+
48
+ it "is empty by default" do
49
+ subject.current.should be_empty
50
+ end
51
+ end
52
+
53
+ context "with a current" do
54
+ before { subject.start_new }
55
+
56
+ it "does not start a new" do
57
+ expect { subject.current }.to_not change(subject, :depth)
58
+ end
59
+
60
+ it "returns the same as last time" do
61
+ subject.current.should eq subject.current
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#start_new" do
67
+ it "creates a new batches" do
68
+ expect { 2.times { subject.start_new } }.to change(subject, :depth).by 2
69
+ end
70
+
71
+ it "changes current" do
72
+ subject << :foo
73
+ subject.start_new
74
+ should_not include :foo
75
+ end
76
+ end
77
+
78
+ describe "#end_current" do
79
+ context "no current batch" do
80
+ it "fails" do
81
+ expect { subject.end_current }.to raise_error Sunspot::Batcher::NoCurrentBatchError
82
+ end
83
+ end
84
+
85
+ context "with current batch" do
86
+ before { subject.start_new }
87
+
88
+ it "changes current" do
89
+ subject << :foo
90
+ subject.end_current
91
+ should_not include :foo
92
+ end
93
+
94
+ it "returns current" do
95
+ subject << :foo
96
+ subject.end_current.should include :foo
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "#batching?" do
102
+ it "is false when depth is 0" do
103
+ subject.should_receive(:depth).and_return 0
104
+ should_not be_batching
105
+ end
106
+
107
+ it "is true when depth is more than 0" do
108
+ subject.should_receive(:depth).and_return 1
109
+ should be_batching
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,24 @@
1
+ require "spec_helper"
2
+
3
+ describe Sunspot::ClassSet do
4
+ it "is enumerable" do
5
+ class1, class2 = stub(:name => "Class1"), stub(:name => "Class2")
6
+
7
+ set = described_class.new
8
+ set << class1 << class2
9
+
10
+ set.to_a.should =~ [class1, class2]
11
+ end
12
+
13
+ it "replaces classes with the same name" do
14
+ set = described_class.new
15
+
16
+ class1 = stub(:name => "Class1")
17
+ set << class1
18
+ set.to_a.should == [class1]
19
+
20
+ class1_dup = stub(:name => "Class1")
21
+ set << class1_dup
22
+ set.to_a.should == [class1_dup]
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Sunspot::Search::HitEnumerable do
4
+ subject do
5
+ Class.new do
6
+ include Sunspot::Search::HitEnumerable
7
+ end.new
8
+ end
9
+
10
+ describe "#hits" do
11
+ before do
12
+ subject.stub(:solr_docs).and_return([{"id" => "Post 1", "score" => 3.14}])
13
+ subject.stub(:highlights_for)
14
+ end
15
+
16
+ it "retrieves the raw Solr response from #solr_docs and constructs Hit objects" do
17
+ Sunspot::Search::Hit.should_receive(:new).
18
+ with({"id" => "Post 1", "score" => 3.14}, anything, anything)
19
+
20
+ subject.hits
21
+ end
22
+
23
+ it "constructs Hit objects with highlights" do
24
+ subject.should_receive(:highlights_for).with({"id" => "Post 1", "score" => 3.14})
25
+
26
+ subject.hits
27
+ end
28
+
29
+ it "returns only verified hits if :verify => true is passed" do
30
+ Sunspot::Search::Hit.any_instance.stub(:result).and_return(nil)
31
+
32
+ subject.hits(:verify => true).should be_empty
33
+ end
34
+
35
+ it "returns an empty array if no results are available from Solr" do
36
+ subject.stub(:solr_docs).and_return(nil)
37
+
38
+ subject.hits.should == []
39
+ end
40
+
41
+ it "provides #populate_hits so that querying for one hit result will eager load the rest" do
42
+ Sunspot::Search::Hit.any_instance.should_receive(:result=)
43
+
44
+ subject.populate_hits
45
+ end
46
+ end
47
+ end