[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] | 47 | text_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 |
|
---|
| 66 | lucenesearchclass::lucenesearchclass ()
|
---|
| 67 | : searchclass() {
|
---|
| 68 |
|
---|
| 69 | gdbm_level = "Doc";
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | lucenesearchclass::~lucenesearchclass ()
|
---|
| 73 | {
|
---|
| 74 | if (cache != NULL)
|
---|
| 75 | {
|
---|
| 76 | delete cache;
|
---|
| 77 | cache = NULL;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void lucenesearchclass::set_gdbm_level(const text_t &level) {
|
---|
| 82 | gdbm_level = level;
|
---|
| 83 |
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 |
|
---|
| 87 | bool lucenesearchclass::search(const queryparamclass &queryparams,
|
---|
| 88 | queryresultsclass &queryresult) {
|
---|
| 89 |
|
---|
| 90 | #ifdef __WIN32__
|
---|
| 91 | char basepath[]="";
|
---|
| 92 | #else
|
---|
| 93 | char basepath[] = "/";
|
---|
| 94 | #endif
|
---|
| 95 |
|
---|
[9218] | 96 | cerr << "**** in lucene search" << endl;
|
---|
[8027] | 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 | }
|
---|
[10412] | 116 |
|
---|
| 117 | text_t utf8querystring = to_utf8(queryparams.querystring);
|
---|
[8027] | 118 | cerr << "**** query string = " << utf8querystring << endl;
|
---|
[10412] | 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;
|
---|
[8027] | 128 | cerr << "***** index name = " << indexname << endl;
|
---|
| 129 |
|
---|
[12246] | 130 | text_t cmd = "\"" + filename_cat(gsdlhome, "bin", "script", "lucene_query.pl") + "\"";
|
---|
[10412] | 131 | cmd += (text_t)" \""+indexname + (text_t)"\" \"" + escaped_utf8querystring + (text_t)"\"";
|
---|
| 132 | cerr << "Lucene command: " << cmd << endl;
|
---|
[9210] | 133 |
|
---|
| 134 | text_t xml_text = "";
|
---|
[8027] | 135 |
|
---|
[12246] | 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 |
|
---|
[9115] | 140 | #ifdef __WIN32__
|
---|
[12246] | 141 | putenv("GSDLOS=windows");
|
---|
| 142 |
|
---|
[9210] | 143 | //FILE *PIN = _popen(cmd.getcstr(), "r"); // didn't seem to work
|
---|
[9218] | 144 | cmd = (text_t)"perl -S "+cmd;
|
---|
| 145 | // we write the result to a file
|
---|
[9210] | 146 | clock_t this_time = clock();
|
---|
| 147 | text_t filename = "luc";
|
---|
| 148 | filename.append(this_time);
|
---|
| 149 | filename.append(".txt");
|
---|
[9218] | 150 |
|
---|
[9210] | 151 | text_t out_file = filename_cat(collectdir, filename);
|
---|
[9218] | 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 | }
|
---|
[9115] | 160 | #else
|
---|
[12246] | 161 | putenv("GSDLOS=linux");
|
---|
| 162 |
|
---|
[9115] | 163 | FILE *PIN = popen(cmd.getcstr(), "r");
|
---|
[9210] | 164 |
|
---|
[8027] | 165 | if (PIN==NULL) {
|
---|
[9210] | 166 | perror("PIPE");
|
---|
[8027] | 167 | cerr << "Error: unable to open pipe to " << cmd << endl;
|
---|
[9210] | 168 |
|
---|
[8027] | 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 |
|
---|
[9210] | 177 | #endif
|
---|
[12246] | 178 |
|
---|
[9031] | 179 | expat_resultset(xml_text,queryresult);
|
---|
[9218] | 180 |
|
---|
[9115] | 181 | #ifdef __WIN32__
|
---|
[9210] | 182 | // _pclose(PIN);
|
---|
[9115] | 183 | #else
|
---|
[8027] | 184 | pclose(PIN);
|
---|
[9115] | 185 | #endif
|
---|
[8027] | 186 |
|
---|
| 187 | return true;
|
---|
[9218] | 188 | }
|
---|
[8027] | 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;
|
---|
[9620] | 234 | for (int i=0; i<(int)queryResult.docs.size(); ++i) {
|
---|
[8027] | 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;
|
---|
[9620] | 245 | for (int k=0; k<(int)queryResult.termFreqs.size(); ++k) {
|
---|
[8027] | 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 |
|
---|
[9620] | 256 | for (int j=0; j<(int)queryResult.termFreqs[k].equivTerms.size(); ++j) {
|
---|
[8027] | 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 |
|
---|
| 269 | bool 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;
|
---|
[9620] | 309 | for (int i=0; i<(int)browseResult.termFreqs.size(); ++i) {
|
---|
[8027] | 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 |
|
---|
| 337 | bool 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 |
|
---|
[9915] | 344 | // we now get the document directly by lucenegdbmsource, so don't use this
|
---|
| 345 | // method
|
---|
[8027] | 346 | return false;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | // used to clear any cached databases for persistent versions of
|
---|
| 350 | // Greenstone like the Windows local library
|
---|
| 351 | void lucenesearchclass::unload_database () {
|
---|
| 352 | }
|
---|
| 353 |
|
---|
[12246] | 354 | void lucenesearchclass::set_gsdlhome (const text_t &gh)
|
---|
| 355 | {
|
---|
| 356 | gsdlhome = gh;
|
---|
| 357 | }
|
---|