Simple CLucene example query/search returns no hits

1k views Asked by At

I have what I think should be a very simple CLucene experiment, but it returns no hits.

I have two separate programs, CreateIndex and Query.

As far as I can tell, CreateIndex builds a viable index file, but Query returns zero hits. OS is Centos 6.4, CLucene version is 2.3.3.4.

Here is 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();
}

And here is 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;
}
0

There are 0 answers