source: main/trunk/greenstone2/runtime-src/src/colservr/lucenesearch.cpp@ 28758

Last change on this file since 28758 was 27361, checked in by kjdon, 11 years ago

new handling of sortfield query param

  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
RevLine 
[8027]1/**********************************************************************
2 *
3 * lucenesearch.cpp --
4 * Copyright (C) 1999-2002 The New Zealand Digital Library Project
5 *
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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26
27#if defined(GSDL_USE_OBJECTSPACE)
28# include <ospace\std\iostream>
29#elif defined(GSDL_USE_IOS_H)
30# include <iostream.h>
31#else
32# include <iostream>
33#endif
34
[9031]35#include <stdio.h>
[9210]36#include <time.h>
[8027]37
38#include "gsdlconf.h"
[9218]39#include "gsdltools.h"
[8027]40#include "lucenesearch.h"
41#include "fileutil.h"
42#include "queryinfo.h"
43#include "gsdlunicode.h"
44
[9031]45#include "expat_resultset.h"
[8027]46
[9937]47text_t lucenesearchclass::getindexsuffix(const queryparamclass &qp) {
[8027]48 text_t indexsuffix = "index";
[9089]49 // get the first char of the level to be the start of the index name
50 text_t suffix = substr(qp.level.begin(), qp.level.begin()+1);
51 lc(suffix);
[8027]52 text_t ind = qp.index;
53 text_t sub = qp.subcollection;
54 text_t lang = qp.language;
55
56 // collection name not added for Lucene
[9089]57 indexsuffix = filename_cat(indexsuffix, suffix +ind + sub + lang);
[8027]58 return indexsuffix;
59
60}
61
62////////////////////
63// lucenesearch class //
64////////////////////
65
66lucenesearchclass::lucenesearchclass ()
67 : searchclass() {
68
[15594]69 textlevel = "Doc";
[8027]70}
71
72lucenesearchclass::~lucenesearchclass ()
73{
74 if (cache != NULL)
75 {
76 delete cache;
77 cache = NULL;
78 }
79}
80
[15594]81void lucenesearchclass::set_text_level(const text_t &textlevel_arg)
82{
83 textlevel = textlevel_arg;
[8027]84}
85
[16310]86
[8027]87bool lucenesearchclass::search(const queryparamclass &queryparams,
88 queryresultsclass &queryresult) {
89
90#ifdef __WIN32__
91 char basepath[]="";
92#else
93 char basepath[] = "/";
94#endif
95
96 char *indexname = (filename_cat(collectdir, getindexsuffix(queryparams))).getcstr();
97
98 // set default stem method from values originally set on prefs page
99 int defaultStemMethod = 0;
100 if (queryparams.casefolding) {
101 defaultStemMethod |= 1;
102 }
103 if (queryparams.stemming) {
104 defaultStemMethod |= 2;
105 }
[12388]106
[10412]107 text_t utf8querystring = to_utf8(queryparams.querystring);
108
109 text_t escaped_utf8querystring = "";
110 text_t::const_iterator here = utf8querystring.begin();
111 while (here != utf8querystring.end()) {
112 if (*here == '"') escaped_utf8querystring.push_back('\\');
113 escaped_utf8querystring.push_back(*here);
[12276]114 ++here;
[10412]115 }
[8027]116
[12992]117 // Use this to skip lucene_query.pl and access GS2LuceneQuery directly (Java must be on path)
[27332]118 // text_t cmd = "java -classpath \"" + filename_cat(gsdlhome, "bin", "java", "LuceneWrapper3.jar") + "\" org.greenstone.LuceneWrapper3.GS2LuceneQuery ";
[12246]119 text_t cmd = "\"" + filename_cat(gsdlhome, "bin", "script", "lucene_query.pl") + "\"";
[10412]120 cmd += (text_t)" \""+indexname + (text_t)"\" \"" + escaped_utf8querystring + (text_t)"\"";
[12410]121
122 if (!queryparams.filterstring.empty()) {
123 cmd += " -filter \"" + queryparams.filterstring + "\"";
124 }
[27361]125 if (!queryparams.sortfield.empty() && queryparams.sortfield != "rank") {
[12364]126 cmd += " -sort \"" + queryparams.sortfield + "\"";
[12276]127 }
[27061]128 if (queryparams.sortorder == 1) {
129 cmd += " -reverse_sort";
130 }
[12770]131 if (!queryparams.fuzziness.empty()) {
132 cmd += " -fuzziness " + queryparams.fuzziness;
133 }
[12364]134
135 // New code to support configuration of the default conjuction operator
136 // set default Boolean combiner from all/some setting
137 // if match_mode == 1, ie all, default=1 ie AND
138 // if match_mode == 0, ie some, default=0, ie OR
139 if (queryparams.match_mode)
140 {
141 cmd += " -dco AND";
142 }
143
[12655]144 // New code to allow Lucene to do paging of search results. This should
145 // substantially improve performance as we don't have to return all 12000
146 // hits if we only need the first 20!
147 if (queryparams.startresults && queryparams.endresults)
148 {
149 cmd += (text_t)" -startresults " + queryparams.startresults;
150 cmd += (text_t)" -endresults " + queryparams.endresults;
151 }
152
[9210]153 text_t xml_text = "";
[8027]154
[12246]155 // I don't want to do this, but I have to.
156 text_t gsdlhome_env = "GSDLHOME=" + gsdlhome;
157 putenv(gsdlhome_env.getcstr());
158
[9115]159#ifdef __WIN32__
[12246]160 putenv("GSDLOS=windows");
161
[9210]162 //FILE *PIN = _popen(cmd.getcstr(), "r"); // didn't seem to work
[9218]163 cmd = (text_t)"perl -S "+cmd;
164 // we write the result to a file
[9210]165 clock_t this_time = clock();
166 text_t filename = "luc";
167 filename.append(this_time);
168 filename.append(".txt");
[9218]169
[23776]170 // Used concat 'collectdir' with 'filename' but this ran into problems
171 // when run from a CD-ROM.
172 // Since this is Windows only code, now changed to use %TEMP% directory,
173 // a location designed to be writable
174
[24008]175 // Need to set the Apache server to pass the TEMP envvar (or TMP) with
176 // passenv. Little point using _tempnam() function that generates a
177 // filename and where you need to pass in the dir if *TMP* doesn't exist.
178 // http://msdn.microsoft.com/en-us/library/hs3e7355%28v=vs.80%29.aspx
179 // dir parameter: "The path used in the file name if there is no TMP
180 // environment variable, or if TMP is not a valid directory." Seems
181 // the inverse of what we want: to use TMP/TEMP if collectdir is invalid.
[23776]182
[24008]183 text_t out_file = filename_cat(getenv("TEMP"),filename); // TMP or TEMP
184
[12374]185 cmd += " -out \"" + out_file + "\"";
[9218]186 int rv = gsdl_system(cmd, true, cerr);
187 if (rv != 0) {
188 cerr << "tried to run command \""<<cmd<<"\", but it failed\n";
189 } else {
190 read_file(out_file, xml_text);
191 remove(out_file.getcstr()); // now delete it
192 }
[9115]193#else
[12246]194
[18458]195#if defined(MACOS)
196 putenv("GSDLOS=darwin");
197#else
198 putenv("GSDLOS=linux");
199#endif
200
201 // What about Solaris etc ????
202
203
[9115]204 FILE *PIN = popen(cmd.getcstr(), "r");
[9210]205
[8027]206 if (PIN==NULL) {
[9210]207 perror("PIPE");
[8027]208 cerr << "Error: unable to open pipe to " << cmd << endl;
[9210]209
[8027]210 return false;
211 }
212 while (!feof(PIN)) {
213 char buffer[256];
214 int num_bytes = fread(buffer,1,256,PIN);
215 xml_text.appendcarr(buffer,num_bytes);
216 }
217
[9210]218#endif
[12246]219
[9031]220 expat_resultset(xml_text,queryresult);
[9218]221
[9115]222#ifdef __WIN32__
[9210]223 // _pclose(PIN);
[9115]224#else
[8027]225 pclose(PIN);
[9115]226#endif
[8027]227
228 return true;
[9218]229}
[8027]230
231
232bool lucenesearchclass::browse_search(const queryparamclass &queryparams,
233 int start, int numDocs,
234 queryresultsclass &queryresult) {
235
236 cerr << "**** Not sure what this function does!" << endl;
237 return false;
238}
239
240// the document text for 'docnum' is placed in 'output'
241// docTargetDocument returns 'true' if it was able to
242// try to get a document
243// collection is needed to see if an index from the
244// collection is loaded. THe default index bits are just there cos
245// the mg version needs them
246
247bool lucenesearchclass::docTargetDocument(const text_t &/*defaultindex*/,
248 const text_t &/*defaultsubcollection*/,
249 const text_t &/*defaultlanguage*/,
250 const text_t &collection,
251 int docnum,
252 text_t &output) {
253
[15594]254 // we now get the document directly by lucenesource, so don't use this
[9915]255 // method
[8027]256 return false;
257}
258
259// used to clear any cached databases for persistent versions of
260// Greenstone like the Windows local library
261void lucenesearchclass::unload_database () {
262}
263
[12246]264void lucenesearchclass::set_gsdlhome (const text_t &gh)
265{
266 gsdlhome = gh;
267}
Note: See TracBrowser for help on using the repository browser.