1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#!/usr/bin/perl
#
# A sample indexer which demonstrates many of Xapian's commonly used features.
#
# Copyright (C) 2009 Olly Betts
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
use 5.006;
use strict;
use warnings;
use Search::Xapian (':all');
use POSIX;
# Constants denoting what we're using the number value slots for.
my $SLOT_DATE = 0;
my $SLOT_DOCNUM = 1;
my $SLOT_TYPE = 2;
my $SLOT_AUTHOR = 3;
my $SLOT_TITLE = 4;
# We want exactly one command line argument.
if (scalar @ARGV != 1) {
print STDERR "Usage: $0 PATH_TO_DATABASE\n";
exit 1;
}
my ($database, $indexer);
eval {
# Open the database for writing. If it doesn't exist, create it.
$database = Search::Xapian::WritableDatabase->new(
$ARGV[0],
DB_CREATE_OR_OPEN);
# Set up a TermGenerator to index text stemmed with the "english" stemmer.
$indexer = Search::Xapian::TermGenerator->new();
my $stemmer = Search::Xapian::Stem->new("english");
$indexer->set_stemmer($stemmer);
};
if ($@) {
# Report the exception which we've caught.
print STDERR "Exception: $@\n";
exit 1;
}
# Add some documents to the index (in a real indexer, this data would come from
# some external source like a file or a database).
index_document(
"The Old Man and the Sea",
"Ernest Hemingway",
"Santiago goes fishing, without much success.",
"978-0-684-80122-3",
"1952-09-01",
"book"
);
index_document(
"Star Wars",
"George Lucas",
"Luke goes to meet his destiny in the stars.",
"tt0076759",
"1977-05-25",
"film"
);
index_document(
"Accidental Death of an Anarchist",
"Dario Fo",
"An anarchist dies, accidentally!",
"12345",
"1970-12-10",
"play"
);
sub index_document {
my ($doc_name, $author, $keywords, $doc_number, $date, $type) = @_;
eval {
my $doc = Search::Xapian::Document->new();
$indexer->set_document($doc);
# Set the document data to the doc_name so we can show it for matches.
$doc->set_data($doc_name);
# Index the author to allow fielded free-text searching.
$indexer->index_text($author, 1, "A");
# Index the title to allow fielded free-text searching.
$indexer->index_text($doc_name, 1, "S");
# Index the title without a prefix too.
$indexer->index_text($doc_name);
# Increase the term position so that phrases can't straddle the
# doc_name and keywords.
$indexer->increase_termpos();
# Index the keywords as free-text.
$indexer->index_text($keywords);
# Unique ID.
$doc->add_term("Q" . $doc_number);
# To allow boolean filtering by type.
$doc->add_term("XTYPE" . lc $type);
# To allow date range searching and sorting by date.
if ($date =~ /^(\d{4})-(\d\d)-(\d\d)$/) {
# DateValueRangeProcessor wants values in the form "YYYYMMDD".
$doc->add_value($SLOT_DATE, "$1$2$3");
}
# To allow sorting by document number.
$doc->add_value($SLOT_DOCNUM, $doc_number);
# To allow sorting by document type.
$doc->add_value($SLOT_TYPE, lc $type);
# To allow sorting by author.
$doc->add_value($SLOT_AUTHOR, $author);
# To allow sorting by title..
$doc->add_value($SLOT_TITLE, $doc_name);
# Add the document to the database.
$database->add_document($doc);
};
if ($@) {
# Report the exception which we've caught.
print STDERR "Exception: $@\n";
exit 1;
}
}
|