Class: SimpleSolrClient::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_solr_client/schema/matcher.rb,
lib/simple_solr_client/schema.rb,
lib/simple_solr_client/schema/field.rb,
lib/simple_solr_client/schema/analysis.rb,
lib/simple_solr_client/schema/field_type.rb,
lib/simple_solr_client/schema/dynamic_field.rb,
lib/simple_solr_client/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, InvalidTokenError

Instance Method Summary collapse

Constructor Details

#initialize(core) ⇒ Schema

Returns a new instance of Schema.



13
14
15
16
17
18
19
20
# File 'lib/simple_solr_client/schema.rb', line 13

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

Instance Method Details

#add_copy_field(f) ⇒ Object



82
83
84
85
# File 'lib/simple_solr_client/schema.rb', line 82

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



69
70
71
72
73
74
75
# File 'lib/simple_solr_client/schema.rb', line 69

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



47
48
49
50
# File 'lib/simple_solr_client/schema.rb', line 47

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

#add_field_type(ft) ⇒ Object



92
93
94
95
# File 'lib/simple_solr_client/schema.rb', line 92

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

#copy_fieldsObject



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

def copy_fields
  @copy_fields.values.flatten
end

#copy_fields_for(n) ⇒ Object



39
40
41
# File 'lib/simple_solr_client/schema.rb', line 39

def copy_fields_for(n)
  @copy_fields[n]
end

#drop_copy_field(str) ⇒ Object



87
88
89
90
# File 'lib/simple_solr_client/schema.rb', line 87

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

#drop_dynamic_field(str) ⇒ Object



77
78
79
80
# File 'lib/simple_solr_client/schema.rb', line 77

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

#drop_field(str) ⇒ Object



52
53
54
55
# File 'lib/simple_solr_client/schema.rb', line 52

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

#drop_field_type(str) ⇒ Object



97
98
99
100
# File 'lib/simple_solr_client/schema.rb', line 97

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

#dynamic_field(n) ⇒ Object



35
36
37
# File 'lib/simple_solr_client/schema.rb', line 35

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

#dynamic_fieldsObject



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

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

#field(n) ⇒ Object



27
28
29
# File 'lib/simple_solr_client/schema.rb', line 27

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

#field_type(k) ⇒ Object



62
63
64
# File 'lib/simple_solr_client/schema.rb', line 62

def field_type(k)
  @field_types[k]
end

#field_typesObject



58
59
60
# File 'lib/simple_solr_client/schema.rb', line 58

def field_types
  @field_types.values
end

#fieldsObject



23
24
25
# File 'lib/simple_solr_client/schema.rb', line 23

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

#first_matching_dfield(str) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/simple_solr_client/schema.rb', line 188

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



184
185
186
# File 'lib/simple_solr_client/schema.rb', line 184

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,



104
105
106
107
108
109
# File 'lib/simple_solr_client/schema.rb', line 104

def load
  load_explicit_fields
  load_dynamic_fields
  load_copy_fields
  load_field_types
end

#load_copy_fieldsObject



130
131
132
133
134
135
# File 'lib/simple_solr_client/schema.rb', line 130

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/simple_solr_client/schema.rb', line 119

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



112
113
114
115
116
117
# File 'lib/simple_solr_client/schema.rb', line 112

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



137
138
139
140
141
142
143
# File 'lib/simple_solr_client/schema.rb', line 137

def load_field_types
  @field_types = {}
  @core.get('schema/fieldtypes')['fieldTypes'].each do |fthash|
    ft        = FieldType.new_from_solr_hash(fthash)
    add_field_type(ft)
  end
end

#reloadObject



145
146
147
# File 'lib/simple_solr_client/schema.rb', line 145

def reload
  @core.reload
end

#resulting_fields(str) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/simple_solr_client/schema.rb', line 198

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