Permalink
Browse files

Move search facet accessors into AbstractSearch

  • Loading branch information...
1 parent 8a6ea3c commit 920098673f2648011102c915f44f8fd8755f5099 Mat Brown committed Mar 24, 2010
Showing with 66 additions and 70 deletions.
  1. +66 −0 sunspot/lib/sunspot/search/abstract_search.rb
  2. +0 −70 sunspot/lib/sunspot/search/standard_search.rb
@@ -7,6 +7,11 @@ module Search #:nodoc:
# Sunspot.new_search methods.
#
class AbstractSearch
+ #
+ # Retrieve all facet objects defined for this search, in order they were
+ # defined. To retrieve an individual facet by name, use #facet()
+ #
+ attr_reader :facets
attr_reader :query #:nodoc:
def initialize(connection, setup, query, configuration) #:nodoc:
@@ -107,6 +112,67 @@ def total
end
#
+ # Get the facet object for the given name. `name` can either be the name
+ # given to a query facet, or the field name of a field facet. Returns a
+ # Sunspot::Facet object.
+ #
+ # ==== Parameters
+ #
+ # name<Symbol>::
+ # Name of the field to return the facet for, or the name given to the
+ # query facet when the search was constructed.
+ # dynamic_name<Symbol>::
+ # If faceting on a dynamic field, this is the dynamic portion of the field
+ # name.
+ #
+ # ==== Example:
+ #
+ # search = Sunspot.search(Post) do
+ # facet :category_ids
+ # dynamic :custom do
+ # facet :cuisine
+ # end
+ # facet :age do
+ # row 'Less than a month' do
+ # with(:published_at).greater_than(1.month.ago)
+ # end
+ # row 'Less than a year' do
+ # with(:published_at, 1.year.ago..1.month.ago)
+ # end
+ # row 'More than a year' do
+ # with(:published_at).less_than(1.year.ago)
+ # end
+ # end
+ # end
+ # search.facet(:category_ids)
+ # #=> Facet for :category_ids field
+ # search.facet(:custom, :cuisine)
+ # #=> Facet for the dynamic field :cuisine in the :custom field definition
+ # search.facet(:age)
+ # #=> Facet for the query facet named :age
+ #
+ def facet(name, dynamic_name = nil)
+ if name
+ if dynamic_name
+ @facets_by_name[:"#{name}:#{dynamic_name}"]
+ else
+ @facets_by_name[name.to_sym]
+ end
+ end
+ end
+
+ #
+ # Deprecated in favor of optional second argument to #facet
+ #
+ def dynamic_facet(base_name, dynamic_name) #:nodoc:
+ facet(base_name, dynamic_name)
+ end
+
+ def facet_response #:nodoc:
+ @solr_result['facet_counts']
+ end
+
+ #
# Get the data accessor that will be used to load a particular class out of
# persistent storage. Data accessors can implement any methods that may be
# useful for refining how data is loaded out of storage. When building a
@@ -7,76 +7,6 @@ module Search
# Sunspot.new_search methods.
#
class StandardSearch < AbstractSearch
- #
- # Retrieve all facet objects defined for this search, in order they were
- # defined. To retrieve an individual facet by name, use #facet()
- #
- attr_reader :facets
-
- def initialize(connection, setup, query, configuration) #:nodoc:
- super(connection, setup, query, configuration)
- end
-
- #
- # Get the facet object for the given name. `name` can either be the name
- # given to a query facet, or the field name of a field facet. Returns a
- # Sunspot::Facet object.
- #
- # ==== Parameters
- #
- # name<Symbol>::
- # Name of the field to return the facet for, or the name given to the
- # query facet when the search was constructed.
- # dynamic_name<Symbol>::
- # If faceting on a dynamic field, this is the dynamic portion of the field
- # name.
- #
- # ==== Example:
- #
- # search = Sunspot.search(Post) do
- # facet :category_ids
- # dynamic :custom do
- # facet :cuisine
- # end
- # facet :age do
- # row 'Less than a month' do
- # with(:published_at).greater_than(1.month.ago)
- # end
- # row 'Less than a year' do
- # with(:published_at, 1.year.ago..1.month.ago)
- # end
- # row 'More than a year' do
- # with(:published_at).less_than(1.year.ago)
- # end
- # end
- # end
- # search.facet(:category_ids)
- # #=> Facet for :category_ids field
- # search.facet(:custom, :cuisine)
- # #=> Facet for the dynamic field :cuisine in the :custom field definition
- # search.facet(:age)
- # #=> Facet for the query facet named :age
- #
- def facet(name, dynamic_name = nil)
- if name
- if dynamic_name
- @facets_by_name[:"#{name}:#{dynamic_name}"]
- else
- @facets_by_name[name.to_sym]
- end
- end
- end
-
- #
- # Deprecated in favor of optional second argument to #facet
- #
- def dynamic_facet(base_name, dynamic_name) #:nodoc:
- facet(base_name, dynamic_name)
- end
-
- def facet_response #:nodoc:
- @solr_result['facet_counts']
- end
private

0 comments on commit 9200986

Please sign in to comment.