Class: DataMapper::SphinxClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dm-sphinx-adapter/sphinx_client.rb

Direct Known Subclasses

SphinxManagedClient

Instance Method Summary collapse

Constructor Details

#initialize(uri_or_options) ⇒ SphinxClient

Returns a new instance of SphinxClient.



8
9
10
11
# File 'lib/dm-sphinx-adapter/sphinx_client.rb', line 8

def initialize(uri_or_options)
  # TODO: Documentation.
  @config = SphinxConfig.new(uri_or_options)
end

Instance Method Details

#index(indexes = nil, options = {}) ⇒ Object

Index one or more indexes.

Parameters:

  • indexes (Array, String) (defaults to: nil)

    Defaults to –all if indexes is nil or ‘*’.



32
33
34
35
36
37
38
39
# File 'lib/dm-sphinx-adapter/sphinx_client.rb', line 32

def index(indexes = nil, options = {})
  indexes = indexes.join(' ') if indexes.kind_of?(Array)

  command = @config.indexer_bin
  command << " --rotate" if running?
  command << ((indexes.nil? || indexes == '*') ? ' --all' : " #{indexes.to_s}")
  warn "Sphinx: Indexer #{$1}" if `#{command}` =~ /(?:error|fatal|warning):?\s*([^\n]+)/i
end

#search(query, indexes = '*', options = {}) ⇒ Object

Search one or more indexes.

Parameters:

  • query (String)

    The sphinx query string.

  • indexes (Array, String) (defaults to: '*')

    A string or array of indexes to search. Default is ‘*’ (all).

  • options (Hash) (defaults to: {})

    Any options you’d like to pass through to Riddle::Client.

See Also:

  • Riddle::Client


20
21
22
23
24
25
26
# File 'lib/dm-sphinx-adapter/sphinx_client.rb', line 20

def search(query, indexes = '*', options = {})
  indexes = indexes.join(' ') if indexes.kind_of?(Array)

  client = Riddle::Client.new(@config.address, @config.port)
  options.each{|k, v| client.method("#{k}=".to_sym).call(v) if client.respond_to?("#{k}=".to_sym)}
  client.query(query, indexes.to_s)
end