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

Last change on this file since 31888 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
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 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 }
55
56 # Hope that Java is on the PATH
57 return "java";
58}
59
60sub open_java_lucene
61{
62 my $full_indexdir = shift(@_);
63 my $fuzziness = shift(@_);
64 my $filter_string = shift(@_);
65 my $sort_field = shift(@_);
66 my $reverse_sort = shift(@_);
67 my $dco = shift(@_);
68 my $start_results = shift(@_);
69 my $end_results = shift(@_);
70 my $out_file = shift(@_);
71
72 my $java = &get_java_path();
73 my $classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "LuceneWrapper4.jar");
74 my $java_lucene = "\"$java\" -classpath \"$classpath\" org.greenstone.LuceneWrapper4.GS2LuceneQuery";
75
76 my $cmd = "| " . $java_lucene . " \"" . $full_indexdir . "\"";
77 if (defined($fuzziness)) {
78 $cmd .= " -fuzziness " . $fuzziness;
79 }
80 if (defined($filter_string)) {
81 $cmd .= " -filter \"" . $filter_string . "\"";
82 }
83 if (defined($sort_field)) {
84 $cmd .= " -sort " . $sort_field;
85 }
86 if ($reverse_sort) {
87 $cmd .= " -reverse_sort";
88 }
89 if (defined($dco)) {
90 $cmd .= " -dco " . $dco;
91 }
92 if (defined($start_results)) {
93 $cmd .= " -startresults " . $start_results;
94 }
95 if (defined($end_results)) {
96 $cmd .= " -endresults " . $end_results;
97 }
98 if (defined($out_file)) {
99 $cmd .= " > \"" . $out_file . "\"";
100 }
101 print STDERR $cmd . "\n";
102
103 if (!open (PIPEOUT, $cmd)) {
104 die "$PROGNAME - couldn't run $cmd\n";
105 }
106}
107
108sub close_java_lucene
109{
110 close(PIPEOUT);
111}
112
113sub main
114{
115 my (@argv) = @_;
116 my $argc = scalar(@argv);
117 if ($argc == 0) {
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";
119 exit 1;
120 }
121
122 my $full_indexdir = shift(@argv);
123 my $query = undef;
124 my $fuzziness = undef;
125 my $filter_string = undef;
126 my $sort_field = undef;
127 my $reverse_sort = 0;
128 my $dco = undef;
129 my $start_results = undef;
130 my $end_results = undef;
131 my $out_file = undef;
132 for (my $i = 0; $i < scalar(@argv); $i++)
133 {
134 if ($argv[$i] eq "-fuzziness") {
135 $i++;
136 $fuzziness = $argv[$i];
137 }
138 elsif ($argv[$i] eq "-filter") {
139 $i++;
140 $filter_string = $argv[$i];
141 }
142 elsif ($argv[$i] eq "-sort") {
143 $i++;
144 $sort_field = $argv[$i];
145 }
146 elsif ($argv[$i] eq "-reverse_sort") {
147 $reverse_sort = 1;
148 }
149 elsif ($argv[$i] eq "-dco") {
150 $i++;
151 $dco = $argv[$i];
152 }
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 }
161 elsif ($argv[$i] eq "-out") {
162 $i++;
163 $out_file = $argv[$i];
164 }
165 else {
166 $query = $argv[$i];
167 }
168 }
169
170 open_java_lucene($full_indexdir, $fuzziness, $filter_string, $sort_field, $reverse_sort, $dco, $start_results, $end_results, $out_file);
171
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.