Class: CheapSkate::Schema
- Inherits:
-
Object
- Object
- CheapSkate::Schema
- Includes:
- CheapSkate
- Defined in:
- lib/cheap_skate/schema.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#copy_fields ⇒ Object
readonly
Returns the value of attribute copy_fields.
-
#default_field ⇒ Object
readonly
Returns the value of attribute default_field.
-
#default_operator ⇒ Object
readonly
Returns the value of attribute default_operator.
-
#dynamic_fields ⇒ Object
readonly
Returns the value of attribute dynamic_fields.
-
#field_types ⇒ Object
readonly
Returns the value of attribute field_types.
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#id_field ⇒ Object
readonly
Returns the value of attribute id_field.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #field_names ⇒ Object
- #field_to_field_info(field_name) ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
- #load_from_conf(conf) ⇒ Object
- #multi_valued?(field) ⇒ Boolean
- #type_field(field_name, value) ⇒ Object
- #typed_document(lazy_doc) ⇒ Object
Constructor Details
#initialize ⇒ Schema
Returns a new instance of Schema.
103 104 105 |
# File 'lib/cheap_skate/schema.rb', line 103 def initialize @copy_fields = {} end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def config @config end |
#copy_fields ⇒ Object (readonly)
Returns the value of attribute copy_fields.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def copy_fields @copy_fields end |
#default_field ⇒ Object (readonly)
Returns the value of attribute default_field.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def default_field @default_field end |
#default_operator ⇒ Object (readonly)
Returns the value of attribute default_operator.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def default_operator @default_operator end |
#dynamic_fields ⇒ Object (readonly)
Returns the value of attribute dynamic_fields.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def dynamic_fields @dynamic_fields end |
#field_types ⇒ Object (readonly)
Returns the value of attribute field_types.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def field_types @field_types end |
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def fields @fields end |
#id_field ⇒ Object (readonly)
Returns the value of attribute id_field.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def id_field @id_field end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/cheap_skate/schema.rb', line 6 def name @name end |
Class Method Details
.new_from_config(config_hash) ⇒ Object
97 98 99 100 101 |
# File 'lib/cheap_skate/schema.rb', line 97 def self.new_from_config(config_hash) schema = self.new schema.load_from_conf(config_hash) schema end |
.xml_to_yaml(xml) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cheap_skate/schema.rb', line 7 def self.xml_to_yaml(xml) doc = REXML::Document.new xml y = {"schema"=>{"types"=>{}, "fields"=>{}}} y["schema"]["name"] = doc.root.attributes["name"] y["schema"]["version"] = doc.root.attributes["version"] doc.each_element("/schema/fields/field") do |field| f = {} field.attributes.each do |a,v| next if a == "name" f[a] = case v when "true" then true when "false" then false else v end end y["schema"]["fields"][field.attributes['name']] = f end doc.each_element("/schema/fields/dynamicField") do |dyn_field| f = {} dyn_field.attributes.each do |a,v| next if a == "name" f[a] = case v when "true" then true when "false" then false else v end end y["schema"]["dynamic_fields"] ||= {} y["schema"]["dynamic_fields"][dyn_field.attributes['name']] = f end doc.each_element("/schema/types/fieldType") do |type| t = {} t[:type] = case type.attributes['class'] when "solr.StrField" then :string when "solr.TextField" then :text when "solr.IntField" then :int when "solr.FloatField" then :float when "solr.BoolField" then :bool when "solr.DateField" then :date end if type.attributes['omitNorms'] && type.attributes['omitNorms'] == "true" t[:index] = :omit_norms end unless t[:type] == :text if t[:index] == :omit_norms t[:index] = :untokenized_omit_norms else t[:index] = :untokenized end end y["schema"]["types"][type.attributes['name']] = t end doc.each_element("/schema/types/fieldtype") do |type| t = {} t[:type] = case type.attributes['class'] when "solr.StrField" then :string when "solr.TextField" then :text when "solr.IntField" then :int when "solr.FloatField" then :float when "solr.BoolField" then :bool when "solr.DateField" then :date end if type.attributes['omitNorms'] && type.attributes['omitNorms'] == "true" t[:index] = :omit_norms end unless t[:type] == :text if t[:index] == :omit_norms t[:index] = :untokenized_omit_norms else t[:index] = :untokenized end end y["schema"]["types"][type.attributes['name']] = t end if dflt = doc.elements['/schema/defaultSearchField'] y["schema"]["defaultSearchField"] = dflt.get_text.value if dflt.has_text? end if uniq_key = doc.elements['/schema/uniqueKey'] y["schema"]["uniqueKey"] = uniq_key.get_text.value if uniq_key.has_text? end copy_fields = [] doc.each_element("/schema/copyField") do |copy| copy_fields << {copy.attributes['source']=>copy.attributes['dest']} end unless copy_fields.empty? y["schema"]["copyFields"] = copy_fields end y.to_yaml end |
Instance Method Details
#field_names ⇒ Object
216 217 218 |
# File 'lib/cheap_skate/schema.rb', line 216 def field_names return @fields.keys end |
#field_to_field_info(field_name) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/cheap_skate/schema.rb', line 220 def field_to_field_info(field_name) opts = {} if @fields[field_name][:index] == :no opts[:index] = :no opts[:term_vector] = :no elsif @field_types[@fields[field_name][:field_type]][:index] opts[:index] = @field_types[@fields[field_name][:field_type]][:index] end if @fields[field_name][:stored] == :no opts[:store] = :no end Ferret::Index::FieldInfo.new(field_name, opts) end |
#load_from_conf(conf) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/cheap_skate/schema.rb', line 107 def load_from_conf(conf) @fields ={} @field_types ={} @name = conf['schema']['name'] conf['schema']['fields'].keys.each do |field| @fields[field.to_sym] = {} fld = conf['schema']['fields'][field] @fields[field.to_sym][:field_type] = fld['type'].to_sym if fld['indexed'] == false @fields[field.to_sym][:index] = :no end if fld['stored'] == false @fields[field.to_sym][:store] = :no end @fields[field.to_sym][:multi_valued] = fld['multiValued']||false end if conf['schema']['dynamic_fields'] conf['schema']['dynamic_fields'].keys.each do |field| @dynamic_fields ||= {} @dynamic_fields[field.to_sym] = {} fld = conf['schema']['dynamic_fields'][field] @dynamic_fields[field.to_sym][:field_type] = fld['type'].to_sym if fld['indexed'] == false @dynamic_fields[field.to_sym][:index] = :no end if fld['stored'] == false @dynamic_fields[field.to_sym][:store] = :no end @dynamic_fields[field.to_sym][:multi_valued] = fld['multiValued']||false end end conf['schema']['types'].keys.each do |type| @field_types[type.to_sym] = conf['schema']['types'][type] end if conf['schema']['copyFields'] conf['schema']['copyFields'].each do |copy| copy.each_pair do | orig, dest| @copy_fields[orig.to_sym] ||= [] @copy_fields[orig.to_sym] << dest.to_sym end end end @id_field = (conf['schema']['uniqueKey'] || "id").to_sym @default_field = (conf['schema']['defaultSearchField']||"*").to_sym @default_operator = (conf['schema']['defaultOperator']||"OR") end |
#multi_valued?(field) ⇒ Boolean
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/cheap_skate/schema.rb', line 175 def multi_valued?(field) if @fields[field] return @fields[field][:multi_valued] else dyn_field = nil @dynamic_fields.keys.each do |dyn| if dyn =~ /^\*/ r = Regexp.new(dyn.sub(/^\*/,".*")) elsif dyn =~ /\*$/ r = Regexp.new(dyn.sub(/\*$/,".*")) end if field =~ dyn dyn_field = dyn break end end return dyn_field[:multi_valued] if dyn_field end false end |
#type_field(field_name, value) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/cheap_skate/schema.rb', line 196 def type_field(field_name, value) return value.to_s unless @fields[field_name] val = case @field_types[@fields[field_name][:field_type]][:type] when :string then value.to_s when :text then value.to_s when :int then value.to_i when :float then value.to_f when :date then Date.parse(value) when :bool if value == "true" true else false end else val.to_s end val end |
#typed_document(lazy_doc) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cheap_skate/schema.rb', line 154 def typed_document(lazy_doc) doc = {} lazy_doc.fields.each do |field| [*lazy_doc[field]].each do |fld| if doc[field] if multi_valued?(field) doc[field] = [*doc[field]] else doc[field] << "\n" end doc[field] << type_field(field, fld) elsif multi_valued?(field) doc[field] = [type_field(field, fld)] else doc[field] = type_field(field, fld) end end end doc end |