Class: ThinkingSphinx::Index
- Inherits:
-
Object
- Object
- ThinkingSphinx::Index
- Defined in:
- lib/thinking_sphinx/index.rb,
lib/thinking_sphinx/index/builder.rb,
lib/thinking_sphinx/index/faux_column.rb
Overview
The Index class is a ruby representation of a Sphinx source (not a Sphinx index - yes, I know it’s a little confusing. You’ll manage). This is another ‘internal’ Thinking Sphinx class - if you’re using it directly, you either know what you’re doing, or messing with things beyond your ken. Enjoy.
Defined Under Namespace
Classes: Builder, FauxColumn
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#conditions ⇒ Object
Returns the value of attribute conditions.
-
#delta ⇒ Object
Returns the value of attribute delta.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#model ⇒ Object
Returns the value of attribute model.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
- #adapter ⇒ Object
-
#delta? ⇒ Boolean
Flag to indicate whether this index has a corresponding delta index.
- #infix_fields ⇒ Object
-
#initialize(model, &block) ⇒ Index
constructor
Create a new index instance by passing in the model it is tied to, and a block to build it with (optional but recommended).
-
#link! ⇒ Object
Link all the fields and associations to their corresponding associations and joins.
- #name ⇒ Object
- #prefix_fields ⇒ Object
- #to_config(index, database_conf, charset_type) ⇒ Object
-
#to_sql(options = {}) ⇒ Object
Generates the big SQL statement to get the data back for all the fields and attributes, using all the relevant association joins.
-
#to_sql_query_info ⇒ Object
Simple helper method for the query info SQL - which is a statement that returns the single row for a corresponding id.
-
#to_sql_query_pre ⇒ Object
Returns the SQL query to run before a full index - ie: nothing unless the index has a delta, and then it’s an update statement to set delta values back to 0.
-
#to_sql_query_range(options = {}) ⇒ Object
Simple helper method for the query range SQL - which is a statement that returns minimum and maximum id values.
Constructor Details
#initialize(model, &block) ⇒ Index
Create a new index instance by passing in the model it is tied to, and a block to build it with (optional but recommended). For documentation on the syntax for inside the block, the Builder class is what you want.
Quick Example:
Index.new(User) do
indexes login, email
has created_at
set_property :delta => true
end
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/thinking_sphinx/index.rb', line 28 def initialize(model, &block) @model = model @associations = {} @fields = [] @attributes = [] @conditions = [] = {} @delta = false initialize_from_builder(&block) if block_given? end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def attributes @attributes end |
#conditions ⇒ Object
Returns the value of attribute conditions.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def conditions @conditions end |
#delta ⇒ Object
Returns the value of attribute delta.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def delta @delta end |
#fields ⇒ Object
Returns the value of attribute fields.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def fields @fields end |
#model ⇒ Object
Returns the value of attribute model.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def model @model end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/thinking_sphinx/index.rb', line 12 def end |
Instance Method Details
#adapter ⇒ Object
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/thinking_sphinx/index.rb', line 215 def adapter @adapter ||= case @model.connection.class.name when "ActiveRecord::ConnectionAdapters::MysqlAdapter" :mysql when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter" :postgres else raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL" end end |
#delta? ⇒ Boolean
Flag to indicate whether this index has a corresponding delta index.
211 212 213 |
# File 'lib/thinking_sphinx/index.rb', line 211 def delta? @delta end |
#infix_fields ⇒ Object
230 231 232 |
# File 'lib/thinking_sphinx/index.rb', line 230 def infix_fields @fields.select { |field| field.infixes } end |
#link! ⇒ Object
Link all the fields and associations to their corresponding associations and joins. This must be called before interrogating the index’s fields and associations for anything that may reference their SQL structure.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/thinking_sphinx/index.rb', line 103 def link! base = ::ActiveRecord::Associations::ClassMethods::JoinDependency.new( @model, [], nil ) @fields.each { |field| field.model ||= @model field.columns.each { |col| field.associations[col] = associations(col.__stack.clone) field.associations[col].each { |assoc| assoc.join_to(base) } } } @attributes.each { |attribute| attribute.model ||= @model attribute.columns.each { |col| attribute.associations[col] = associations(col.__stack.clone) attribute.associations[col].each { |assoc| assoc.join_to(base) } } } end |
#name ⇒ Object
40 41 42 |
# File 'lib/thinking_sphinx/index.rb', line 40 def name model.name.underscore.tr(':/\\', '_') end |
#prefix_fields ⇒ Object
226 227 228 |
# File 'lib/thinking_sphinx/index.rb', line 226 def prefix_fields @fields.select { |field| field.prefixes } end |
#to_config(index, database_conf, charset_type) ⇒ Object
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 96 |
# File 'lib/thinking_sphinx/index.rb', line 44 def to_config(index, database_conf, charset_type) # Set up associations and joins link! attr_sources = attributes.collect { |attrib| attrib.to_sphinx_clause }.join("\n ") db_adapter = case adapter when :postgres "pgsql" when :mysql "mysql" else raise "Unsupported Database Adapter: Sphinx only supports MySQL and PosgreSQL" end config = "\nsource \#{model.indexes.first.name}_\#{index}_core\n{\ntype = \#{db_adapter}\nsql_host = \#{database_conf[:host] || \"localhost\"}\nsql_user = \#{database_conf[:username]}\nsql_pass = \#{database_conf[:password]}\nsql_db = \#{database_conf[:database]}\n\nsql_query_pre = \#{charset_type == \"utf-8\" && adapter == :mysql ? \"SET NAMES utf8\" : \"\"}\n\#{\"sql_query_pre = SET SESSION group_concat_max_len = \#{@options[:group_concat_max_len]}\" if @options[:group_concat_max_len]}\nsql_query_pre = \#{to_sql_query_pre}\nsql_query = \#{to_sql.gsub(/\\n/, ' ')}\nsql_query_range = \#{to_sql_query_range}\nsql_query_info = \#{to_sql_query_info}\n\#{attr_sources}\n}\n" if delta? config += "\nsource \#{model.indexes.first.name}_\#{index}_delta : \#{model.indexes.first.name}_\#{index}_core\n{\nsql_query_pre =\nsql_query_pre = \#{charset_type == \"utf-8\" && adapter == :mysql ? \"SET NAMES utf8\" : \"\"}\n\#{\"sql_query_pre = SET SESSION group_concat_max_len = \#{@options[:group_concat_max_len]}\" if @options[:group_concat_max_len]}\nsql_query = \#{to_sql(:delta => true).gsub(/\\n/, ' ')}\nsql_query_range = \#{to_sql_query_range :delta => true}\n}\n" end config end |
#to_sql(options = {}) ⇒ Object
Generates the big SQL statement to get the data back for all the fields and attributes, using all the relevant association joins. If you want the version filtered for delta values, send through :delta => true in the options. Won’t do much though if the index isn’t set up to support a delta sibling.
Examples:
index.to_sql
index.to_sql(:delta => true)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/thinking_sphinx/index.rb', line 136 def to_sql(={}) assocs = all_associations where_clause = "" if self.delta? where_clause << " AND #{@model.quoted_table_name}.#{quote_column('delta')}" +" = #{options[:delta] ? db_boolean(true) : db_boolean(false)}" end unless @conditions.empty? where_clause << " AND " << @conditions.join(" AND ") end sql = "SELECT \#{ (\n[\"\#{@model.quoted_table_name}.\#{quote_column(@model.primary_key)}\"] +\[email protected] { |field| field.to_select_sql } +\[email protected] { |attribute| attribute.to_select_sql }\n).join(\", \") }\nFROM \#{ @model.table_name }\n\#{ assocs.collect { |assoc| assoc.to_sql }.join(' ') }\nWHERE \#{@model.quoted_table_name}.\#{quote_column(@model.primary_key)} >= $start\nAND \#{@model.quoted_table_name}.\#{quote_column(@model.primary_key)} <= $end\n\#{ where_clause }\nGROUP BY \#{ (\n[\"\#{@model.quoted_table_name}.\#{quote_column(@model.primary_key)}\"] +\[email protected] { |field| field.to_group_sql }.compact +\[email protected] { |attribute| attribute.to_group_sql }.compact\n).join(\", \") }\n" if @model.connection.class.name == "ActiveRecord::ConnectionAdapters::MysqlAdapter" sql += " ORDER BY NULL" end sql end |
#to_sql_query_info ⇒ Object
Simple helper method for the query info SQL - which is a statement that returns the single row for a corresponding id.
175 176 177 178 |
# File 'lib/thinking_sphinx/index.rb', line 175 def to_sql_query_info "SELECT * FROM #{@model.quoted_table_name} WHERE " + " #{quote_column(@model.primary_key)} = $id" end |
#to_sql_query_pre ⇒ Object
Returns the SQL query to run before a full index - ie: nothing unless the index has a delta, and then it’s an update statement to set delta values back to 0.
205 206 207 |
# File 'lib/thinking_sphinx/index.rb', line 205 def to_sql_query_pre self.delta? ? "UPDATE #{@model.quoted_table_name} SET #{quote_column('delta')} = #{db_boolean(false)}" : "" end |
#to_sql_query_range(options = {}) ⇒ Object
Simple helper method for the query range SQL - which is a statement that returns minimum and maximum id values. These can be filtered by delta - so pass in :delta => true to get the delta version of the SQL.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/thinking_sphinx/index.rb', line 184 def to_sql_query_range(={}) min_statement = "MIN(#{quote_column(@model.primary_key)})" max_statement = "MAX(#{quote_column(@model.primary_key)})" # Fix to handle Sphinx PostgreSQL bug (it doesn't like NULLs or 0's) if adapter == :postgres min_statement = "COALESCE(#{min_statement}, 1)" max_statement = "COALESCE(#{max_statement}, 1)" end sql = "SELECT #{min_statement}, #{max_statement} " + "FROM #{@model.quoted_table_name} " sql << "WHERE #{@model.quoted_table_name}.#{quote_column('delta')} " + "= #{options[:delta] ? db_boolean(true) : db_boolean(false)}" if self.delta? sql end |