Class: Sunspot::Search::Hit

Inherits:
Object
  • Object
show all
Defined in:
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 geographical distance (for geographical search).

Constant Summary collapse

SPECIAL_KEYS =

:nodoc:

Set.new(%w(id type score))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_hit, highlights, distance, search) ⇒ Hit

:nodoc:



33
34
35
36
37
38
39
40
41
# File 'lib/sunspot/search/hit.rb', line 33

def initialize(raw_hit, highlights, distance, search) #:nodoc:
  @class_name, @primary_key = *raw_hit['id'].match(/([^ ]+) (.+)/)[1..2]
  @score = raw_hit['score']
  @distance = distance
  @search = search
  @stored_values = raw_hit
  @stored_cache = {}
  @highlights = highlights
end

Instance Attribute Details

#class_nameObject (readonly)

Class name of object associated with this hit, as string.



19
20
21
# File 'lib/sunspot/search/hit.rb', line 19

def class_name
  @class_name
end

#distanceObject (readonly)

For geographical searches, this is the distance between the search centerpoint and the document’s location. Otherwise, it’s nil.



29
30
31
# File 'lib/sunspot/search/hit.rb', line 29

def distance
  @distance
end

#primary_keyObject (readonly)

Primary key of object associated with this hit, as string.



15
16
17
# File 'lib/sunspot/search/hit.rb', line 15

def primary_key
  @primary_key
end

#resultObject 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.



93
94
95
96
97
# File 'lib/sunspot/search/hit.rb', line 93

def result
  return @result if defined?(@result)
  @search.populate_hits
  @result
end

#scoreObject (readonly)

Keyword relevance score associated with this result. Nil if this hit is not from a keyword search.



24
25
26
# File 'lib/sunspot/search/hit.rb', line 24

def score
  @score
end

Instance Method Details

#highlight(field_name) ⇒ Object

Return the first highlight found for a given field, or nil if there is none.



59
60
61
# File 'lib/sunspot/search/hit.rb', line 59

def highlight(field_name)
  highlights(field_name).first
end

#highlights(field_name = nil) ⇒ Object

Returns all highlights for this hit when called without parameters. When a field_name is provided, returns only the highlight for this field.



47
48
49
50
51
52
53
# File 'lib/sunspot/search/hit.rb', line 47

def highlights(field_name = nil)
  if field_name.nil?
    highlights_cache.values.flatten 
  else
    highlights_cache[field_name.to_sym]
  end || []
end

#inspectObject

:nodoc:



100
101
102
# File 'lib/sunspot/search/hit.rb', line 100

def inspect #:nodoc:
  "#<Sunspot::Search::Hit:#{@class_name} #{@primary_key}>"
end

#stored(field_name, dynamic_field_name = nil) ⇒ Object

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<Symbol>

The name of the field for which to retrieve the stored value.

dynamic_field_name<Symbol>

If you want to access a stored dynamic field, this should be the dynamic component of the field name.



77
78
79
80
81
82
83
84
85
86
# File 'lib/sunspot/search/hit.rb', line 77

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