Class: Solrizer::StringDescriptor

Inherits:
Descriptor show all
Defined in:
lib/solrizer/field_mapper.rb

Overview

Maps Term names and values to Solr fields, based on the Term’s data type and any index_as options.

The basic structure of a mapper is:

Mapping on Index Type

To add a custom mapper to the default mapper

module Solrizer::DefaultDescriptors
  def self.some_field_type
    @some_field_type ||= Descriptor.new(:string, :stored, :indexed, :multivalued)
  end

  # Or some totally different field:
  def self.edible
    @some_field_type ||= EdibleDescriptor.new()
  end

  class EdibleDescriptor < Solrizer::Descriptor
    def name_and_converter(field_name, field_type)
      [field_name + '_food']
    end
  end
end

#   t.dish_name   :index_as => [:some_field_type]           -maps to->   dish_name_ssim
#   t.ingredients :index_as => [:some_field_type, :edible]  -maps to->   ingredients_ssim, ingredients_food

Custom Value Converters

All of the above applies to the generation of Solr names. Mappers can also provide custom conversion logic for the generation of Solr values by attaching a custom value converter block to a data type:

require 'time'
module Solrizer::DefaultDescriptors
  def self.searchable
    @searchable ||= SearchableDescriptor.new(:string, :stored, :indexed, :multivalued, converter: my_converter)
  end

  def self.my_converter
    lambda do |type|
      case type
      when :date
        lambda { |value| Time.parse(value).utc.to_i }
      else
        lambda { |value| value.to_s.strip }
      end
    end
  end
end

This example converts searchable dates to milliseconds, and strips extra whitespace from all other searchable data types.

ID Field

In addition to the normal field mappings, Solrizer gives special treatment to an ID field. If you want that logic (and you probably do), specify a name for this field:

Solrizer::FieldMapper.id_field  = 'id'

Extending the Default

The default mapper is Solrizer::FieldMapper. You can customize the default mapping by subclassing it. For example, to override the ID field name and the default suffix for sortable, and inherit everything else:

class CustomMapperBasedOnDefault < Solrizer::FieldMapper
  self.id_field = 'guid'

  module MyCustomIndexDescriptors
    def self.my_converter
      @my_converter ||= Descriptor.new(:string, :stored, :indexed, :multivalued)
    end
  end

  self.descriptors = [MyCustomIndexDescriptors, DefaultDescriptors]
end

Instance Attribute Summary

Attributes inherited from Descriptor

#index_type

Instance Method Summary collapse

Methods inherited from Descriptor

#evaluate_suffix, #name_and_converter, #type_required?

Constructor Details

#initialize(suffix) ⇒ StringDescriptor

Returns a new instance of StringDescriptor.



90
91
92
# File 'lib/solrizer/field_mapper.rb', line 90

def initialize(suffix)
  @suffix = suffix
end

Instance Method Details

#suffix(field_type) ⇒ Object



94
95
96
# File 'lib/solrizer/field_mapper.rb', line 94

def suffix(field_type)
  '_'+@suffix
end