class Sunspot::Search::Hit

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.

Attributes

class_name[R]

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

id_prefix[R]

ID prefix used for compositeId shard router

primary_key[R]

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

score[R]

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

Public Instance Methods

highlight(field_name) click to toggle source

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

# File lib/sunspot/search/hit.rb, line 59
def highlight(field_name)
  highlights(field_name).first
end
highlights(field_name = nil) click to toggle source

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

# 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
instance()
Alias for: result
result() click to toggle source

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.

# File lib/sunspot/search/hit.rb, line 93
def result
  return @result if defined?(@result)
  @search.populate_hits
  @result
end
Also aliased as: instance
stored(field_name, dynamic_field_name = nil) click to toggle source

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.

# 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
to_param() click to toggle source

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
# File lib/sunspot/search/hit.rb, line 114
def to_param
  self.primary_key
end

Private Instance Methods

highlights_cache() click to toggle source
# File lib/sunspot/search/hit.rb, line 124
def highlights_cache
  @highlights_cache ||=
    begin
      cache = {}
      if @highlights
        @highlights.each_pair do |indexed_field_name, highlight_strings|
          field_name = indexed_field_name.sub(/_[a-z]+$/, '').to_sym
          cache[field_name] = highlight_strings.map do |highlight_string|
            Highlight.new(field_name, highlight_string)
          end
        end
      end
      cache
    end
end
setup() click to toggle source
# File lib/sunspot/search/hit.rb, line 120
def setup
  @setup ||= Sunspot::Setup.for(Util.full_const_get(@class_name))
end
stored_value(field_name, dynamic_field_name) click to toggle source
# File lib/sunspot/search/hit.rb, line 140
def stored_value(field_name, dynamic_field_name)
  setup.stored_fields(field_name, dynamic_field_name).each do |field|
    value = @stored_values[field.indexed_name]

    if Array === value
      return value.map { |item| field.cast(item) }
    elsif !value.nil?
      return field.cast(value)
    end
  end

  nil
end