// BK> all test functions are the same except RAMDirectory constructor, so shared code moved here void checkDir(CuTest *tc, MockRAMDirectory * ramDir) { // Check size CuAssertTrue(tc, ramDir->sizeInBytes == ramDir->getRecomputedSizeInBytes(), _T("RAMDir size")); // open reader to test document count IndexReader * reader = IndexReader::open(ramDir); CuAssertEquals(tc, docsToAdd, reader->numDocs(), _T("document count")); // open search to check if all doc's are there IndexSearcher * searcher = _CLNEW IndexSearcher(reader); // search for all documents Document doc; for (int i = 0; i < docsToAdd; i++) { searcher->doc(i, doc); CuAssertTrue(tc, doc.getField(_T("content")) != NULL, _T("content is NULL")); } // cleanup reader->close(); searcher->close(); _CLLDELETE(reader); _CLLDELETE(searcher); }
void searchPhrase(IndexSearcher &is, const string &qStr, bool fuzzy) { string queryString = qStr; BoolAnalyzer analyzer; const Query *pQuery1 = QueryParser::parsePhrase( queryString, "text", analyzer, fuzzy); Query *pQuery2 = QueryParser::parsePhrase( queryString, "title", analyzer, fuzzy); pQuery2->setBoost(10); const Query *query = new OrQuery(*pQuery1, *pQuery2); cout << "Inner Query: " << query->toString() << endl; vector<ScoreDoc> scoreDocs = is.search(*query); cout << "Hit Numbers: " << scoreDocs.size() << endl; cout << "Hit Documents:" << endl; for (size_t i = 0; i < scoreDocs.size(); i ++) { fprintf(stdout, "%4ld. #%-4ld ", i + 1, scoreDocs[i].id()); cout << is.doc(scoreDocs[i].id()).toString() << endl; } delete query; }