Class: Sunspot::Setup
- Inherits:
-
Object
- Object
- Sunspot::Setup
- Defined in:
- lib/sunspot/setup.rb
Overview
This class encapsulates the search/indexing setup for a given class. Its contents are built using the Sunspot.setup method.
Class Method Summary collapse
-
.for(clazz) ⇒ Object
Retrieve the setup instance for the given class, or for the nearest ancestor that has a setup, if any.
-
.setup(clazz, &block) ⇒ Object
Retrieve or create the Setup instance for the given class, evaluating the given block to add to the setup’s configuration.
Instance Method Summary collapse
-
#add_document_boost(attr_name, &block) ⇒ Object
Add a document boost to documents at index time.
-
#add_dynamic_field_factory(name, type, options = {}, &block) ⇒ Object
Add dynamic field_factories.
-
#add_field_factory(name, type, options = {}, &block) ⇒ Object
Add field_factories for scope/ordering.
-
#add_text_field_factory(name, options = {}, &block) ⇒ Object
Add field_factories for fulltext search.
-
#all_field_factories ⇒ Object
Get all static, dynamic, and text field_factories associated with this setup as well as all inherited field_factories.
-
#all_text_fields ⇒ Object
Return all text fields.
-
#clazz ⇒ Object
Return the class associated with this setup.
-
#document_boost_for(model) ⇒ Object
Get the document boost for a given model.
-
#dynamic_field_factories ⇒ Object
Get all dynamic field_factories for this and parent setups.
-
#dynamic_field_factory(field_name) ⇒ Object
Return the DynamicFieldFactory with the given base name.
-
#field(field_name) ⇒ Object
Return the Field with the given (public-facing) name.
-
#field_factories ⇒ Object
Get the field_factories associated with this setup as well as all inherited field_factories.
-
#fields ⇒ Object
Return all attribute fields.
-
#initialize(clazz) ⇒ Setup
constructor
:nodoc:.
-
#set_coordinates_field(name) ⇒ Object
The coordinates field factory is used for populating the coordinate fields of documents during index, but does not actually generate fields (since the field names used in search are static).
-
#setup(&block) ⇒ Object
Builder method for evaluating the setup DSL.
-
#text_field_factories ⇒ Object
Get the text field_factories associated with this setup as well as all inherited text field_factories.
-
#text_fields(field_name) ⇒ Object
Return one or more text fields with the given public-facing name.
- #type_names ⇒ Object
Constructor Details
#initialize(clazz) ⇒ Setup
:nodoc:
7 8 9 10 11 12 13 14 15 |
# File 'lib/sunspot/setup.rb', line 7 def initialize(clazz) @clazz = clazz @class_name = clazz.name @field_factories, @text_field_factories, @dynamic_field_factories, @field_factories_cache, @text_field_factories_cache, @dynamic_field_factories_cache = *Array.new(6) { Hash.new } @dsl = DSL::Fields.new(self) add_field_factory(:class, Type::ClassType) end |
Class Method Details
.for(clazz) ⇒ Object
Retrieve the setup instance for the given class, or for the nearest ancestor that has a setup, if any.
Parameters
- clazz<Class>
-
Class for which to retrieve a setup
Returns
- Sunspot::Setup
-
Setup instance associated with the given class or its nearest ancestor
268 269 270 271 272 273 274 275 276 |
# File 'lib/sunspot/setup.rb', line 268 def for(clazz) #:nodoc: class_name = if clazz.respond_to?(:name) clazz.name else clazz end setups[class_name.to_sym] || self.for(clazz.superclass) if clazz end |
.setup(clazz, &block) ⇒ Object
Retrieve or create the Setup instance for the given class, evaluating the given block to add to the setup’s configuration
251 252 253 |
# File 'lib/sunspot/setup.rb', line 251 def setup(clazz, &block) #:nodoc: self.for!(clazz).setup(&block) end |
Instance Method Details
#add_document_boost(attr_name, &block) ⇒ Object
Add a document boost to documents at index time. Document boost can be static (the same for all documents of this class), or extracted on a per- document basis using either attribute or block extraction as per usual.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sunspot/setup.rb', line 74 def add_document_boost(attr_name, &block) @document_boost_extractor = if attr_name if attr_name.respond_to?(:to_f) DataExtractor::Constant.new(attr_name) else DataExtractor::AttributeExtractor.new(attr_name) end else DataExtractor::BlockExtractor.new(&block) end end |
#add_dynamic_field_factory(name, type, options = {}, &block) ⇒ Object
Add dynamic field_factories
Parameters
- field_factories<Array>
-
Array of dynamic field objects
54 55 56 57 58 |
# File 'lib/sunspot/setup.rb', line 54 def add_dynamic_field_factory(name, type, = {}, &block) field_factory = FieldFactory::Dynamic.new(name, type, , &block) @dynamic_field_factories[field_factory.signature] = field_factory @dynamic_field_factories_cache[field_factory.name] = field_factory end |
#add_field_factory(name, type, options = {}, &block) ⇒ Object
Add field_factories for scope/ordering
Parameters
- field_factories<Array>
-
Array of Sunspot::Field objects
28 29 30 31 32 |
# File 'lib/sunspot/setup.rb', line 28 def add_field_factory(name, type, = {}, &block) field_factory = FieldFactory::Static.new(name, type, , &block) @field_factories[field_factory.signature] = field_factory @field_factories_cache[field_factory.name] = field_factory end |
#add_text_field_factory(name, options = {}, &block) ⇒ Object
Add field_factories for fulltext search
Parameters
- field_factories<Array>
-
Array of Sunspot::Field objects
41 42 43 44 45 |
# File 'lib/sunspot/setup.rb', line 41 def add_text_field_factory(name, = {}, &block) field_factory = FieldFactory::Static.new(name, Type::TextType, , &block) @text_field_factories[name] = field_factory @text_field_factories_cache[field_factory.name] = field_factory end |
#all_field_factories ⇒ Object
Get all static, dynamic, and text field_factories associated with this setup as well as all inherited field_factories
Returns
- Array
-
Collection of all text and scope field_factories associated with this setup
181 182 183 184 185 186 |
# File 'lib/sunspot/setup.rb', line 181 def all_field_factories all_field_factories = [] all_field_factories.concat(field_factories).concat(text_field_factories).concat(dynamic_field_factories) all_field_factories << @coordinates_field_factory if @coordinates_field_factory all_field_factories end |
#all_text_fields ⇒ Object
Return all text fields
146 147 148 |
# File 'lib/sunspot/setup.rb', line 146 def all_text_fields text_field_factories.map { |text_field_factory| text_field_factory.build } end |
#clazz ⇒ Object
Return the class associated with this setup.
Returns
- clazz<Class>
-
Class setup is configured for
206 207 208 |
# File 'lib/sunspot/setup.rb', line 206 def clazz Util.full_const_get(@class_name) end |
#document_boost_for(model) ⇒ Object
Get the document boost for a given model
213 214 215 216 217 |
# File 'lib/sunspot/setup.rb', line 213 def document_boost_for(model) if @document_boost_extractor @document_boost_extractor.value_for(model) end end |
#dynamic_field_factories ⇒ Object
Get all dynamic field_factories for this and parent setups
Returns
- Array
-
Dynamic field_factories
195 196 197 |
# File 'lib/sunspot/setup.rb', line 195 def dynamic_field_factories collection_from_inheritable_hash(:dynamic_field_factories) end |
#dynamic_field_factory(field_name) ⇒ Object
Return the DynamicFieldFactory with the given base name
129 130 131 132 133 134 |
# File 'lib/sunspot/setup.rb', line 129 def dynamic_field_factory(field_name) @dynamic_field_factories_cache[field_name.to_sym] || raise( UnrecognizedFieldError, "No dynamic field configured for #{@clazz.name} with name '#{field_name}'" ) end |
#field(field_name) ⇒ Object
Return the Field with the given (public-facing) name
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sunspot/setup.rb', line 97 def field(field_name) if field_factory = @field_factories_cache[field_name.to_sym] field_factory.build else raise( UnrecognizedFieldError, "No field configured for #{@clazz.name} with name '#{field_name}'" ) end end |
#field_factories ⇒ Object
Get the field_factories associated with this setup as well as all inherited field_factories
Returns
- Array
-
Collection of all field_factories associated with this setup
157 158 159 |
# File 'lib/sunspot/setup.rb', line 157 def field_factories collection_from_inheritable_hash(:field_factories) end |
#fields ⇒ Object
Return all attribute fields
139 140 141 |
# File 'lib/sunspot/setup.rb', line 139 def fields field_factories.map { |field_factory| field_factory.build } end |
#set_coordinates_field(name) ⇒ Object
The coordinates field factory is used for populating the coordinate fields of documents during index, but does not actually generate fields (since the field names used in search are static).
65 66 67 |
# File 'lib/sunspot/setup.rb', line 65 def set_coordinates_field(name) @coordinates_field_factory = FieldFactory::Coordinates.new(name) end |
#setup(&block) ⇒ Object
Builder method for evaluating the setup DSL
90 91 92 |
# File 'lib/sunspot/setup.rb', line 90 def setup(&block) @dsl.instance_eval(&block) end |
#text_field_factories ⇒ Object
Get the text field_factories associated with this setup as well as all inherited text field_factories
Returns
- Array
-
Collection of all text field_factories associated with this setup
169 170 171 |
# File 'lib/sunspot/setup.rb', line 169 def text_field_factories collection_from_inheritable_hash(:text_field_factories) end |
#text_fields(field_name) ⇒ Object
Return one or more text fields with the given public-facing name. This implementation will always return a single field (in an array), but CompositeSetup objects might return more than one.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/sunspot/setup.rb', line 113 def text_fields(field_name) text_field = if field_factory = @text_field_factories_cache[field_name.to_sym] field_factory.build else raise( UnrecognizedFieldError, "No text field configured for #{@clazz.name} with name '#{field_name}'" ) end [text_field] end |
#type_names ⇒ Object
17 18 19 |
# File 'lib/sunspot/setup.rb', line 17 def type_names [@class_name] end |