Class: Sunspot::Search::Hit
- Inherits:
-
Object
- Object
- Sunspot::Search::Hit
- Defined in:
- sunspot/lib/sunspot/search/hit.rb
Overview
Hit objects represent the raw information returned by Solr for a single document. As well as the primary key and class name, hit objects give access to stored field values, keyword relevance score, and keyword highlighting.
Constant Summary
- SPECIAL_KEYS =
:nodoc:
Set.new(%w(id type score))
Instance Attribute Summary (collapse)
-
- (Object) class_name
readonly
Class name of object associated with this hit, as string.
-
- (Object) primary_key
readonly
Primary key of object associated with this hit, as string.
-
- (Object) result
(also: #instance)
Retrieve the instance associated with this hit.
-
- (Object) score
readonly
Keyword relevance score associated with this result.
Instance Method Summary (collapse)
-
- (Object) highlight(field_name)
Return the first highlight found for a given field, or nil if there is none.
-
- (Object) highlights(field_name = nil)
Returns all highlights for this hit when called without parameters.
-
- (Hit) initialize(raw_hit, highlights, search)
constructor
:nodoc:.
-
- (Object) inspect
:nodoc:.
-
- (Object) stored(field_name, dynamic_field_name = nil)
Retrieve stored field value.
-
- (Object) to_param
Returns the instance primary key when the Hit is used to generate urls For example, using a search that stores the :name attribute:.
Constructor Details
- (Hit) initialize(raw_hit, highlights, search)
:nodoc:
29 30 31 32 33 34 35 36 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 29 def initialize(raw_hit, highlights, search) #:nodoc: @class_name, @primary_key = *raw_hit['id'].match(/([^ ]+) (.+)/)[1..2] @score = raw_hit['score'] @search = search @stored_values = raw_hit @stored_cache = {} @highlights = highlights end |
Instance Attribute Details
- (Object) class_name (readonly)
Class name of object associated with this hit, as string.
19 20 21 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 19 def class_name @class_name end |
- (Object) primary_key (readonly)
Primary key of object associated with this hit, as string.
15 16 17 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 15 def primary_key @primary_key end |
- (Object) result Also known as: instance
Retrieve the instance associated with this hit. This is lazy-loaded, but the first time it is called on any hit, all the hits for the search will load their instances using the adapter’s #load_all method.
88 89 90 91 92 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 88 def result return @result if defined?(@result) @search.populate_hits @result end |
- (Object) score (readonly)
Keyword relevance score associated with this result. Nil if this hit is not from a keyword search.
24 25 26 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 24 def score @score end |
Instance Method Details
- (Object) highlight(field_name)
Return the first highlight found for a given field, or nil if there is none.
54 55 56 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 54 def highlight(field_name) highlights(field_name).first end |
- (Object) highlights(field_name = nil)
Returns all highlights for this hit when called without parameters. When a field_name is provided, returns only the highlight for this field.
42 43 44 45 46 47 48 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 42 def highlights(field_name = nil) if field_name.nil? highlights_cache.values.flatten else highlights_cache[field_name.to_sym] end || [] end |
- (Object) inspect
:nodoc:
95 96 97 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 95 def inspect #:nodoc: "#<Sunspot::Search::Hit:#{@class_name} #{@primary_key}>" end |
- (Object) stored(field_name, dynamic_field_name = nil)
Retrieve stored field value. For any attribute field configured with :stored => true, the Hit object will contain the stored value for that field. The value of this field will be typecast according to the type of the field.
Parameters
field_name | The name of the field for which to retrieve the stored value. |
dynamic_field_name | If you want to access a stored dynamic field, this should be the dynamic component of the field name. |
72 73 74 75 76 77 78 79 80 81 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 72 def stored(field_name, dynamic_field_name = nil) field_key = if dynamic_field_name [field_name.to_sym, dynamic_field_name.to_sym] else field_name.to_sym end return @stored_cache[field_key] if @stored_cache.has_key?(field_key) @stored_cache[field_key] = stored_value(field_name, dynamic_field_name) end |
- (Object) to_param
Returns the instance primary key when the Hit is used to generate urls For example, using a search that stores the :name attribute:
hits = Sunspot.search(Object) do ... hits.each do |hit| link_to hit.stored(:name), edit_object_path(hit) end
109 110 111 |
# File 'sunspot/lib/sunspot/search/hit.rb', line 109 def to_param self.primary_key end |