Class: DataMapper::Adapters::Sphinx::Client

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

Overview

Client wrapper for Riddle::Client.

  • Simple interface to searchd and indexer.

  • Can read searchd configuration options from your sphinx configuration file.

  • Managed searchd using daemon_controller for on demand daemon control in development.

Direct Known Subclasses

ManagedClient

Instance Method Summary collapse

Constructor Details

#initialize(uri_or_options = {}) ⇒ Client

Parameters

uri_or_options<URI, DataObject::URI, Addressable::URI, String, Hash, Pathname>

DataMapper uri or options hash.



19
20
21
# File 'lib/dm-sphinx-adapter/client.rb', line 19

def initialize(uri_or_options = {})
  @config = Sphinx::Config.new(uri_or_options)
end

Instance Method Details

#index(indexes = nil) ⇒ Object

Index one or more indexes.

Parameters

indexes<Array, String>

Indexes to index (and rotate). Defaults to –all if indexes is nil or ‘*’.



47
48
49
50
51
52
53
54
# File 'lib/dm-sphinx-adapter/client.rb', line 47

def index(indexes = nil)
  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.

See

  • Riddle::Client

Parameters

query<String>

A sphinx query string.

indexes<Array, String>

Indexes to search. Default is ‘*’ all.

options<Hash>

Any attributes supported by the Riddle::Client.

Returns

Hash

Riddle::Client#query response struct.



35
36
37
38
39
40
41
# File 'lib/dm-sphinx-adapter/client.rb', line 35

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