Skip to content

Sources Configuration

floere edited this page Nov 24, 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, *categories, 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')
  books_index.define_category :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 catgories:

index = index :name, Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', :file => 'app/db.yml')
index.define_category :isbn
index.define_category :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.

Options

  • file: Relative file path of the data csv file.

Ruby 1.9 options like col_sep, row_sep etc. (see http://ruby-doc.org/ruby-1.9/classes/CSV.html for details) are all available.

Delicious

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

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

Experimental

If needed you can provide each category with its own source.

index = index :name, Sources::DB.new('SELECT id, title, author, isbn13 as isbn FROM books', :file => 'app/db.yml')
index.define_category :isbn, :source => Sources::CSV.new(:isbn, :file => 'app/isbn.csv')
index.define_category :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.