0
votes

Running version 5.2.1 of Lucene.

Here is the code I am running:

Path indexPath = Paths.get("index");
Directory dir = NIOFSDirectory.open(indexPath);

IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);

IndexWriter indexWriter = new IndexWriter(dir, iwc);
IndexReader indexReader = DirectoryReader.open(indexWriter, false);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
QueryParser queryParser = new QueryParser("defaultField", new StandardAnalyzer());
String query = "field:value";

indexSearcher.search(queryParser.parse(query), new SimpleCollector() {
    @Override
    public void collect(int doc) throws IOException {
        System.out.println("result " + doc);
    }

    @Override
    public boolean needsScores() {
        return false;
    }
});

I am expecting to print the doc ids to console, but I am not getting any results.

If use Luke to look at the index, I can see the results in the index that it should be returning via the "Documents" tab, but if I use the "Search" tab, again it returns nothing which leads me to think its something to do with my query?

What could be going wrong?

2

2 Answers

0
votes

It doesn't return doc ids, it return doc indexes.

You need to make a call to get the document at that index. Take a look at the JavaDoc for Collector for more details.

0
votes

I was indexing as field:Value and search via field:value...lucene is case sensitive.

I am converting everything to lowercase now.