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)
- config  Optional but recommended path to sphinx config file.

Alternatively supply a Hash:

DataMapper.setup(:search, {
  :adapter  => 'sphinx',       # required
  :config   => './sphinx.conf' # optional. Recommended though.
  :host     => 'localhost',    # optional. Default: localhost
  :port     => 3312            # optional. Default: 3312
  :managed  => true            # optional. Self managed searchd server using daemon_controller.
})

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, uri_or_options) ⇒ Adapter

See

  • DataMapper::Adapters::Sphinx::Config

  • DataMapper::Adapters::Sphinx::Client

Parameters

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

DataMapper uri or options hash.



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

def initialize(name, uri_or_options)
  super

  managed = !!(uri_or_options.kind_of?(Hash) && uri_or_options[:managed])
  @client  = managed ? ManagedClient.new(uri_or_options) : Client.new(uri_or_options)
end

Instance Attribute Details

#clientObject (readonly)

Interaction with searchd and indexer.

See

  • DataMapper::Adapters::Sphinx::Client

  • DataMapper::Adapters::Sphinx::ManagedClient

Returns

DataMapper::Adapters::Sphinx::Client

The client.



59
60
61
# File 'lib/dm-sphinx-adapter/adapter.rb', line 59

def client
  @client
end

Instance Method Details

#create(resources) ⇒ Object

:nodoc:



61
62
63
# File 'lib/dm-sphinx-adapter/adapter.rb', line 61

def create(resources) #:nodoc:
  true
end

#delete(query) ⇒ Object

:nodoc:



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

def delete(query) #:nodoc:
  true
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.



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

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.



99
100
101
# File 'lib/dm-sphinx-adapter/adapter.rb', line 99

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