Skip to content

Commit

Permalink
Make a start at a Lucy backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
KakeLP committed Oct 23, 2013
1 parent e1ec724 commit 311ef0e
Show file tree
Hide file tree
Showing 12 changed files with 467 additions and 212 deletions.
35 changes: 24 additions & 11 deletions Build.PL
Expand Up @@ -85,7 +85,7 @@ my %yn_vars = map { $_ => 1 }
qw(use_plucene enable_page_deletion navbar_on_home_page backlinks_in_title
moderation_requires_password enable_node_image enable_common_categories
enable_common_locales recent_changes_on_home_page use_leaflet
random_page_omits_locales random_page_omits_categories
random_page_omits_locales random_page_omits_categories use_lucy
content_above_navbar_in_html show_gmap_in_node_display force_wgs84
send_moderation_notifications read_only);

Expand Down Expand Up @@ -135,14 +135,15 @@ push @answers, { question => $dbtype_qu,

my $install_directory; # used to suggest template paths
my $use_plucene = 1; # keep track of this so we know what to put in prereqs
my $use_lucy = 0;
my $use_leaflet; # if true, we skip the GMaps questions
my %gmaps_vars = map { $_ => 1 } qw( gmaps_api_key centre_long centre_lat
default_gmaps_zoom default_gmaps_search_zoom );
my $centre_lat = ''; # contains centre lat derived from Google Maps URL
foreach my $var ( qw(
dbname dbuser dbpass dbhost dbport script_name
install_directory template_path custom_template_path script_url
custom_lib_path use_plucene indexing_directory enable_page_deletion
custom_lib_path use_lucy use_plucene indexing_directory enable_page_deletion
admin_pass stylesheet_url site_name navbar_on_home_page
recent_changes_on_home_page random_page_omits_locales
random_page_omits_categories content_above_navbar_in_html home_name
Expand Down Expand Up @@ -178,15 +179,26 @@ foreach my $var ( qw(
next;
}

# We don't ask this for new installs as Search::InvertedIndex is
# deprecated
if ( $var eq "use_plucene" and $existing_config->$var == 1) {
print "Skipping question about plucene\n"
unless $skip_config;
push @answers, { question => $qu,
variable => $var,
value => 1 };
next;
# Don't ask about Plucene if we've said to use Lucy. If we haven't said
# to use Lucy, ask about Plucene only for existing installs, since the old
# Search::InvertedIndex method is deprecated.
if ( $var eq "use_plucene" ) {
if ( $use_lucy ) {
print "Skipping question about Plucene - we're using Lucy.\n"
unless $skip_config;
push @answers, { question => $qu,
variable => $var,
value => 0 };
next;
} elsif ( $existing_config->$var == 1 ) {
print "Skipping question about Plucene - we'll use it by "
. "default.\n"
unless $skip_config;
push @answers, { question => $qu,
variable => $var,
value => 1 };
next;
}
}

# If we're using Leaflet, we can skip the GMaps stuff. Don't erase any
Expand Down Expand Up @@ -245,6 +257,7 @@ foreach my $var ( qw(

# Keep track of chosen search method so we know what to put in prereqs.
# From Module::Build docs: ->y_n returns a Perl boolean true or false.
$use_lucy = 1 if $var eq "use_lucy" and $val;
$use_plucene = 1 if $var eq "use_plucene" and $val;

# If they've just chosen to use Leaflet, we won't need to ask any of the
Expand Down
4 changes: 4 additions & 0 deletions Changes
Expand Up @@ -3,6 +3,10 @@
More detailed changelogs can be found at
<http://github.com/OpenGuides/OpenGuides>

0.75
Added a Lucy backend for searching. This is still experimental,
so we recommend most people stick with Plucene for now.

0.74 19 July 2013
Skip the correct number tests in t/210_json.t

Expand Down
13 changes: 13 additions & 0 deletions INSTALL
Expand Up @@ -103,6 +103,19 @@ to search here just as you would enter them in a 'use lib qw( ... );'
in a Perl script.


"Do you want to use Lucy for searching?"

This is currently experimental - don't say "yes" to this question unless
you know what you're doing.

If you are changing to Lucy from another search method (Plucene or
Search::InvertedIndex), you will need to do two things:
- either delete your old indexes (they're just files in the index
directory) or use a different index directory
- reindex your entire wiki (see reindex.pl in the examples/
directory of this distribution)


"Do you want to use Plucene for searching?"

This question is no longer asked, but documentation here is retained for
Expand Down
6 changes: 5 additions & 1 deletion lib/OpenGuides/Config.pm
Expand Up @@ -12,7 +12,7 @@ use base qw( Class::Accessor );
my @variables = qw(
dbtype dbname dbuser dbpass dbport dbhost dbencoding
script_name install_directory script_url
custom_lib_path use_plucene indexing_directory enable_page_deletion
custom_lib_path use_plucene use_lucy indexing_directory enable_page_deletion
admin_pass stylesheet_url site_name navbar_on_home_page
recent_changes_on_home_page random_page_omits_locales
random_page_omits_categories content_above_navbar_in_html home_name
Expand Down Expand Up @@ -78,6 +78,7 @@ sub _init {
script_name => "wiki.cgi",
install_directory => "/usr/lib/cgi-bin/openguides/",
use_plucene => 1,
use_lucy => 0,
indexing_directory => "/usr/lib/cgi-bin/openguides/indexes/",
enable_page_deletion => 0,
moderation_requires_password => 1,
Expand Down Expand Up @@ -163,6 +164,7 @@ sub _init {
script_url => "What URL does the install directory map to?",
custom_lib_path => "Do you want me to munge a custom lib path into the scripts? If so, enter it here. Separate path entries with whitespace.",
use_plucene => "Do you want to use Plucene for searching? (recommended, but see Changes file before saying yes to this if you are upgrading)",
use_lucy => "Do you want to use Lucy for searching? (experimental)",
indexing_directory => "What directory can I use to store indexes in for searching? ***NOTE*** This directory must exist and be writeable by the user that your script will run as. See README for more on this.",
enable_page_deletion => "Do you want to enable page deletion?",
moderation_requires_password => "Is the admin password required for moderating pages?",
Expand Down Expand Up @@ -267,6 +269,8 @@ sub script_url {
=item * use_plucene (default: true)
=item * use_lucy (default: false)
=item * indexing_directory (default: C</usr/lib/cgi-bin/openguides/indexes>)
=item * enable_page_deletion (default: false)
Expand Down
9 changes: 9 additions & 0 deletions lib/OpenGuides/Search.pm
Expand Up @@ -340,6 +340,15 @@ sub run_text_search {
my $self = shift;
my $searchstr = $self->{search_string};
my $wiki = $self->wiki;
my $config = $self->config;

if ( $config->use_lucy ) {
require OpenGuides::Search::Lucy;
my $lucy = OpenGuides::Search::Lucy->new( config => $config );
my %results = $lucy->run_text_search( search_string => $searchstr );
$self->{results} = \%results;
return $self;
}

# Create parser to parse the search string.
my $parser = Parse::RecDescent->new( q{
Expand Down
94 changes: 94 additions & 0 deletions lib/OpenGuides/Search/Lucy.pm
@@ -0,0 +1,94 @@
package OpenGuides::Search::Lucy;
use strict;

use Wiki::Toolkit::Search::Lucy;

=head1 NAME
OpenGuides::Search::Lucy - Run Lucy-backed text searches for OpenGuides.
=head1 DESCRIPTION
Does search stuff for OpenGuides. Distributed and installed as part of
the OpenGuides project, not intended for independent installation.
This documentation is probably only useful to OpenGuides developers.
=head1 SYNOPSIS
use OpenGuides::Config;
use OpenGuides::Search::Lucy;
my $config = OpenGuides::Config->new( file => "wiki.conf" );
my $search = OpenGuides::Search::Lucy->new( config => $config );
$search->run_text_search( search_string => "wombat defenestration" );
=head1 METHODS
=over 4
=item B<new>
my $config = OpenGuides::Config->new( file => "wiki.conf" );
my $search = OpenGuides::Search::Lucy->new( config => $config );
=cut

sub new {
my ($class, %args) = @_;
my $config = $args{config};
my $searcher = Wiki::Toolkit::Search::Lucy->new(
path => $config->indexing_directory );

my $self = {
config => $config,
searcher => $searcher,
};

bless $self, $class;
}

=item B<run_text_search>
my $config = OpenGuides::Config->new( file => "wiki.conf" );
my $search = OpenGuides::Search::Lucy->new( config => $config );
$search->run_text_search( search_string => "wombat defenestration" );
=cut

sub run_text_search {
my ( $self, %args ) = @_;

# If there are commas in the search string, we're looking at an OR search.
my $str = $args{search_string};
my $and_or = ( $str =~ /,/ ) ? "OR" : "AND";

my %finds = $self->{searcher}->search_nodes( $str, $and_or );

# Package the finds in a way that OpenGuides::Search expects.
my %results = map { $_ => { name => $_, score => $finds{$_} } }
keys %finds;
return %results;
}

=back
=cut

=head1 AUTHOR
The OpenGuides Project (openguides-dev@lists.openguides.org)
=head1 COPYRIGHT
Copyright (C) 2013 The OpenGuides Project. All Rights Reserved.
The OpenGuides distribution is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<OpenGuides>
=cut

1;
63 changes: 63 additions & 0 deletions lib/OpenGuides/Search/Lucy.pm~
@@ -0,0 +1,63 @@
package OpenGuides::Search::Lucy;
use strict;

=head1 NAME

OpenGuides::Search::Lucy - Run Lucy-backed text searches for OpenGuides.

=head1 DESCRIPTION

Does search stuff for OpenGuides. Distributed and installed as part of
the OpenGuides project, not intended for independent installation.
This documentation is probably only useful to OpenGuides developers.

=head1 SYNOPSIS

use OpenGuides::Config;
use OpenGuides::Search::Lucy;

my $config = OpenGuides::Config->new( file => "wiki.conf" );
OpenGuides::Search::Lucy->run_text_search(
config => $config,
search_string => "wombat defenestration",
);

=head1 METHODS

=over 4

=item B<run_text_search>

my $config = OpenGuides::Config->new( file => "wiki.conf" );
OpenGuides::Search::Lucy->run_text_search(
config => $config,
search_string => "wombat defenestration",
);

=cut

sub run_text_search {
}

=back

=cut

=head1 AUTHOR

The OpenGuides Project (openguides-dev@lists.openguides.org)

=head1 COPYRIGHT

Copyright (C) 2013 The OpenGuides Project. All Rights Reserved.

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

=head1 SEE ALSO

L<OpenGuides>

=cut

1;
3 changes: 2 additions & 1 deletion lib/OpenGuides/Test.pm
Expand Up @@ -208,7 +208,8 @@ sub make_cgi_object {
Openguides::Test::refresh_db();
Unlink the existing SQLite database t/node.db and plucene indexes. Then create a new SQLite database t/node.db
Unlink the existing SQLite database t/node.db and Plucene/Lucy indexes.
Then create a new SQLite database t/node.db
=cut
sub refresh_db {
Expand Down
9 changes: 7 additions & 2 deletions lib/OpenGuides/Utils.pm
Expand Up @@ -60,7 +60,8 @@ dbname
=item *
indexing_directory - for the L<Search::InvertedIndex> or L<Plucene> files to go
indexing_directory - for the L<Search::InvertedIndex>, L<Plucene>,
or C<Lucy> files to go in
=back
Expand Down Expand Up @@ -97,7 +98,11 @@ sub make_wiki_object {

# Make search.
my $search;
if ( $config->use_plucene
if ( $config->use_lucy ) {
require Wiki::Toolkit::Search::Lucy;
$search = Wiki::Toolkit::Search::Lucy->new(
path => $config->indexing_directory );
} elsif ( $config->use_plucene
&& ( lc($config->use_plucene) eq "y"
|| $config->use_plucene == 1 )
) {
Expand Down
13 changes: 9 additions & 4 deletions t/206_geo_data_search_form.t
Expand Up @@ -10,9 +10,10 @@ if ( $@ ) {
plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)";
}

eval { require Plucene; };
if ( $@ ) {
plan skip_all => "Plucene not installed";
my $have_lucy = eval { require Lucy; } ? 1 : 0;
my $have_plucene = eval { require Plucene; } ? 1 : 0;
unless ( $have_lucy || $have_plucene ) {
plan skip_all => "Neither Lucy nor Plucene is installed";
}

eval { require Test::HTML::Content; };
Expand All @@ -26,7 +27,11 @@ plan tests => 12;
OpenGuides::Test::refresh_db();

my $config = OpenGuides::Test->make_basic_config;
$config->use_plucene( 1 );
if ( $have_lucy ) {
$config->use_lucy ( 1 );
} else {
$config->use_plucene( 1 );
}

# British National Grid guides should have os_x/os_y/os_dist search fields.
my $guide = OpenGuides->new( config => $config );
Expand Down

0 comments on commit 311ef0e

Please sign in to comment.