我有我认为应该是一个非常简单的 CLucene 实验,但它没有返回任何结果。
我有两个独立的程序,CreateIndex 和 Query。
据我所知, CreateIndex 构建了一个可行的索引文件,但 Query 返回零命中。操作系统为 Centos 6.4,CLucene 版本为 2.3.3.4。
这是 CreateIndex.cpp:
lucene::analysis::SimpleAnalyzer* analyzer;
int main(int argc, char** argv)
{
analyzer = new lucene::analysis::SimpleAnalyzer();
Directory* indexDir = FSDirectory::getDirectory("../Index");
IndexWriter* w = new IndexWriter(indexDir, analyzer, true, true);
int config = Field::STORE_YES && Field::INDEX_TOKENIZED;
Field* field;
Document* doc;
doc = new Document();
field = new Field(L"president", L"Nixon", config);
doc->clear();
doc->add(*field);
w->addDocument(doc);
field = new Field(L"president", L"Obama", config);
doc->clear();
doc->add(*field);
w->addDocument(doc);
field = new Field(L"president", L"Clinton", config);
doc->clear();
doc->add(*field);
w->addDocument(doc);
w->close();
indexDir->close();
}
这是 Query.cpp:
int main(int argc, char** argv)
{
IndexReader* reader = IndexReader::open("../Index");
lucene::analysis::SimpleAnalyzer* analyzer =
new lucene::analysis::SimpleAnalyzer();
IndexReader* newreader = reader->reopen();
if ( newreader != reader )
{
_CLLDELETE(reader);
reader = newreader;
}
IndexSearcher searcher(reader);
Query* query = QueryParser::parse(L"Nixon*",
L"president", analyzer);
Hits* hits = searcher.search(query);
cout << "Total hits: " << hits->length() << endl;
}