Module: ThinkingSphinx

Extended by:
SearchMethods::ClassMethods
Defined in:
lib/thinking_sphinx.rb,
lib/thinking_sphinx/facet.rb,
lib/thinking_sphinx/field.rb,
lib/thinking_sphinx/index.rb,
lib/thinking_sphinx/deltas.rb,
lib/thinking_sphinx/search.rb,
lib/thinking_sphinx/source.rb,
lib/thinking_sphinx/property.rb,
lib/thinking_sphinx/attribute.rb,
lib/thinking_sphinx/excerpter.rb,
lib/thinking_sphinx/source/sql.rb,
lib/thinking_sphinx/association.rb,
lib/thinking_sphinx/class_facet.rb,
lib/thinking_sphinx/core/string.rb,
lib/thinking_sphinx/facet_search.rb,
lib/thinking_sphinx/active_record.rb,
lib/thinking_sphinx/configuration.rb,
lib/thinking_sphinx/index/builder.rb,
lib/thinking_sphinx/search_methods.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/index/faux_column.rb,
lib/thinking_sphinx/active_record/delta.rb,
lib/thinking_sphinx/active_record/scopes.rb,
lib/thinking_sphinx/deltas/default_delta.rb,
lib/thinking_sphinx/deltas/delayed_delta.rb,
lib/thinking_sphinx/deltas/datetime_delta.rb,
lib/thinking_sphinx/adapters/mysql_adapter.rb,
lib/thinking_sphinx/deltas/delayed_delta/job.rb,
lib/thinking_sphinx/adapters/abstract_adapter.rb,
lib/thinking_sphinx/source/internal_properties.rb,
lib/thinking_sphinx/adapters/postgresql_adapter.rb,
lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb,
lib/thinking_sphinx/active_record/attribute_updates.rb,
lib/thinking_sphinx/active_record/has_many_association.rb,
lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb,
lib/thinking_sphinx/rails_additions.rb

Defined Under Namespace

Modules: AbstractQuotedTableName, ActiveRecord, ActiveRecordQuotedName, ActiveRecordStoreFullSTIClass, ArrayExtractOptions, ClassAttributeMethods, Core, Deltas, HashExcept, MetaClass, MysqlQuotedTableName, SearchMethods Classes: AbstractAdapter, Association, Attribute, ClassFacet, Configuration, ConnectionError, Excerpter, Facet, FacetSearch, Field, Index, MysqlAdapter, PostgreSQLAdapter, Property, Search, Source, StaleIdsException

Constant Summary collapse

@@deltas_enabled =
nil
@@updates_enabled =
nil
@@suppress_delta_output =
false
@@remote_sphinx =
false

Class Method Summary collapse

Methods included from SearchMethods::ClassMethods

count, facets, search, search_context, search_count, search_for_id, search_for_ids

Class Method Details

.define_indexes=(value) ⇒ Object

Enable/disable indexes - you may want to do this while migrating data.

ThinkingSphinx.define_indexes = false


85
86
87
# File 'lib/thinking_sphinx.rb', line 85

def self.define_indexes=(value)
  @@define_indexes = value
end

.define_indexes?Boolean

Check if index definition is disabled.

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/thinking_sphinx.rb', line 76

def self.define_indexes?
  @@define_indexes =  true unless defined?(@@define_indexes)
  @@define_indexes == true
end

.deltas_enabled=(value) ⇒ Object

Enable/disable all delta indexing.

ThinkingSphinx.deltas_enabled = false


102
103
104
# File 'lib/thinking_sphinx.rb', line 102

def self.deltas_enabled=(value)
  @@deltas_enabled = value
end

.deltas_enabled?Boolean

Check if delta indexing is enabled.

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/thinking_sphinx.rb', line 93

def self.deltas_enabled?
  @@deltas_enabled  = (ThinkingSphinx::Configuration.environment != 'test') if @@deltas_enabled.nil?
  @@deltas_enabled
end

.indexed_modelsObject

The collection of indexed models. Keep in mind that Rails lazily loads its classes, so this may not actually be populated with all the models that have Sphinx indexes.



66
67
68
# File 'lib/thinking_sphinx.rb', line 66

def self.indexed_models
  @@indexed_models ||= []
end

.jruby?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/thinking_sphinx.rb', line 199

def self.jruby?
  defined?(JRUBY_VERSION)
end

.microsoft?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/thinking_sphinx.rb', line 195

def self.microsoft?
  RUBY_PLATFORM =~ /mswin/
end

.mysql?Boolean

Returns:

  • (Boolean)


203
204
205
206
207
208
# File 'lib/thinking_sphinx.rb', line 203

def self.mysql?
  ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlAdapter" ||
  ::ActiveRecord::Base.connection.class.name.demodulize == "MysqlplusAdapter" || (
    jruby? && ::ActiveRecord::Base.connection.config[:adapter] == "jdbcmysql"
  )
end

.pid_active?(pid) ⇒ Boolean

Returns:

  • (Boolean)


189
190
191
192
193
# File 'lib/thinking_sphinx.rb', line 189

def self.pid_active?(pid)
  !!Process.kill(0, pid.to_i)
rescue Exception => e
  false
end

.remote_sphinx=(value) ⇒ Object

Tells Thinking Sphinx that Sphinx is running on a different machine, and thus it can’t reliably guess whether it is running or not (ie: the #sphinx_running? method), and so just assumes it is.

Useful for multi-machine deployments. Set it in your production.rb file.

ThinkingSphinx.remote_sphinx = true


162
163
164
# File 'lib/thinking_sphinx.rb', line 162

def self.remote_sphinx=(value)
  @@remote_sphinx = value
end

.remote_sphinx?Boolean

An indication of whether Sphinx is running on a remote machine instead of the same machine.

Returns:

  • (Boolean)


150
151
152
# File 'lib/thinking_sphinx.rb', line 150

def self.remote_sphinx?
  @@remote_sphinx
end

.sphinx_pidObject



181
182
183
184
185
186
187
# File 'lib/thinking_sphinx.rb', line 181

def self.sphinx_pid
  if File.exists?(ThinkingSphinx::Configuration.instance.pid_file)
    File.read(ThinkingSphinx::Configuration.instance.pid_file)[/\d+/]
  else
    nil
  end
end

.sphinx_running?Boolean

Check if Sphinx is running. If remote_sphinx is set to true (indicating Sphinx is on a different machine), this will always return true, and you will have to handle any connection errors yourself.

Returns:

  • (Boolean)


170
171
172
# File 'lib/thinking_sphinx.rb', line 170

def self.sphinx_running?
  remote_sphinx? || sphinx_running_by_pid?
end

.sphinx_running_by_pid?Boolean

Check if Sphinx is actually running, provided the pid is on the same machine as this code.

Returns:

  • (Boolean)


177
178
179
# File 'lib/thinking_sphinx.rb', line 177

def self.sphinx_running_by_pid?
  !!sphinx_pid && pid_active?(sphinx_pid)
end

.suppress_delta_output=(value) ⇒ Object



130
131
132
# File 'lib/thinking_sphinx.rb', line 130

def self.suppress_delta_output=(value)
  @@suppress_delta_output = value
end

.suppress_delta_output?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/thinking_sphinx.rb', line 126

def self.suppress_delta_output?
  @@suppress_delta_output
end

.unique_id_expression(offset = nil) ⇒ Object



70
71
72
# File 'lib/thinking_sphinx.rb', line 70

def self.unique_id_expression(offset = nil)
  "* #{ThinkingSphinx.indexed_models.size} + #{offset || 0}"
end

.updates_enabled=(value) ⇒ Object

Enable/disable updates to Sphinx

ThinkingSphinx.updates_enabled = false


120
121
122
# File 'lib/thinking_sphinx.rb', line 120

def self.updates_enabled=(value)
  @@updates_enabled = value
end

.updates_enabled?Boolean

Check if updates are enabled. True by default, unless within the test environment.

Returns:

  • (Boolean)


111
112
113
114
# File 'lib/thinking_sphinx.rb', line 111

def self.updates_enabled?
  @@updates_enabled  = (ThinkingSphinx::Configuration.environment != 'test') if @@updates_enabled.nil?
  @@updates_enabled
end

.use_group_by_shortcut?Boolean

Checks to see if MySQL will allow simplistic GROUP BY statements. If not, or if not using MySQL, this will return false.

Returns:

  • (Boolean)


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

def self.use_group_by_shortcut?
  !!(
    mysql? && ::ActiveRecord::Base.connection.select_all(
      "SELECT @@global.sql_mode, @@session.sql_mode;"
    ).all? { |key,value| value.nil? || value[/ONLY_FULL_GROUP_BY/].nil? }
  )
end

.versionString

The current version of Thinking Sphinx.

Returns:

  • (String)

    The version number as a string



58
59
60
61
# File 'lib/thinking_sphinx.rb', line 58

def self.version
  hash = YAML.load_file File.join(File.dirname(__FILE__), '../VERSION.yml')
  [hash[:major], hash[:minor], hash[:patch]].join('.')
end