Class: SimpleSolrClient::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_solr/schema/matcher.rb,
lib/simple_solr/schema.rb,
lib/simple_solr/schema/field.rb,
lib/simple_solr/schema/analysis.rb,
lib/simple_solr/schema/field_type.rb,
lib/simple_solr/schema/dynamic_field.rb,
lib/simple_solr/schema/field_or_type.rb

Overview

A basic field type

We don’t even try to represent the analysis chain; just store the raw xml

We also, in blatent disregard for separation of concerns and encapsulation, put in a place to store a core. This is filled when the fieldtype is added to the schema via add_field_type, so we can have access to the analysis chain.

Defined Under Namespace

Modules: Analysis, Matcher Classes: CopyField, DynamicField, Field, FieldType, Field_or_Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Schema

Returns a new instance of Schema.



15
16
17
18
19
20
21
22
# File 'lib/simple_solr/schema.rb', line 15

def initialize(core)
  @core           = core
  @fields         = {}
  @dynamic_fields = {}
  @copy_fields    = Hash.new { |h, k| h[k] = [] }
  @field_types    = {}
  self.load
end

Instance Attribute Details

#xmldocObject (readonly)

Returns the value of attribute xmldoc.



13
14
15
# File 'lib/simple_solr/schema.rb', line 13

def xmldoc
  @xmldoc
end

Instance Method Details

#add_copy_field(f) ⇒ Object



84
85
86
87
# File 'lib/simple_solr/schema.rb', line 84

def add_copy_field(f)
  cf = @copy_fields[f.source]
  cf << f
end

#add_dynamic_field(f) ⇒ Object

When we add dynamic fields, we need to keep them sorted by length of the key, since that’s how they match



71
72
73
74
75
76
77
# File 'lib/simple_solr/schema.rb', line 71

def add_dynamic_field(f)
  raise "Dynamic field should be dynamic and have a '*' in it somewhere; '#{f.name}' does not" unless f.name =~ /\*/
  @dynamic_fields[f.name] = f

  @dynamic_fields = @dynamic_fields.sort { |a, b| b[0].size <=> a[0].size }.to_h

end

#add_field(f) ⇒ Object



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

def add_field(f)
  @fields[f.name] = f
  field(f.name)
end

#add_field_type(ft) ⇒ Object



94
95
96
97
# File 'lib/simple_solr/schema.rb', line 94

def add_field_type(ft)
  ft.core = @core
  @field_types[ft.name] = ft
end

#clean_schema_xmlObject



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/simple_solr/schema.rb', line 158

def clean_schema_xml
  d = @xmldoc.dup
  d.xpath('//comment()').remove
  d.css('field').remove
  d.css('fieldType').remove
  d.css('fieldtype').remove
  d.css('dynamicField').remove
  d.css('copyField').remove
  d.css('dynamicfield').remove
  d.css('copyfield').remove
  d.css('schema').children.find_all { |x| x.name == 'text' }.each { |x| x.remove }
  d
end

#copy_fieldsObject



45
46
47
# File 'lib/simple_solr/schema.rb', line 45

def copy_fields
  @copy_fields.values.flatten
end

#copy_fields_for(n) ⇒ Object



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

def copy_fields_for(n)
  @copy_fields[n]
end

#drop_copy_field(str) ⇒ Object



89
90
91
92
# File 'lib/simple_solr/schema.rb', line 89

def drop_copy_field(str)
  @copy_fields.delete(str)
  self
end

#drop_dynamic_field(str) ⇒ Object



79
80
81
82
# File 'lib/simple_solr/schema.rb', line 79

def drop_dynamic_field(str)
  @dynamic_fields.delete(str)
  self
end

#drop_field(str) ⇒ Object



54
55
56
57
# File 'lib/simple_solr/schema.rb', line 54

def drop_field(str)
  @fields.delete(str)
  self
end

#drop_field_type(str) ⇒ Object



99
100
101
102
# File 'lib/simple_solr/schema.rb', line 99

def drop_field_type(str)
  @field_types.delete(str)
  self
end

#dynamic_field(n) ⇒ Object



37
38
39
# File 'lib/simple_solr/schema.rb', line 37

def dynamic_field(n)
  @dynamic_fields[n].resolve_type(self)
end

#dynamic_fieldsObject



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

def dynamic_fields
  @dynamic_fields.values.map { |x| x.resolve_type(self) }
end

#field(n) ⇒ Object



29
30
31
# File 'lib/simple_solr/schema.rb', line 29

def field(n)
  @fields[n].resolve_type(self)
end

#field_type(k) ⇒ Object



64
65
66
# File 'lib/simple_solr/schema.rb', line 64

def field_type(k)
  @field_types[k]
end

#field_typesObject



60
61
62
# File 'lib/simple_solr/schema.rb', line 60

def field_types
  @field_types.values
end

#fieldsObject



25
26
27
# File 'lib/simple_solr/schema.rb', line 25

def fields
  @fields.values.map { |x| x.resolve_type(self) }
end

#first_matching_dfield(str) ⇒ Object



232
233
234
235
236
237
238
239
240
# File 'lib/simple_solr/schema.rb', line 232

def first_matching_dfield(str)
  df = dynamic_fields.find { |x| x.matches str }
  if df
    f        = Field.new(df.to_h)
    f[:name] = df.dynamic_name str
  end
  f

end

#first_matching_field(str) ⇒ Object



228
229
230
# File 'lib/simple_solr/schema.rb', line 228

def first_matching_field(str)
  f = fields.find { |x| x.matches str } or first_matching_dfield(str)
end

#loadObject

For loading, we get the information about the fields via the API, but grab an XML document for modifying/writing



107
108
109
110
111
112
113
114
115
# File 'lib/simple_solr/schema.rb', line 107

def load
  @xmldoc = Nokogiri.XML(@core.raw_get_content('admin/file', {:file => 'schema.xml'})) do |config|
    config.noent
  end
  load_explicit_fields
  load_dynamic_fields
  load_copy_fields
  load_field_types
end

#load_copy_fieldsObject



136
137
138
139
140
141
# File 'lib/simple_solr/schema.rb', line 136

def load_copy_fields
  @copy_fields = Hash.new { |h, k| h[k] = [] }
  @core.get('schema/copyfields')['copyFields'].each do |cfield_hash|
    add_copy_field(CopyField.new(cfield_hash['source'], cfield_hash['dest']))
  end
end

#load_dynamic_fieldsObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/simple_solr/schema.rb', line 125

def load_dynamic_fields
  @dynamic_fields = {}
  @core.get('schema/dynamicfields')['dynamicFields'].each do |field_hash|
    f = DynamicField.new_from_solr_hash(field_hash)
    if @dynamic_fields[f.name]
      raise "Dynamic field '#{f.name}' defined more than once"
    end
    add_dynamic_field(f)
  end
end

#load_explicit_fieldsObject



118
119
120
121
122
123
# File 'lib/simple_solr/schema.rb', line 118

def load_explicit_fields
  @fields = {}
  @core.get('schema/fields')['fields'].each do |field_hash|
    add_field(Field.new_from_solr_hash(field_hash))
  end
end

#load_field_typesObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/simple_solr/schema.rb', line 143

def load_field_types
  @field_types = {}
  @core.get('schema/fieldtypes')['fieldTypes'].each do |fthash|
    ft        = FieldType.new_from_solr_hash(fthash)
    type_name = ft.name
    attr      = "[@name=\"#{type_name}\"]"
    node      = @xmldoc.css("fieldType#{attr}").first || @xmldoc.css("fieldtype#{attr}").first
    unless node
      puts "Failed for type #{type_name}"
    end
    ft.xml    = node.to_xml
    add_field_type(ft)
  end
end

#reloadObject



189
190
191
# File 'lib/simple_solr/schema.rb', line 189

def reload
  @core.reload
end

#resulting_fields(str) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/simple_solr/schema.rb', line 242

def resulting_fields(str)
  rv = []
  f  = first_matching_field(str)
  rv << f
  copy_fields.each do |cf|
    if cf.matches(f.name)
      dname      = cf.dynamic_name(f.name)
      fmf        = Field.new(first_matching_field(dname).to_h)
      fmf[:name] = dname
      rv << fmf
    end
  end
  rv.uniq
end

#to_xmlObject



172
173
174
175
176
177
178
179
180
# File 'lib/simple_solr/schema.rb', line 172

def to_xml
  # Get a clean schema XML document
  d = clean_schema_xml
  s = d.css('schema').first
  [fields, dynamic_fields, copy_fields, field_types].flatten.each do |f|
    s.add_child f.to_xml_node
  end
  d.to_xml
end

#writeObject



183
184
185
186
187
# File 'lib/simple_solr/schema.rb', line 183

def write
  File.open(@core.schema_file, 'w:utf-8') do |out|
    out.puts self.to_xml
  end
end