Class: DataMapper::Adapters::Sphinx::Adapter

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

Overview

Synopsis

DataMapper uses URIs or a connection has to connect to your data-stores. In this case the sphinx search daemon searchd.

On its own this adapter will only return an array of document hashes when queried. The DataMapper library dm-more however provides dm-is-searchable, a common interface to search one adapter and load documents from another. My preference is to use this adapter in tandem with dm-is-searchable.

Like all DataMapper adapters you can connect with a Hash or URI.

A URI:

DataMapper.setup(:search, 'sphinx://localhost')

The breakdown is:

"#{adapter}://#{host}:#{port}/#{config}"
- adapter Must be :sphinx
- host    Hostname (default: localhost)
- port    Optional port number (default: 3312)

Alternatively supply a Hash:

DataMapper.setup(:search, {
  :adapter  => 'sphinx',       # required
  :host     => 'localhost',    # optional. Default: localhost
  :port     => 3312            # optional. Default: 3312
})

Instance Method Summary collapse

Constructor Details

#initialize(name, uri_or_options) ⇒ Adapter

See

  • DataMapper::Adapters::AbstractAdapter

Parameters

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

DataMapper uri or options hash.



38
39
40
41
42
# File 'lib/dm-sphinx-adapter/adapter.rb', line 38

def initialize(name, uri_or_options)
  options = normalize_options(uri_or_options)
  @client = Riddle::Client.new(options.delete(:host), options.delete(:port))
  options.each{|k, v| @client.method("#{k}=".to_sym).call(v) if @client.respond_to?("#{k}=".to_sym)}
end

Instance Method Details

#create(resources) ⇒ Object

:nodoc:



44
45
46
# File 'lib/dm-sphinx-adapter/adapter.rb', line 44

def create(resources) #:nodoc:
  0
end

#delete(query) ⇒ Object

:nodoc:



48
49
50
# File 'lib/dm-sphinx-adapter/adapter.rb', line 48

def delete(query) #:nodoc:
  0
end

#read_many(query) ⇒ Object

Query your Sphinx repository and return all matching documents.

Notes

These methods are public but normally called indirectly through DataMapper::Resource#get, DataMapper::Resource#first or DataMapper::Resource#all.

Parameters

query<DataMapper::Query>

The query object.

Returns

Array<Hash>

An array of document hashes. [{:id => 1}, {:id => 2}]

Array<>

An empty array if no documents match.



65
66
67
# File 'lib/dm-sphinx-adapter/adapter.rb', line 65

def read_many(query)
  read(query)
end

#read_one(query) ⇒ Object

Query your Sphinx repository and return the first document matched.

Notes

These methods are public but normally called indirectly through DataMapper::Resource#get, DataMapper::Resource#first or DataMapper::Resource#all.

Parameters

query<DataMapper::Query>

The query object.

Returns

Hash

An document hash of the first document matched. {:id => 1}

Nil

If no documents match.



82
83
84
# File 'lib/dm-sphinx-adapter/adapter.rb', line 82

def read_one(query)
  read(query).first
end