sunspot 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. data/README.rdoc +3 -0
  2. data/TODO +6 -5
  3. data/bin/sunspot-solr +4 -0
  4. data/installer/config/schema.yml +24 -0
  5. data/lib/sunspot/composite_setup.rb +14 -0
  6. data/lib/sunspot/dsl/adjustable.rb +47 -0
  7. data/lib/sunspot/dsl/fulltext.rb +23 -8
  8. data/lib/sunspot/dsl/function.rb +14 -0
  9. data/lib/sunspot/dsl/functional.rb +41 -0
  10. data/lib/sunspot/dsl/more_like_this_query.rb +56 -0
  11. data/lib/sunspot/dsl/paginatable.rb +28 -0
  12. data/lib/sunspot/dsl/search.rb +1 -1
  13. data/lib/sunspot/dsl/{query.rb → standard_query.rb} +4 -49
  14. data/lib/sunspot/dsl.rb +3 -2
  15. data/lib/sunspot/field.rb +16 -2
  16. data/lib/sunspot/indexer.rb +1 -1
  17. data/lib/sunspot/installer/schema_builder.rb +1 -1
  18. data/lib/sunspot/installer/solrconfig_updater.rb +13 -0
  19. data/lib/sunspot/installer/task_helper.rb +1 -1
  20. data/lib/sunspot/query/abstract_field_facet.rb +5 -0
  21. data/lib/sunspot/query/boost_query.rb +5 -1
  22. data/lib/sunspot/query/{query.rb → common_query.rb} +26 -20
  23. data/lib/sunspot/query/composite_fulltext.rb +31 -0
  24. data/lib/sunspot/query/dismax.rb +45 -6
  25. data/lib/sunspot/query/function_query.rb +52 -0
  26. data/lib/sunspot/query/more_like_this.rb +60 -0
  27. data/lib/sunspot/query/more_like_this_query.rb +12 -0
  28. data/lib/sunspot/query/standard_query.rb +20 -0
  29. data/lib/sunspot/query/text_field_boost.rb +2 -0
  30. data/lib/sunspot/query.rb +3 -2
  31. data/lib/sunspot/search/abstract_search.rb +302 -0
  32. data/lib/sunspot/search/date_facet.rb +1 -1
  33. data/lib/sunspot/search/facet_row.rb +1 -1
  34. data/lib/sunspot/search/field_facet.rb +1 -1
  35. data/lib/sunspot/search/highlight.rb +1 -1
  36. data/lib/sunspot/search/hit.rb +1 -1
  37. data/lib/sunspot/search/more_like_this_search.rb +31 -0
  38. data/lib/sunspot/search/query_facet.rb +1 -1
  39. data/lib/sunspot/search/standard_search.rb +21 -0
  40. data/lib/sunspot/search.rb +3 -288
  41. data/lib/sunspot/server.rb +8 -4
  42. data/lib/sunspot/session.rb +30 -2
  43. data/lib/sunspot/session_proxy/master_slave_session_proxy.rb +1 -1
  44. data/lib/sunspot/session_proxy/sharding_session_proxy.rb +9 -0
  45. data/lib/sunspot/session_proxy/thread_local_session_proxy.rb +9 -2
  46. data/lib/sunspot/setup.rb +32 -3
  47. data/lib/sunspot/type.rb +74 -0
  48. data/lib/sunspot/util.rb +3 -2
  49. data/lib/sunspot/version.rb +1 -1
  50. data/lib/sunspot.rb +9 -1
  51. data/solr/solr/conf/schema.xml +12 -0
  52. data/solr/solr/conf/solrconfig.xml +6 -0
  53. data/spec/api/indexer/attributes_spec.rb +9 -3
  54. data/spec/api/indexer/fulltext_spec.rb +2 -2
  55. data/spec/api/indexer/removal_spec.rb +1 -1
  56. data/spec/api/query/advanced_manipulation_examples.rb +35 -0
  57. data/spec/api/query/{connectives_spec.rb → connectives_examples.rb} +19 -19
  58. data/spec/api/query/{dynamic_fields_spec.rb → dynamic_fields_examples.rb} +33 -17
  59. data/spec/api/query/{faceting_spec.rb → faceting_examples.rb} +146 -43
  60. data/spec/api/query/{fulltext_spec.rb → fulltext_examples.rb} +81 -47
  61. data/spec/api/query/function_spec.rb +70 -0
  62. data/spec/api/query/{highlighting_spec.rb → highlighting_examples.rb} +27 -27
  63. data/spec/api/query/{local_spec.rb → local_examples.rb} +5 -5
  64. data/spec/api/query/more_like_this_spec.rb +140 -0
  65. data/spec/api/query/{ordering_pagination_spec.rb → ordering_pagination_examples.rb} +16 -16
  66. data/spec/api/query/{scope_spec.rb → scope_examples.rb} +44 -61
  67. data/spec/api/query/standard_spec.rb +28 -0
  68. data/spec/api/query/{text_field_scoping_spec.rb → text_field_scoping_examples.rb} +5 -5
  69. data/spec/api/search/dynamic_fields_spec.rb +6 -0
  70. data/spec/api/search/faceting_spec.rb +10 -10
  71. data/spec/api/search/hits_spec.rb +1 -1
  72. data/spec/api/search/results_spec.rb +10 -0
  73. data/spec/api/server_spec.rb +6 -0
  74. data/spec/api/session_proxy/master_slave_session_proxy_spec.rb +2 -2
  75. data/spec/api/session_proxy/thread_local_session_proxy_spec.rb +17 -0
  76. data/spec/api/spec_helper.rb +2 -0
  77. data/spec/helpers/query_helper.rb +25 -0
  78. data/spec/helpers/search_helper.rb +4 -0
  79. data/spec/integration/faceting_spec.rb +8 -0
  80. data/spec/integration/keyword_search_spec.rb +75 -3
  81. data/spec/integration/local_search_spec.rb +1 -1
  82. data/spec/integration/more_like_this_spec.rb +43 -0
  83. data/spec/mocks/comment.rb +1 -1
  84. data/spec/mocks/connection.rb +27 -12
  85. data/spec/mocks/post.rb +5 -4
  86. data/spec/spec_helper.rb +4 -21
  87. data/tasks/gemspec.rake +1 -1
  88. metadata +39 -27
  89. data/spec/api/query/adjust_params_spec.rb +0 -37
  90. data/spec/api/query/facet_local_params_spec.rb +0 -103
@@ -1,12 +1,10 @@
1
1
  module Sunspot
2
- module Query
3
- class Query
4
- attr_accessor :scope, :fulltext, :parameter_adjustment
5
-
2
+ module Query #:nodoc:
3
+ class CommonQuery
6
4
  def initialize(types)
7
5
  @scope = Scope.new
8
6
  @sort = SortComposite.new
9
- @components = []
7
+ @components = [@scope, @sort]
10
8
  if types.length == 1
11
9
  @scope.add_restriction(TypeField.instance, Restriction::EqualTo, types.first)
12
10
  else
@@ -14,18 +12,10 @@ module Sunspot
14
12
  end
15
13
  end
16
14
 
17
- def set_fulltext(keywords)
18
- @fulltext = Dismax.new(keywords)
19
- end
20
-
21
- def set_solr_parameter_adjustment( block )
15
+ def solr_parameter_adjustment=(block)
22
16
  @parameter_adjustment = block
23
17
  end
24
18
 
25
- def add_location_restriction(coordinates, radius)
26
- @local = Local.new(coordinates, radius)
27
- end
28
-
29
19
  def add_sort(sort)
30
20
  @sort << sort
31
21
  end
@@ -40,21 +30,22 @@ module Sunspot
40
30
  facet
41
31
  end
42
32
 
33
+ def add_function(function)
34
+ @components << function
35
+ function
36
+ end
37
+
43
38
  def paginate(page, per_page)
44
39
  if @pagination
45
40
  @pagination.page = page
46
41
  @pagination.per_page = per_page
47
42
  else
48
- @pagination = Pagination.new(page, per_page)
43
+ @components << @pagination = Pagination.new(page, per_page)
49
44
  end
50
45
  end
51
46
 
52
47
  def to_params
53
- params = @scope.to_params
54
- Sunspot::Util.deep_merge!(params, @fulltext.to_params) if @fulltext
55
- Sunspot::Util.deep_merge!(params, @sort.to_params)
56
- Sunspot::Util.deep_merge!(params, @pagination.to_params) if @pagination
57
- Sunspot::Util.deep_merge!(params, @local.to_params) if @local
48
+ params = {}
58
49
  @components.each do |component|
59
50
  Sunspot::Util.deep_merge!(params, component.to_params)
60
51
  end
@@ -74,6 +65,21 @@ module Sunspot
74
65
  def per_page
75
66
  @pagination.per_page if @pagination
76
67
  end
68
+
69
+
70
+ private
71
+
72
+ #
73
+ # If we have a single fulltext query, merge is normally. If there are
74
+ # multiple nested queries, serialize them as `_query_` subqueries.
75
+ #
76
+ def merge_fulltext(params)
77
+ return nil if @fulltexts.empty?
78
+ return Sunspot::Util.deep_merge!(params, @fulltexts.first.to_params) if @fulltexts.length == 1
79
+ subqueries = @fulltexts.map {|fulltext| fulltext.to_subquery }.join(' ')
80
+ Sunspot::Util.deep_merge!(params, {:q => subqueries})
81
+ end
82
+
77
83
  end
78
84
  end
79
85
  end
@@ -0,0 +1,31 @@
1
+ module Sunspot
2
+ module Query
3
+ class CompositeFulltext
4
+ def initialize
5
+ @components = []
6
+ end
7
+
8
+ def add(keywords)
9
+ @components << dismax = Dismax.new(keywords)
10
+ dismax
11
+ end
12
+
13
+ def to_params
14
+ case @components.length
15
+ when 0
16
+ {}
17
+ when 1
18
+ @components.first.to_params
19
+ else
20
+ to_subqueries
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def to_subqueries
27
+ { :q => @components.map { |dismax| dismax.to_subquery }.join(' ') }
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,11 @@
1
1
  module Sunspot
2
2
  module Query
3
+
4
+ #
5
+ # Solr full-text queries use Solr's DisMaxRequestHandler, a search handler
6
+ # designed to process user-entered phrases, and search for individual
7
+ # words across a union of several fields.
8
+ #
3
9
  class Dismax
4
10
  attr_writer :minimum_match, :phrase_slop, :query_phrase_slop, :tie
5
11
 
@@ -7,10 +13,11 @@ module Sunspot
7
13
  @keywords = keywords
8
14
  @fulltext_fields = {}
9
15
  @boost_queries = []
16
+ @boost_functions = []
10
17
  @highlights = []
11
18
  end
12
19
 
13
- #
20
+ #
14
21
  # The query as Solr parameters
15
22
  #
16
23
  def to_params
@@ -26,6 +33,11 @@ module Sunspot
26
33
  boost_query.to_boolean_phrase
27
34
  end
28
35
  end
36
+ unless @boost_functions.empty?
37
+ params[:bf] = @boost_functions.map do |boost_function|
38
+ boost_function.to_s
39
+ end
40
+ end
29
41
  if @minimum_match
30
42
  params[:mm] = @minimum_match
31
43
  end
@@ -44,7 +56,18 @@ module Sunspot
44
56
  params
45
57
  end
46
58
 
47
- #
59
+ #
60
+ # Serialize the query as a Solr nested subquery.
61
+ #
62
+ def to_subquery
63
+ params = self.to_params
64
+ params.delete :defType
65
+ keywords = params.delete(:q)
66
+ options = params.map { |key, value| "#{key}='#{escape_quotes(value)}'"}.join(' ')
67
+ "_query_:\"{!dismax #{options}}#{escape_quotes(keywords)}\""
68
+ end
69
+
70
+ #
48
71
  # Assign a new boost query and return it.
49
72
  #
50
73
  def create_boost_query(factor)
@@ -53,21 +76,28 @@ module Sunspot
53
76
  end
54
77
 
55
78
  #
56
- # Add a fulltext field to be searched, with optional boost
79
+ # Add a boost function
80
+ #
81
+ def add_boost_function(function_query)
82
+ @boost_functions << function_query
83
+ end
84
+
85
+ #
86
+ # Add a fulltext field to be searched, with optional boost.
57
87
  #
58
88
  def add_fulltext_field(field, boost = nil)
59
89
  @fulltext_fields[field.indexed_name] = TextFieldBoost.new(field, boost)
60
90
  end
61
91
 
62
92
  #
63
- # Add a phrase field for extra boost
93
+ # Add a phrase field for extra boost.
64
94
  #
65
95
  def add_phrase_field(field, boost = nil)
66
96
  @phrase_fields ||= []
67
97
  @phrase_fields << TextFieldBoost.new(field, boost)
68
98
  end
69
99
 
70
- #
100
+ #
71
101
  # Set highlighting options for the query. If fields is empty, the
72
102
  # Highlighting object won't pass field names at all, which means
73
103
  # the dismax's :qf parameter will be used by Solr.
@@ -76,13 +106,22 @@ module Sunspot
76
106
  @highlights << Highlighting.new(fields, options)
77
107
  end
78
108
 
79
- #
109
+ #
80
110
  # Determine if a given field is being searched. Used by DSL to avoid
81
111
  # overwriting boost parameters when injecting defaults.
82
112
  #
83
113
  def has_fulltext_field?(field)
84
114
  @fulltext_fields.has_key?(field.indexed_name)
85
115
  end
116
+
117
+
118
+ private
119
+
120
+ def escape_quotes(value)
121
+ return value unless value.is_a? String
122
+ value.gsub(/(['"])/, '\\\\\1')
123
+ end
124
+
86
125
  end
87
126
  end
88
127
  end
@@ -0,0 +1,52 @@
1
+ module Sunspot
2
+ module Query
3
+ #
4
+ # Abstract class for function queries.
5
+ #
6
+ class FunctionQuery
7
+ include RSolr::Char
8
+ end
9
+
10
+ #
11
+ # Function query which represents a constant.
12
+ #
13
+ class ConstantFunctionQuery < FunctionQuery
14
+ def initialize(constant)
15
+ @constant = constant
16
+ end
17
+
18
+ def to_s
19
+ Type.to_literal(@constant)
20
+ end
21
+ end
22
+
23
+ #
24
+ # Function query which represents a field.
25
+ #
26
+ class FieldFunctionQuery < FunctionQuery
27
+ def initialize(field)
28
+ @field = field
29
+ end
30
+
31
+ def to_s
32
+ "#{escape(@field.indexed_name)}"
33
+ end
34
+ end
35
+
36
+ #
37
+ # Function query which represents an actual function invocation.
38
+ # Takes a function name and arguments as parameters.
39
+ # Arguments are in turn FunctionQuery objects.
40
+ #
41
+ class FunctionalFunctionQuery < FunctionQuery
42
+ def initialize(function_name, function_args)
43
+ @function_name, @function_args = function_name, function_args
44
+ end
45
+
46
+ def to_s
47
+ params = @function_args.map { |arg| arg.to_s }.join(",")
48
+ "#{@function_name}(#{params})"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,60 @@
1
+ module Sunspot
2
+ module Query
3
+ class MoreLikeThis
4
+ attr_reader :fields
5
+
6
+ def initialize(document)
7
+ @document_scope = Restriction::EqualTo.new(
8
+ IdField.instance,
9
+ Adapters::InstanceAdapter.adapt(document).index_id
10
+ )
11
+ @fields = {}
12
+ @params = {}
13
+ end
14
+
15
+ def add_field(field, boost = nil)
16
+ raise(ArgumentError, "Field #{field.name} is not set up for more_like_this") unless field.more_like_this?
17
+ @fields[field.indexed_name] = TextFieldBoost.new(field, boost)
18
+ end
19
+
20
+ def minimum_term_frequency=(mintf)
21
+ @params[:"mlt.mintf"] = mintf
22
+ end
23
+
24
+ def minimum_document_frequency=(mindf)
25
+ @params[:"mlt.mindf"] = mindf
26
+ end
27
+
28
+ def minimum_word_length=(minwl)
29
+ @params[:"mlt.minwl"] = minwl
30
+ end
31
+
32
+ def maximum_word_length=(maxwl)
33
+ @params[:"mlt.maxwl"] = maxwl
34
+ end
35
+
36
+ def maximum_query_terms=(maxqt)
37
+ @params[:"mlt.maxqt"] = maxqt
38
+ end
39
+
40
+ def boost_by_relevance=(should_boost)
41
+ @params[:"mlt.boost"] = should_boost
42
+ end
43
+
44
+ def to_params
45
+ params = Sunspot::Util.deep_merge(
46
+ @params,
47
+ :q => @document_scope.to_boolean_phrase
48
+ )
49
+ params[:"mlt.fl"] = @fields.keys.join(",")
50
+ boosted_fields = @fields.values.select { |field| field.boost }
51
+ unless boosted_fields.empty?
52
+ params[:qf] = boosted_fields.map do |field|
53
+ field.to_boosted_field
54
+ end.join(' ')
55
+ end
56
+ params
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ module Sunspot
2
+ module Query
3
+ class MoreLikeThisQuery < CommonQuery
4
+ attr_accessor :scope, :more_like_this
5
+
6
+ def initialize(document, types)
7
+ super(types)
8
+ @components << @more_like_this = MoreLikeThis.new(document)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,20 @@
1
+ module Sunspot
2
+ module Query
3
+ class StandardQuery < CommonQuery
4
+ attr_accessor :scope, :fulltext
5
+
6
+ def initialize(types)
7
+ super
8
+ @components << @fulltext = CompositeFulltext.new
9
+ end
10
+
11
+ def add_fulltext(keywords)
12
+ @fulltext.add(keywords)
13
+ end
14
+
15
+ def add_location_restriction(coordinates, radius)
16
+ @components << @local = Local.new(coordinates, radius)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,6 +1,8 @@
1
1
  module Sunspot
2
2
  module Query
3
3
  class TextFieldBoost #:nodoc:
4
+ attr_reader :boost
5
+
4
6
  def initialize(field, boost = nil)
5
7
  @field, @boost = field, boost
6
8
  end
data/lib/sunspot/query.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  %w(filter abstract_field_facet connective boost_query date_field_facet dismax
2
- field_facet highlighting local pagination restriction query
3
- query_facet scope sort sort_composite text_field_boost).each do |file|
2
+ field_facet highlighting local pagination restriction common_query
3
+ standard_query more_like_this more_like_this_query query_facet scope sort
4
+ sort_composite text_field_boost function_query composite_fulltext).each do |file|
4
5
  require(File.join(File.dirname(__FILE__), 'query', file))
5
6
  end
6
7
  module Sunspot
@@ -0,0 +1,302 @@
1
+ module Sunspot
2
+ module Search #:nodoc:
3
+ #
4
+ # This class encapsulates the results of a Solr search. It provides access
5
+ # to search results, total result count, facets, and pagination information.
6
+ # Instances of Search are returned by the Sunspot.search and
7
+ # Sunspot.new_search methods.
8
+ #
9
+ class AbstractSearch
10
+ #
11
+ # Retrieve all facet objects defined for this search, in order they were
12
+ # defined. To retrieve an individual facet by name, use #facet()
13
+ #
14
+ attr_reader :facets
15
+ attr_reader :query #:nodoc:
16
+ attr_accessor :request_handler
17
+
18
+ def initialize(connection, setup, query, configuration) #:nodoc:
19
+ @connection, @setup, @query = connection, setup, query
20
+ @query.paginate(1, configuration.pagination.default_per_page)
21
+ @facets = []
22
+ @facets_by_name = {}
23
+ end
24
+
25
+ #
26
+ # Execute the search on the Solr instance and store the results. If you
27
+ # use Sunspot#search() to construct your searches, there is no need to call
28
+ # this method as it has already been called. If you use
29
+ # Sunspot#new_search(), you will need to call this method after building the
30
+ # query.
31
+ #
32
+ def execute
33
+ reset
34
+ params = @query.to_params
35
+ @solr_result = @connection.request("/#{request_handler}", params)
36
+ self
37
+ end
38
+
39
+ def execute! #:nodoc: deprecated
40
+ execute
41
+ end
42
+
43
+ #
44
+ # Get the collection of results as instantiated objects. If WillPaginate is
45
+ # available, the results will be a WillPaginate::Collection instance; if
46
+ # not, it will be a vanilla Array.
47
+ #
48
+ # If not all of the results referenced by the Solr hits actually exist in
49
+ # the data store, Sunspot will only return the results that do exist.
50
+ #
51
+ # ==== Returns
52
+ #
53
+ # WillPaginate::Collection or Array:: Instantiated result objects
54
+ #
55
+ def results
56
+ @results ||= maybe_will_paginate(verified_hits.map { |hit| hit.instance })
57
+ end
58
+
59
+ #
60
+ # Access raw Solr result information. Returns a collection of Hit objects
61
+ # that contain the class name, primary key, keyword relevance score (if
62
+ # applicable), and any stored fields.
63
+ #
64
+ # ==== Options (options)
65
+ #
66
+ # :verify::
67
+ # Only return hits that reference objects that actually exist in the data
68
+ # store. This causes results to be eager-loaded from the data store,
69
+ # unlike the normal behavior of this method, which only loads the
70
+ # referenced results when Hit#result is first called.
71
+ #
72
+ # ==== Returns
73
+ #
74
+ # Array:: Ordered collection of Hit objects
75
+ #
76
+ def hits(options = {})
77
+ if options[:verify]
78
+ verified_hits
79
+ else
80
+ @hits ||=
81
+ begin
82
+ hits = if solr_response && solr_response['docs']
83
+ solr_response['docs'].map do |doc|
84
+ Hit.new(doc, highlights_for(doc), distance_for(doc), self)
85
+ end
86
+ end
87
+ maybe_will_paginate(hits || [])
88
+ end
89
+ end
90
+ end
91
+ alias_method :raw_results, :hits
92
+
93
+ #
94
+ # Convenience method to iterate over hit and result objects. Block is
95
+ # yielded a Sunspot::Server::Hit instance and a Sunspot::Server::Result
96
+ # instance.
97
+ #
98
+ # Note that this method iterates over verified hits (see #hits method
99
+ # for more information).
100
+ #
101
+ def each_hit_with_result
102
+ verified_hits.each do |hit|
103
+ yield(hit, hit.result)
104
+ end
105
+ end
106
+
107
+ #
108
+ # The total number of documents matching the query parameters
109
+ #
110
+ # ==== Returns
111
+ #
112
+ # Integer:: Total matching documents
113
+ #
114
+ def total
115
+ @total ||= solr_response['numFound'] || 0
116
+ end
117
+
118
+ #
119
+ # Get the facet object for the given name. `name` can either be the name
120
+ # given to a query facet, or the field name of a field facet. Returns a
121
+ # Sunspot::Facet object.
122
+ #
123
+ # ==== Parameters
124
+ #
125
+ # name<Symbol>::
126
+ # Name of the field to return the facet for, or the name given to the
127
+ # query facet when the search was constructed.
128
+ # dynamic_name<Symbol>::
129
+ # If faceting on a dynamic field, this is the dynamic portion of the field
130
+ # name.
131
+ #
132
+ # ==== Example:
133
+ #
134
+ # search = Sunspot.search(Post) do
135
+ # facet :category_ids
136
+ # dynamic :custom do
137
+ # facet :cuisine
138
+ # end
139
+ # facet :age do
140
+ # row 'Less than a month' do
141
+ # with(:published_at).greater_than(1.month.ago)
142
+ # end
143
+ # row 'Less than a year' do
144
+ # with(:published_at, 1.year.ago..1.month.ago)
145
+ # end
146
+ # row 'More than a year' do
147
+ # with(:published_at).less_than(1.year.ago)
148
+ # end
149
+ # end
150
+ # end
151
+ # search.facet(:category_ids)
152
+ # #=> Facet for :category_ids field
153
+ # search.facet(:custom, :cuisine)
154
+ # #=> Facet for the dynamic field :cuisine in the :custom field definition
155
+ # search.facet(:age)
156
+ # #=> Facet for the query facet named :age
157
+ #
158
+ def facet(name, dynamic_name = nil)
159
+ if name
160
+ if dynamic_name
161
+ @facets_by_name[:"#{name}:#{dynamic_name}"]
162
+ else
163
+ @facets_by_name[name.to_sym]
164
+ end
165
+ end
166
+ end
167
+
168
+ #
169
+ # Deprecated in favor of optional second argument to #facet
170
+ #
171
+ def dynamic_facet(base_name, dynamic_name) #:nodoc:
172
+ facet(base_name, dynamic_name)
173
+ end
174
+
175
+ def facet_response #:nodoc:
176
+ @solr_result['facet_counts']
177
+ end
178
+
179
+ #
180
+ # Get the data accessor that will be used to load a particular class out of
181
+ # persistent storage. Data accessors can implement any methods that may be
182
+ # useful for refining how data is loaded out of storage. When building a
183
+ # search manually (e.g., using the Sunspot#new_search method), this should
184
+ # be used before calling #execute(). Use the
185
+ # Sunspot::DSL::Search#data_accessor_for method when building searches using
186
+ # the block DSL.
187
+ #
188
+ def data_accessor_for(clazz) #:nodoc:
189
+ (@data_accessors ||= {})[clazz.name.to_sym] ||=
190
+ Adapters::DataAccessor.create(clazz)
191
+ end
192
+
193
+ #
194
+ # Build this search using a DSL block. This method can be called more than
195
+ # once on an unexecuted search (e.g., Sunspot.new_search) in order to build
196
+ # a search incrementally.
197
+ #
198
+ # === Example
199
+ #
200
+ # search = Sunspot.new_search(Post)
201
+ # search.build do
202
+ # with(:published_at).less_than Time.now
203
+ # end
204
+ # search.execute
205
+ #
206
+ def build(&block)
207
+ Util.instance_eval_or_call(dsl, &block)
208
+ self
209
+ end
210
+
211
+ #
212
+ # Populate the Hit objects with their instances. This is invoked the first
213
+ # time any hit has its instance requested, and all hits are loaded as a
214
+ # batch.
215
+ #
216
+ def populate_hits #:nodoc:
217
+ id_hit_hash = Hash.new { |h, k| h[k] = {} }
218
+ hits.each do |hit|
219
+ id_hit_hash[hit.class_name][hit.primary_key] = hit
220
+ end
221
+ id_hit_hash.each_pair do |class_name, hits|
222
+ ids = hits.map { |id, hit| hit.primary_key }
223
+ data_accessor = data_accessor_for(Util.full_const_get(class_name))
224
+ hits_for_class = id_hit_hash[class_name]
225
+ data_accessor.load_all(ids).each do |result|
226
+ hit = hits_for_class.delete(Adapters::InstanceAdapter.adapt(result).id.to_s)
227
+ hit.result = result
228
+ end
229
+ hits_for_class.values.each { |hit| hit.result = nil }
230
+ end
231
+ end
232
+
233
+ def inspect #:nodoc:
234
+ "<Sunspot::Search:#{query.to_params.inspect}>"
235
+ end
236
+
237
+ def add_field_facet(field, options = {}) #:nodoc:
238
+ name = (options[:name] || field.name)
239
+ add_facet(name, FieldFacet.new(field, self, options))
240
+ end
241
+
242
+ def add_query_facet(name, options) #:nodoc:
243
+ add_facet(name, QueryFacet.new(name, self, options))
244
+ end
245
+
246
+ def add_date_facet(field, options) #:nodoc:
247
+ name = (options[:name] || field.name)
248
+ add_facet(name, DateFacet.new(field, self, options))
249
+ end
250
+
251
+ private
252
+
253
+ def dsl
254
+ raise NotImplementedError
255
+ end
256
+
257
+ def execute_request(params)
258
+ raise NotImplementedError
259
+ end
260
+
261
+ def solr_response
262
+ @solr_response ||= @solr_result['response'] || {}
263
+ end
264
+
265
+ def highlights_for(doc)
266
+ if @solr_result['highlighting']
267
+ @solr_result['highlighting'][doc['id']]
268
+ end
269
+ end
270
+
271
+ def distance_for(doc)
272
+ if @solr_result['distances']
273
+ @solr_result['distances'][doc['id']]
274
+ end
275
+ end
276
+
277
+ def verified_hits
278
+ @verified_hits ||= maybe_will_paginate(hits.select { |hit| hit.instance })
279
+ end
280
+
281
+ def maybe_will_paginate(collection)
282
+ if defined?(WillPaginate::Collection)
283
+ WillPaginate::Collection.create(@query.page, @query.per_page, total) do |pager|
284
+ pager.replace(collection)
285
+ end
286
+ else
287
+ collection
288
+ end
289
+ end
290
+
291
+ def add_facet(name, facet)
292
+ @facets << facet
293
+ @facets_by_name[name.to_sym] = facet
294
+ end
295
+
296
+ # Clear out all the cached ivars so the search can be called again.
297
+ def reset
298
+ @results = @hits = @verified_hits = @total = @solr_response = @doc_ids = nil
299
+ end
300
+ end
301
+ end
302
+ end
@@ -1,5 +1,5 @@
1
1
  module Sunspot
2
- class Search
2
+ module Search
3
3
  class DateFacet
4
4
  def initialize(field, search, options)
5
5
  @field, @search, @options = field, search, options