source: trunk/gsdl/bin/script/lucene_query.pl@ 12848

Last change on this file since 12848 was 12848, checked in by nzdl, 17 years ago

try the JAVA_HOME variable first to find java, otherwise just use 'java' [kjdon]

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# lucene_query.pl -- perl wrapper to initiate query using Lucene
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28
29BEGIN {
30 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
31 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
32 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
33}
34
35use strict;
36use util;
37
38my $PROGNAME = $0;
39$PROGNAME =~ s/^.*\/(.*)$/$1/;
40
41
42sub get_java_path()
43{
44 # Check the JAVA_HOME environment variable first
45 if (defined $ENV{'JAVA_HOME'}) {
46 my $java_home = $ENV{'JAVA_HOME'};
47 $java_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
48 return &util::filename_cat($java_home, "bin", "java");
49 }
50
51 # Hope that Java is on the PATH
52 return "java";
53}
54
55sub open_java_lucene
56{
57 my $full_indexdir = shift(@_);
58 my $fuzziness = shift(@_);
59 my $filter_string = shift(@_);
60 my $sort_field = shift(@_);
61 my $dco = shift(@_);
62 my $start_results = shift(@_);
63 my $end_results = shift(@_);
64 my $out_file = shift(@_);
65
66 my $java = &get_java_path();
67 my $classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "LuceneWrap.jar");
68 my $java_lucene = "$java -classpath \"$classpath\" org.nzdl.gsdl.LuceneWrap.GS2LuceneQuery";
69
70 my $cmd = "| " . $java_lucene . " \"" . $full_indexdir . "\"";
71 if (defined($fuzziness)) {
72 $cmd .= " -fuzziness " . $fuzziness;
73 }
74 if (defined($filter_string)) {
75 $cmd .= " -filter \"" . $filter_string . "\"";
76 }
77 if (defined($sort_field)) {
78 $cmd .= " -sort " . $sort_field;
79 }
80 if (defined($dco)) {
81 $cmd .= " -dco " . $dco;
82 }
83 if (defined($start_results)) {
84 $cmd .= " -startresults " . $start_results;
85 }
86 if (defined($end_results)) {
87 $cmd .= " -endresults " . $end_results;
88 }
89 if (defined($out_file)) {
90 $cmd .= " > \"" . $out_file . "\"";
91 }
92 # print STDERR $cmd . "\n";
93
94 if (!open (PIPEOUT, $cmd)) {
95 die "$PROGNAME - couldn't run $cmd\n";
96 }
97}
98
99sub close_java_lucene
100{
101 close(PIPEOUT);
102}
103
104sub main
105{
106 my (@argv) = @_;
107 my $argc = scalar(@argv);
108 if ($argc == 0) {
109 print STDERR "Usage: $PROGNAME full-index-dir [query] [-fuzziness value] [-filter filter_string] [-sort sort_field] [-dco AND|OR] [-startresults number -endresults number] [-out out_file]\n";
110 exit 1;
111 }
112
113 my $full_indexdir = shift(@argv);
114 my $query = undef;
115 my $fuzziness = undef;
116 my $filter_string = undef;
117 my $sort_field = undef;
118 my $dco = undef;
119 my $start_results = undef;
120 my $end_results = undef;
121 my $out_file = undef;
122 for (my $i = 0; $i < scalar(@argv); $i++)
123 {
124 if ($argv[$i] eq "-fuzziness") {
125 $i++;
126 $fuzziness = $argv[$i];
127 }
128 elsif ($argv[$i] eq "-filter") {
129 $i++;
130 $filter_string = $argv[$i];
131 }
132 elsif ($argv[$i] eq "-sort") {
133 $i++;
134 $sort_field = $argv[$i];
135 }
136 elsif ($argv[$i] eq "-dco") {
137 $i++;
138 $dco = $argv[$i];
139 }
140 elsif ($argv[$i] eq "-startresults") {
141 $i++;
142 $start_results = $argv[$i];
143 }
144 elsif ($argv[$i] eq "-endresults") {
145 $i++;
146 $end_results = $argv[$i];
147 }
148 elsif ($argv[$i] eq "-out") {
149 $i++;
150 $out_file = $argv[$i];
151 }
152 else {
153 $query = $argv[$i];
154 }
155 }
156
157 open_java_lucene($full_indexdir, $fuzziness, $filter_string, $sort_field, $dco, $start_results, $end_results, $out_file);
158
159 if (defined $query) {
160 print PIPEOUT "$query\n";
161 }
162 else {
163 while (defined (my $line = <STDIN>)) {
164 print PIPEOUT $line;
165 }
166 }
167
168 close_java_lucene();
169}
170
171&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.