Class: Ultrasphinx::Fields
- Includes:
- Singleton
- Defined in:
- lib/ultrasphinx/fields.rb
Overview
This is a special singleton configuration class that stores the index field configurations. Rather than using a magic hash and including relevant behavior in Ultrasphinx::Configure and Ultrasphinx::Search, we unify it here.
Constant Summary collapse
- TYPE_MAP =
{ 'string' => 'text', 'text' => 'text', 'integer' => 'integer', 'date' => 'date', 'datetime' => 'date', 'timestamp' => 'date', 'float' => 'float', 'boolean' => 'integer' }
Instance Attribute Summary collapse
-
#classes ⇒ Object
Returns the value of attribute classes.
-
#types ⇒ Object
Returns the value of attribute types.
Instance Method Summary collapse
- #cast(source_string, field) ⇒ Object
- #configure(configuration) ⇒ Object
- #extract_field_alias!(entry, klass) ⇒ Object
- #extract_table_alias!(entry, klass) ⇒ Object
- #groups ⇒ Object
-
#initialize ⇒ Fields
constructor
A new instance of Fields.
- #install_facets!(entry, klass) ⇒ Object
- #null(field) ⇒ Object
- #save_and_verify_type(field, new_type, string_sortable, klass) ⇒ Object
Constructor Details
Instance Attribute Details
#classes ⇒ Object
Returns the value of attribute classes.
22 23 24 |
# File 'lib/ultrasphinx/fields.rb', line 22 def classes @classes end |
#types ⇒ Object
Returns the value of attribute types.
22 23 24 |
# File 'lib/ultrasphinx/fields.rb', line 22 def types @types end |
Instance Method Details
#cast(source_string, field) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/ultrasphinx/fields.rb', line 61 def cast(source_string, field) if types[field] == "date" "UNIX_TIMESTAMP(#{source_string})" elsif types[field] == "integer" source_string # "CAST(#{source_string} AS UNSIGNED)" else source_string end + " AS #{field}" end |
#configure(configuration) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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 |
# File 'lib/ultrasphinx/fields.rb', line 86 def configure(configuration) configuration.each do |model, | klass = model.constantize save_and_verify_type('class_id', 'integer', nil, klass) save_and_verify_type('class', 'string', nil, klass) begin # Fields are from the model # We destructively canonicize them back onto the configuration hash ['fields'] = ['fields'].to_a.map do |entry| entry = {'field' => entry} unless entry.is_a? Hash extract_table_alias!(entry, klass) extract_field_alias!(entry, klass) unless klass.columns_hash[entry['field']] # XXX I think this is here for migrations Ultrasphinx.say "warning: field #{entry['field']} is not present in #{model}" else save_and_verify_type(entry['as'], klass.columns_hash[entry['field']].type, entry['sortable'], klass) install_facets!(entry, klass) end end # Joins are whatever they are in the target ['include'].to_a.each do |entry| extract_table_alias!(entry, klass) extract_field_alias!(entry, klass) save_and_verify_type(entry['as'] || entry['field'], entry['class_name'].constantize.columns_hash[entry['field']].type, entry['sortable'], klass) install_facets!(entry, klass) end # Regular concats are CHAR, group_concats are BLOB and need to be cast to CHAR ['concatenate'].to_a.each do |entry| extract_table_alias!(entry, klass) # XXX Doesn't actually do anything useful save_and_verify_type(entry['as'], 'text', entry['sortable'], klass) install_facets!(entry, klass) end rescue ActiveRecord::StatementInvalid Ultrasphinx.say "warning: model #{model} does not exist in the database yet" end end self end |
#extract_field_alias!(entry, klass) ⇒ Object
145 146 147 148 149 |
# File 'lib/ultrasphinx/fields.rb', line 145 def extract_field_alias!(entry, klass) unless entry['as'] entry['as'] = entry['field'] end end |
#extract_table_alias!(entry, klass) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ultrasphinx/fields.rb', line 151 def extract_table_alias!(entry, klass) unless entry['table'] # Getting run twice; don't know why if entry['field'] and entry['field'].include? "." # This field is referenced by a table alias entry['table'], entry['field'] = entry['field'].split(".") else klass = entry['class_name'].constantize if entry['class_name'] entry['table'] = klass.table_name end end end |
#groups ⇒ Object
30 31 32 33 34 |
# File 'lib/ultrasphinx/fields.rb', line 30 def groups @groups.compact.sort_by do |string| string[/= (.*)/, 1] end end |
#install_facets!(entry, klass) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/ultrasphinx/fields.rb', line 137 def install_facets!(entry, klass) if entry['facet'] save_and_verify_type(entry['as'], 'text', nil, klass) # source must be a string save_and_verify_type("#{entry['as']}_facet", 'integer', nil, klass) end entry end |
#null(field) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ultrasphinx/fields.rb', line 71 def null(field) case types[field] when 'text' "''" when 'integer', 'float' "0" when 'date' "18000" # Midnight on 1/1/1970 when nil raise "Field #{field} is missing" else raise "Field #{field} does not have a valid type #{types[field]}." end + " AS #{field}" end |
#save_and_verify_type(field, new_type, string_sortable, klass) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ultrasphinx/fields.rb', line 36 def save_and_verify_type(field, new_type, string_sortable, klass) # Smoosh fields together based on their name in the Sphinx query schema field, new_type = field.to_s, TYPE_MAP[new_type.to_s] if types[field] # Existing field name; verify its type raise ConfigurationError, "Column type mismatch for #{field.inspect}; was already #{types[field].inspect}, but is now #{new_type.inspect}." unless types[field] == new_type classes[field] = (classes[field] + [klass]).uniq else # New field types[field] = new_type classes[field] = [klass] @groups << case new_type when 'float', 'integer' "sql_group_column = #{field}" when 'date' "sql_date_column = #{field}" when 'text' "sql_str2ordinal_column = #{field}" if string_sortable end end end |