Updating clucene searcher

This commit is contained in:
synhershko 2011-08-02 16:14:53 +00:00
parent 4b6caa48ce
commit b146d87158
2 changed files with 64 additions and 12 deletions

View File

@ -21,34 +21,78 @@
namespace kiwix { namespace kiwix {
IndexSearcher* CluceneSearcher::searcher = NULL;
Directory* CluceneSearcher::dir = NULL;
TCHAR buffer[MAX_BUFFER_SIZE]; TCHAR buffer[MAX_BUFFER_SIZE];
/* Constructor */ /* Constructor */
CluceneSearcher::CluceneSearcher(const string &cluceneDirectoryPath) CluceneSearcher::CluceneSearcher(const string &cluceneDirectoryPath)
: kiwix::Searcher() { : kiwix::Searcher() {
this->openIndex(cluceneDirectoryPath); if (searcher == NULL)
this->openIndex(cluceneDirectoryPath);
} }
/* Open Clucene readable database */ /* Open Clucene readable database */
void CluceneSearcher::openIndex(const string &directoryPath) { void CluceneSearcher::openIndex(const string &directoryPath) {
this->reader = IndexReader::open(directoryPath.c_str()); cout << "Open index folder at " << directoryPath << endl;
dir = FSDirectory::getDirectory(directoryPath.c_str(), false);
searcher = new IndexSearcher(dir);
} }
/* Close Clucene writable database */ /* Close Clucene writable database */
void CluceneSearcher::closeIndex() { void CluceneSearcher::closeIndex() {
return;
} }
void CluceneSearcher::terminate()
{
dir->close();
searcher->close();
delete searcher;
_CLLDECDELETE(dir);
}
std::string toString(const TCHAR* s){
int32_t len = _tcslen(s);
char* buf = new char[len+1];
STRCPY_WtoA(buf,s,len+1);
string ret = buf;
delete[] buf;
return ret;
}
/* Search strings in the database */ /* Search strings in the database */
void CluceneSearcher::searchInIndex(string &search, const unsigned int resultStart, void CluceneSearcher::searchInIndex(string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose) { const unsigned int resultEnd, const bool verbose) {
IndexSearcher searcher(reader);
QueryParser parser(_T("content"), &analyzer); // Parse query
//STRCPY_AtoT(buffer, search.c_str(), MAX_BUFFER_SIZE); lucene::analysis::standard::StandardAnalyzer* analyzer = new lucene::analysis::standard::StandardAnalyzer();
::mbstowcs(buffer,search.c_str(),MAX_BUFFER_SIZE); QueryParser* parser = new QueryParser(_T("content"), analyzer);
Query* query = parser.parse(buffer); STRCPY_AtoT(buffer, search.c_str(), MAX_BUFFER_SIZE);
Hits* hits = searcher.search(query);
cout << "--------------------------------" << hits->length() << endl; //lucene::util::Misc::_cpycharToWide();
//::mbstowcs(buffer,search.c_str(),MAX_BUFFER_SIZE);
//search.c_str();
Query* query = parser->parse(_T("between"));
cout << "Query: " << search << endl;
wcout << "Buffer: " << buffer;
cout << endl;
const wchar_t* querystring = query->toString();
wcout << L"Query2string: " << querystring << endl;
//string q = toString(querystring);
//STRCPY_TtoA(querystring, buffer, MAX_BUFFER_SIZE);
//cout << "Query object: " << q << endl;
delete[] querystring;
delete parser;
delete analyzer;
// Search
Hits* hits = searcher->search(query);
cout << "Hits length:" << hits->length() << endl;
/* /*
for (size_t i=0; i < hits->length() && i<10; i++) { for (size_t i=0; i < hits->length() && i<10; i++) {
@ -67,6 +111,12 @@ namespace kiwix {
this->results.push_back(result); this->results.push_back(result);
*/ */
searcher->close();
delete searcher;
delete hits;
delete query;
return; return;
} }
} }

View File

@ -44,12 +44,14 @@ namespace kiwix {
void searchInIndex(string &search, const unsigned int resultStart, void searchInIndex(string &search, const unsigned int resultStart,
const unsigned int resultEnd, const bool verbose=false); const unsigned int resultEnd, const bool verbose=false);
static void terminate();
protected: protected:
void closeIndex(); void closeIndex();
void openIndex(const string &cluceneDirectoryPath); void openIndex(const string &cluceneDirectoryPath);
IndexReader* reader; static IndexSearcher* searcher;
lucene::analysis::standard::StandardAnalyzer analyzer; static Directory* dir;
}; };