Simple CLucene example query/search returns no hits
Asked Answered
H

0

6

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;
}
Headed answered 22/3, 2015 at 14:34 Comment(1)
I should have used bitwise-OR on the field configuration, instead of &&. Works now.Headed

© 2022 - 2024 — McMap. All rights reserved.