source: main/trunk/greenstone2/bin/script/lucene_query.pl@ 31747

Last change on this file since 31747 was 31747, checked in by ak19, 7 years ago

When we settle on a JRE (which could be the bundled JRE), only JRE_HOME is now exported. That should be the path to the java executable that is found. Need to rethink whether we export JAVA_HOME too as JRE_HOME in future if we settle on a JRE, just to work with our code where JAVA_HOME is expected.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
RevLine 
[8520]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
[12373]35use strict;
[8520]36use util;
37
[12373]38my $PROGNAME = $0;
39$PROGNAME =~ s/^.*\/(.*)$/$1/;
[8520]40
41
[12848]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 }
[31747]50 elsif (defined $ENV{'JRE_HOME'}) {
51 my $jre_home = $ENV{'JRE_HOME'};
52 $jre_home =~ s/\/$//; # Remove trailing slash if present (Unix specific)
53 return &util::filename_cat($jre_home, "bin", "java");
54 }
[12848]55
56 # Hope that Java is on the PATH
57 return "java";
58}
59
[8520]60sub open_java_lucene
61{
[12275]62 my $full_indexdir = shift(@_);
[12770]63 my $fuzziness = shift(@_);
[12408]64 my $filter_string = shift(@_);
[12373]65 my $sort_field = shift(@_);
[27069]66 my $reverse_sort = shift(@_);
[12373]67 my $dco = shift(@_);
[12656]68 my $start_results = shift(@_);
69 my $end_results = shift(@_);
[12373]70 my $out_file = shift(@_);
[12364]71
[12848]72 my $java = &get_java_path();
[29144]73 my $classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "LuceneWrapper4.jar");
74 my $java_lucene = "\"$java\" -classpath \"$classpath\" org.greenstone.LuceneWrapper4.GS2LuceneQuery";
[8520]75
[12373]76 my $cmd = "| " . $java_lucene . " \"" . $full_indexdir . "\"";
[12770]77 if (defined($fuzziness)) {
78 $cmd .= " -fuzziness " . $fuzziness;
[9219]79 }
[12408]80 if (defined($filter_string)) {
81 $cmd .= " -filter \"" . $filter_string . "\"";
82 }
[12373]83 if (defined($sort_field)) {
[12364]84 $cmd .= " -sort " . $sort_field;
[12373]85 }
[27069]86 if ($reverse_sort) {
87 $cmd .= " -reverse_sort";
88 }
[12373]89 if (defined($dco)) {
[12364]90 $cmd .= " -dco " . $dco;
[12373]91 }
[12656]92 if (defined($start_results)) {
93 $cmd .= " -startresults " . $start_results;
94 }
95 if (defined($end_results)) {
96 $cmd .= " -endresults " . $end_results;
97 }
[12373]98 if (defined($out_file)) {
99 $cmd .= " > \"" . $out_file . "\"";
100 }
[27069]101 print STDERR $cmd . "\n";
[12364]102
103 if (!open (PIPEOUT, $cmd)) {
[12373]104 die "$PROGNAME - couldn't run $cmd\n";
[8520]105 }
106}
107
108sub close_java_lucene
109{
110 close(PIPEOUT);
111}
112
113sub main
114{
115 my (@argv) = @_;
116 my $argc = scalar(@argv);
[12373]117 if ($argc == 0) {
[27069]118 print STDERR "Usage: $PROGNAME full-index-dir [query] [-fuzziness value] [-filter filter_string] [-sort sort_field] [-reverse_sort] [-dco AND|OR] [-startresults number -endresults number] [-out out_file]\n";
[12373]119 exit 1;
120 }
[8520]121
[12373]122 my $full_indexdir = shift(@argv);
[8520]123 my $query = undef;
[12770]124 my $fuzziness = undef;
[12408]125 my $filter_string = undef;
[12275]126 my $sort_field = undef;
[27069]127 my $reverse_sort = 0;
[12373]128 my $dco = undef;
[12656]129 my $start_results = undef;
130 my $end_results = undef;
[9219]131 my $out_file = undef;
[12373]132 for (my $i = 0; $i < scalar(@argv); $i++)
133 {
[12770]134 if ($argv[$i] eq "-fuzziness") {
135 $i++;
136 $fuzziness = $argv[$i];
[12373]137 }
[12408]138 elsif ($argv[$i] eq "-filter") {
139 $i++;
140 $filter_string = $argv[$i];
141 }
[12373]142 elsif ($argv[$i] eq "-sort") {
143 $i++;
144 $sort_field = $argv[$i];
145 }
[27069]146 elsif ($argv[$i] eq "-reverse_sort") {
147 $reverse_sort = 1;
148 }
[12373]149 elsif ($argv[$i] eq "-dco") {
150 $i++;
151 $dco = $argv[$i];
152 }
[12656]153 elsif ($argv[$i] eq "-startresults") {
154 $i++;
155 $start_results = $argv[$i];
156 }
157 elsif ($argv[$i] eq "-endresults") {
158 $i++;
159 $end_results = $argv[$i];
160 }
[12373]161 elsif ($argv[$i] eq "-out") {
162 $i++;
163 $out_file = $argv[$i];
164 }
165 else {
166 $query = $argv[$i];
167 }
[8520]168 }
169
[27069]170 open_java_lucene($full_indexdir, $fuzziness, $filter_string, $sort_field, $reverse_sort, $dco, $start_results, $end_results, $out_file);
[12373]171
[8520]172 if (defined $query) {
173 print PIPEOUT "$query\n";
174 }
175 else {
176 while (defined (my $line = <STDIN>)) {
177 print PIPEOUT $line;
178 }
179 }
180
181 close_java_lucene();
182}
183
184&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.