source: trunk/gsdl/src/colservr/lucenesearch.cpp@ 12246

Last change on this file since 12246 was 12246, checked in by mdewsnip, 18 years ago

GSDLHOME and GSDLOS environment variables need to be set for lucene_query.pl. Thanks to John Thompson and DL Consulting Ltd.

  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
Line 
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
35#include <stdio.h>
36#include <time.h>
37
38#include "gsdlconf.h"
39#include "gsdltools.h"
40#include "lucenesearch.h"
41#include "fileutil.h"
42#include "queryinfo.h"
43#include "gsdlunicode.h"
44
45#include "expat_resultset.h"
46
47text_t lucenesearchclass::getindexsuffix(const queryparamclass &qp) {
48 text_t indexsuffix = "index";
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);
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
57 indexsuffix = filename_cat(indexsuffix, suffix +ind + sub + lang);
58 return indexsuffix;
59
60}
61
62////////////////////
63// lucenesearch class //
64////////////////////
65
66lucenesearchclass::lucenesearchclass ()
67 : searchclass() {
68
69 gdbm_level = "Doc";
70}
71
72lucenesearchclass::~lucenesearchclass ()
73{
74 if (cache != NULL)
75 {
76 delete cache;
77 cache = NULL;
78 }
79}
80
81void lucenesearchclass::set_gdbm_level(const text_t &level) {
82 gdbm_level = level;
83
84}
85
86
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 cerr << "**** in lucene search" << endl;
97
98 char *indexname = (filename_cat(collectdir, getindexsuffix(queryparams))).getcstr();
99
100 // set default stem method from values originally set on prefs page
101 int defaultStemMethod = 0;
102 if (queryparams.casefolding) {
103 defaultStemMethod |= 1;
104 }
105 if (queryparams.stemming) {
106 defaultStemMethod |= 2;
107 }
108
109 // set default Boolean combiner from all/some setting
110 // if match_mode == 1, ie all, default=1 ie AND
111 // if match_mode == 0, ie some, default=0, ie OR
112 int defaultBoolCombine = 0;
113 if (queryparams.match_mode){
114 defaultBoolCombine = 1;
115 }
116
117 text_t utf8querystring = to_utf8(queryparams.querystring);
118 cerr << "**** query string = " << utf8querystring << endl;
119
120 text_t escaped_utf8querystring = "";
121 text_t::const_iterator here = utf8querystring.begin();
122 while (here != utf8querystring.end()) {
123 if (*here == '"') escaped_utf8querystring.push_back('\\');
124 escaped_utf8querystring.push_back(*here);
125 here++;
126 }
127 cerr << "**** escaped query string = " << escaped_utf8querystring << endl;
128 cerr << "***** index name = " << indexname << endl;
129
130 text_t cmd = "\"" + filename_cat(gsdlhome, "bin", "script", "lucene_query.pl") + "\"";
131 cmd += (text_t)" \""+indexname + (text_t)"\" \"" + escaped_utf8querystring + (text_t)"\"";
132 cerr << "Lucene command: " << cmd << endl;
133
134 text_t xml_text = "";
135
136 // I don't want to do this, but I have to.
137 text_t gsdlhome_env = "GSDLHOME=" + gsdlhome;
138 putenv(gsdlhome_env.getcstr());
139
140#ifdef __WIN32__
141 putenv("GSDLOS=windows");
142
143 //FILE *PIN = _popen(cmd.getcstr(), "r"); // didn't seem to work
144 cmd = (text_t)"perl -S "+cmd;
145 // we write the result to a file
146 clock_t this_time = clock();
147 text_t filename = "luc";
148 filename.append(this_time);
149 filename.append(".txt");
150
151 text_t out_file = filename_cat(collectdir, filename);
152 cmd += (text_t)" \""+out_file+ (text_t)"\"";
153 int rv = gsdl_system(cmd, true, cerr);
154 if (rv != 0) {
155 cerr << "tried to run command \""<<cmd<<"\", but it failed\n";
156 } else {
157 read_file(out_file, xml_text);
158 remove(out_file.getcstr()); // now delete it
159 }
160#else
161 putenv("GSDLOS=linux");
162
163 FILE *PIN = popen(cmd.getcstr(), "r");
164
165 if (PIN==NULL) {
166 perror("PIPE");
167 cerr << "Error: unable to open pipe to " << cmd << endl;
168
169 return false;
170 }
171 while (!feof(PIN)) {
172 char buffer[256];
173 int num_bytes = fread(buffer,1,256,PIN);
174 xml_text.appendcarr(buffer,num_bytes);
175 }
176
177#endif
178
179 expat_resultset(xml_text,queryresult);
180
181#ifdef __WIN32__
182 // _pclose(PIN);
183#else
184 pclose(PIN);
185#endif
186
187 return true;
188}
189 /*
190 // use default query info settings - change to reflect user preferences??
191 QueryInfo queryInfo;
192
193 SetCStr (queryInfo.docLevel, (queryparams.level.getcstr()));
194 queryInfo.maxDocs = (unsigned long)queryparams.maxdocs;
195 queryInfo.sortByRank = (queryparams.search_type == 1);
196 queryInfo.exactWeights = false;
197 queryInfo.needRankInfo = true; // used for overall term freq as well as ranking
198 queryInfo.needTermFreqs = true;
199
200 ExtQueryResult queryResult;
201
202 UCArray queryArray;
203 // greenstone gives us the query encoded in unicode. We want utf8.
204 char* utf8querystring=to_utf8(queryparams.querystring).getcstr();
205 SetCStr(queryArray, utf8querystring);
206 delete utf8querystring;
207
208 UCArray level;
209 UCArrayClear(level);
210
211 //set the level for results
212 SetCStr(level, gdbm_level.getcstr());
213
214
215 // do the query
216 // LuceneQuery(*indexData, queryInfo, queryTree, queryResult, level); // ****
217
218
219 // convert ExtQueryResult to queryresultclass
220
221 queryresult.docs_matched = (int)queryResult.docs.size();
222
223 if (queryresult.docs_matched == (int)queryResult.actualNumDocs) {
224 queryresult.is_approx = Exact;
225 }
226 else if (queryresult.docs_matched < (int)queryResult.actualNumDocs) {
227 queryresult.is_approx = MoreThan;
228 }
229 else {
230 queryresult.is_approx = Approximate;
231 }
232
233 docresultclass doc;
234 for (int i=0; i<(int)queryResult.docs.size(); ++i) {
235 doc.clear();
236 doc.docnum = (int)queryResult.levels[i];
237 doc.docweight = queryResult.ranks[i];
238 queryresult.docs.docset[doc.docnum] = doc;
239 queryresult.docs.docorder.push_back(doc.docnum);
240
241 }
242
243 // term info
244 termfreqclass term;
245 for (int k=0; k<(int)queryResult.termFreqs.size(); ++k) {
246 term.clear();
247 char* termfreq_cstr=GetCStr(queryResult.termFreqs[k].term);
248 term.termstr = to_uni(termfreq_cstr);
249 delete termfreq_cstr;
250 term.termstemstr = term.termstr;
251 // we don't set term.utf8equivterms ?? - jrm21
252 term.termfreq = queryResult.termFreqs[k].termFreq;
253 queryresult.terms.push_back(term);
254 queryresult.orgterms.push_back(term); // should this change??
255
256 for (int j=0; j<(int)queryResult.termFreqs[k].equivTerms.size(); ++j) {
257 char* equivterm_cstr=GetCStr(queryResult.termFreqs[k].equivTerms[j]);
258 queryresult.termvariants.insert(to_uni(equivterm_cstr));
259 delete equivterm_cstr;
260 }
261
262 }
263 // clean up
264 delete indexname;
265 return true;
266 */
267
268
269bool lucenesearchclass::browse_search(const queryparamclass &queryparams,
270 int start, int numDocs,
271 queryresultsclass &queryresult) {
272
273 cerr << "**** Not sure what this function does!" << endl;
274
275 /*
276#ifdef __WIN32__
277 char basepath[]="";
278#else
279 char basepath[] = "/";
280#endif
281
282 char *indexname = (filename_cat(collectdir, getindexsuffix(queryparams))).getcstr();
283
284 UCArray level;
285 UCArrayClear(level);
286
287 //browse always at top level
288 SetCStr(level, "Doc"); // this name may change.
289
290
291 BrowseQueryNode browseNode;
292 browseNode.startPosition = start;
293 browseNode.numTerms = numDocs;
294
295 BrowseQueryResult browseResult;
296
297
298 UCArrayClear(browseNode.term);
299 // greenstone gives us the query encoded in unicode. We want utf8.
300 char* utf8querystring=to_utf8(queryparams.querystring).getcstr();
301 SetCStr(browseNode.term, utf8querystring);
302 delete utf8querystring;
303
304 // do the actual query
305 // LuceneBrowseQuery(*indexData, level, browseNode, browseResult); // ****
306
307 // load results into term info
308 termfreqclass term;
309 for (int i=0; i<(int)browseResult.termFreqs.size(); ++i) {
310 term.clear();
311 char* term_cstr = GetCStr(browseResult.termFreqs[i].term);
312 term.termstr = to_uni(term_cstr);
313 delete term_cstr;
314 term.termstemstr = term.termstr;
315 term.termfreq = browseResult.termFreqs[i].termFreq;
316 queryresult.terms.push_back(term);
317 queryresult.orgterms.push_back(term);
318
319 }
320 // clean up
321 delete indexname;
322
323 return true;
324
325 */
326
327 return false;
328}
329
330// the document text for 'docnum' is placed in 'output'
331// docTargetDocument returns 'true' if it was able to
332// try to get a document
333// collection is needed to see if an index from the
334// collection is loaded. THe default index bits are just there cos
335// the mg version needs them
336
337bool lucenesearchclass::docTargetDocument(const text_t &/*defaultindex*/,
338 const text_t &/*defaultsubcollection*/,
339 const text_t &/*defaultlanguage*/,
340 const text_t &collection,
341 int docnum,
342 text_t &output) {
343
344 // we now get the document directly by lucenegdbmsource, so don't use this
345 // method
346 return false;
347}
348
349// used to clear any cached databases for persistent versions of
350// Greenstone like the Windows local library
351void lucenesearchclass::unload_database () {
352}
353
354void lucenesearchclass::set_gsdlhome (const text_t &gh)
355{
356 gsdlhome = gh;
357}
Note: See TracBrowser for help on using the repository browser.