Skip to content

Sources Configuration

floere edited this page Oct 30, 2010 · 32 revisions

Sources

Sources tell an index where to get its data.

Example
Possible Sources
Database
CSV
Delicious
Experimental

Example

The method index(identifier_symbol, source, *fields, options = {}) takes a source as second argument.

class PickySearch < Application
  
  books_index = index :books,
                      Sources::CSV.new(:title, :author, :isbn, :year, :publisher, :subjects, :file => 'app/library.csv'),
                      field(:title, # ...
  
end

The source defines where this index is getting its data from.

Possible sources

Database

Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', :file => 'app/db.yml')
Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', active_records_configuration_options_hash)

You can use join statements etc. (The :file option points to a yml with an active record config hash)
Then, reference your columns by their names in the fields:

Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', :file => 'app/db.yml'),
field(:isbn),
field(:title), # ...

CSV

Sources::CSV.new(:title, :author, :isbn, :year, :publisher, :subjects, :file => 'app/library.csv')

The first column in the CSV needs to be the id. The others are assumed to be in the order you provide.

Delicious

Sources::Delicious.new('username', 'password')

Delicious then provides a selection of fields: title, tags, url.

Experimental

If needed you can provide each field with its own index.

Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', :file => 'app/db.yml'),
field(:isbn, :source => Sources::CSV.new(:isbn, :file => 'app/isbn.csv')),
field(:title, :source => Sources::Delicious.new('username', 'password')), # ...

But normally you won’t need this. Don’t hesitate to ask if you don’t see any other possibility than using this.