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

Last change on this file since 29144 was 29144, checked in by ak19, 10 years ago

Part of port from lucene3.3.0 to lucene4.7.2. LuceneWrapper related. Changes to gs2build/greenstone 2's perllib and bin/script lucene related perl scripts, to switch over from using Lucene3Wrapper to Lucene4Wrapper

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