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')
]
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.



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

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

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
end

#tokenizerObject

Returns the value of attribute tokenizer.



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

def tokenizer
  @tokenizer
end

Instance Method Details

#add_filter(filter) ⇒ Object

Add a filter for text field tokenization



82
83
84
85
86
87
88
89
# File 'lib/sunspot/schema.rb', line 82

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



57
58
59
60
61
62
63
64
65
# File 'lib/sunspot/schema.rb', line 57

def dynamic_fields
  fields = []
  for field_variants in variant_combinations
    for type in FIELD_TYPES
      fields << DynamicField.new(type, field_variants)
    end
  end
  fields
end

#to_xmlObject

Return an XML representation of this schema using the Haml template



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/sunspot/schema.rb', line 94

def to_xml
  template = File.read(
    File.join(
      File.dirname(__FILE__),
      '..',
      '..',
      'templates',
      'schema.xml.haml'
    )
  )
  engine = Haml::Engine.new(template)
  engine.render(Object.new, :schema => self)
end

#typesObject

Attribute field types defined in the schema



50
51
52
# File 'lib/sunspot/schema.rb', line 50

def types
  FIELD_TYPES
end