The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

SWISH::Prog::DBI - index DB records with Swish-e

SYNOPSIS

    package My::DBI::Prog;
    use base qw( SWISH::Prog::DBI );
    
    1;
    
    package My::DBI::Prog::Doc;
    use base qw( SWISH::Prog::DBI::Doc );
    
    sub url_filter
    {
        my $doc = shift;
        my $db_data = $doc->row;
        $doc->url( $db_data->{colname_I_want_as_url} );
    }
    
    1;
    
    package main;
    use Carp;
    
    my $dbi_indexer = My::DBI::Prog->new(
        db => [
            "DBI:mysql:database=movies;host=localhost;port=3306",
            'some_user', 'some_secret_pass',
            {
                RaiseError  => 1,
                HandleError => sub { confess(shift) },
            }
        ]
    );
    
    $dbi_indexer->create(
            tables => {
                'moviesIlike' => {
                    title       => 1,
                    synopsis    => 1,
                    year        => 1,
                    director    => 1,
                    producer    => 1,
                    awards      => 1
                    }
                 }
                );

DESCRIPTION

SWISH::Prog::DBI is a SWISH::Prog subclass designed for providing full-text search for your databases with Swish-e.

Since SWISH::Prog::DBI inherits from SWISH::Prog, read the SWISH::Prog docs first. Any overridden methods are documented here.

VARIABLES

Debug

Default is 0. Set to 1 (true) for verbage on stderr.

METHODS

new( db => DBI_connect_info )

Create new indexer object. DBI_connect_info is passed directly to DBI's connect() method, so see the DBI docs for syntax. If DBI_connect_info is a DBI handle object, it is accepted as is. If DBI_connect_info is an array ref, it will be dereferenced and passed to connect(). Otherwise it will be passed to connect as is.

NOTE: The new() method simply inherits from SWISH::Prog, so any params valid for that method() are allowed here.

init

Initialize object. This overrides SWISH::Prog init() base method.

init_indexer

Adds the special table MetaName to the Config object before opening indexer.

DESTROY

Calls the DBI disconnect() method on the cached dbh before calling the SWISH::Prog::DESTROY method.

NOTE: Internal method only.

info

Internal method for retrieving db meta data.

cols

Internal method for retrieving db column data.

meta

Get all the table/column info for the current db.

create( opts )

Create index. The default is for all tables to be indexed, with each table name saved in the tablename MetaName.

opts supports the following options:

tables

Only index the following tables (and optionally, columns within tables).

Example:

If you only want to index the table foo and only the columns bar and gab, pass this:

 $dbi->index( tables => { foo => { columns => bar=>1, gab=>1 } } } );

To index all columns:

 $dbi->index( tables => { foo => 1 } );
TODO
 #TODO - make the column hash value the MetaRankBias for that column

NOTE: index() calls index_sql() internally to actually create each index. If you want to tailor your SQL (using JOINs etc.) then you probably want to call index_sql() directly for each index you want created.

index_sql( %opts )

Fetch rows from the DB, convert to XML and pass to inherited index() method. %opts should include at least the following:

sql

The SQL statement to execute.

%opts may also contain:

table

The name of the table. Used for creating virtual XML documents passed to indexer.

title

Which column to use as the title of the virtual document. If not defined, the title will be the empty string.

%opts may contain any other param that SWISH::Prog::Index->new() accepts.

Example:

 $prog_dbi->index_sql(  sql => 'SELECT * FROM `movies`',
                        title => 'Movie_Title'
                        );
                        

row2xml( table_name, row_hash_ref, title )

Converts row_hash_ref to a XML string. Returns the XML.

The table_name is included in <table> tagset within each row. You can use the table MetaName to limit searches to a specific table.

title_filter( row_hash_ref )

Override this method if you do not provide a title column in index_sql(). The return value of title_filter() will be used as the swishtitle for the row's virtual XML document.

row_filter( row_hash_ref )

Override this method if you need to alter the data returned from the db prior to it being converted to XML for indexing.

This method is called prior to title_filter() so all row data is affected.

NOTE: This is different from the row() method in the ::Doc subclass. This row_filter() gets called before the Doc object is created.

SEE ALSO

http://swish-e.org/docs/

SWISH::Prog, SWISH::Prog::DBI::Doc, Search::Tools::XML

AUTHOR

Peter Karman, <perl@peknet.com>

Thanks to Atomic Learning for supporting the development of this module.

COPYRIGHT AND LICENSE

Copyright 2006 by Peter Karman

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.