Class: Sunspot::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/schema.rb

Overview

Object that encapsulates schema information for building a Solr schema.xml file. This class is used by the schema:compile task as well as the sunspot-configure-solr executable.

Defined Under Namespace

Classes: DynamicField, FieldType, FieldVariant

Constant Summary collapse

DEFAULT_TOKENIZER =
'solr.StandardTokenizerFactory'
DEFAULT_FILTERS =
%w(solr.StandardFilterFactory solr.LowerCaseFilterFactory)
FIELD_TYPES =
[
  FieldType.new('boolean', 'Bool', 'b'),
  FieldType.new('sfloat', 'SortableFloat', 'f'),
  FieldType.new('date', 'Date', 'd'),
  FieldType.new('sint', 'SortableInt', 'i'),
  FieldType.new('string', 'Str', 's'),
  FieldType.new('sdouble', 'SortableDouble', 'e'),
  FieldType.new('slong', 'SortableLong', 'l')
]
FIELD_VARIANTS =
[
  FieldVariant.new('multiValued', 'm'),
  FieldVariant.new('stored', 's')
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



33
34
35
36
# File 'lib/sunspot/schema.rb', line 33

def initialize
  @tokenizer = DEFAULT_TOKENIZER
  @filters = DEFAULT_FILTERS.dup
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



31
32
33
# File 'lib/sunspot/schema.rb', line 31

def filters
  @filters
end

#tokenizerObject

Returns the value of attribute tokenizer.



31
32
33
# File 'lib/sunspot/schema.rb', line 31

def tokenizer
  @tokenizer
end

Instance Method Details

#add_filter(filter) ⇒ Object

Add a filter for text field tokenization



73
74
75
76
77
78
79
80
# File 'lib/sunspot/schema.rb', line 73

def add_filter(filter)
  @filters <<
    if filter =~ /\./
      filter
    else
      "solr.#{filter}FilterFactory"
    end
end

#dynamic_fieldsObject

DynamicField instances representing all the available types and variants



48
49
50
51
52
53
54
55
56
# File 'lib/sunspot/schema.rb', line 48

def dynamic_fields
  fields = []
  variant_combinations.each do |field_variants|
    FIELD_TYPES.each do |type|
      fields << DynamicField.new(type, field_variants)
    end
  end
  fields
end

#to_xmlObject

Return an XML representation of this schema using the ERB template



85
86
87
88
# File 'lib/sunspot/schema.rb', line 85

def to_xml
  template = File.join(File.dirname(__FILE__), '..', '..', 'templates', 'schema.xml.erb')
  ERB.new(File.read(template), nil, '-').result(binding)
end

#typesObject

Attribute field types defined in the schema



41
42
43
# File 'lib/sunspot/schema.rb', line 41

def types
  FIELD_TYPES
end