root/main/trunk/greenstone2/bin/script/lucene_query.pl @ 29144

Revision 29144, 4.8 KB (checked in by ak19, 6 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
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 $reverse_sort = shift(@_);
62    my $dco = shift(@_);
63    my $start_results = shift(@_);
64    my $end_results = shift(@_);
65    my $out_file = shift(@_);
66
67    my $java = &get_java_path();
68    my $classpath = &util::filename_cat($ENV{'GSDLHOME'}, "bin", "java", "LuceneWrapper4.jar");
69    my $java_lucene = "\"$java\" -classpath \"$classpath\" org.greenstone.LuceneWrapper4.GS2LuceneQuery";
70
71    my $cmd = "| " . $java_lucene . " \"" . $full_indexdir . "\"";
72    if (defined($fuzziness)) {
73        $cmd .= " -fuzziness " . $fuzziness;
74    }
75    if (defined($filter_string)) {
76    $cmd .= " -filter \"" . $filter_string . "\"";
77    }
78    if (defined($sort_field)) {
79        $cmd .= " -sort " . $sort_field;
80    }
81    if ($reverse_sort) {
82    $cmd .= " -reverse_sort";
83    }
84    if (defined($dco)) {
85        $cmd .= " -dco " . $dco;
86    }
87    if (defined($start_results)) {
88        $cmd .= " -startresults " . $start_results;
89    }
90    if (defined($end_results)) {
91        $cmd .= " -endresults " . $end_results;
92    }
93    if (defined($out_file)) {
94    $cmd .= " > \"" . $out_file . "\"";
95    }
96     print STDERR $cmd . "\n";
97
98    if (!open (PIPEOUT, $cmd)) {
99    die "$PROGNAME - couldn't run $cmd\n";
100    }
101}
102
103sub close_java_lucene
104{
105    close(PIPEOUT);
106}
107
108sub main
109{
110    my (@argv) = @_;
111    my $argc = scalar(@argv);
112    if ($argc == 0) {
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";
114    exit 1;
115    }
116
117    my $full_indexdir = shift(@argv);
118    my $query = undef;
119    my $fuzziness = undef;
120    my $filter_string = undef;
121    my $sort_field = undef;
122    my $reverse_sort = 0;
123    my $dco = undef;
124    my $start_results = undef;
125    my $end_results = undef;
126    my $out_file = undef;
127    for (my $i = 0; $i < scalar(@argv); $i++)
128    {
129    if ($argv[$i] eq "-fuzziness") {
130        $i++;
131        $fuzziness = $argv[$i];
132    }
133        elsif ($argv[$i] eq "-filter") {
134            $i++;
135            $filter_string = $argv[$i];
136    }
137        elsif ($argv[$i] eq "-sort") {
138            $i++;
139            $sort_field = $argv[$i];
140    }
141    elsif ($argv[$i] eq "-reverse_sort") {
142        $reverse_sort = 1;
143    }
144        elsif ($argv[$i] eq "-dco") {
145            $i++;
146            $dco = $argv[$i];
147    }
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    }
156        elsif ($argv[$i] eq "-out") {
157            $i++;
158            $out_file = $argv[$i];
159    }
160        else {
161            $query = $argv[$i];
162    }
163    }
164
165    open_java_lucene($full_indexdir, $fuzziness, $filter_string, $sort_field, $reverse_sort, $dco, $start_results, $end_results, $out_file);
166
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 browser.