简单的lucene示例不起作用

时间:2015-04-19 09:30:06

标签: java lucene

我正在乱搞Lucene看看它是如何帮助我的,我无法得到一个非常简单的例子。我正在使用Lucene 5.1

期望是当我搜索时,我获得了我在控制台中添加到索引的文档的文档ID。我什么也没得到,没有错误(只是"完成"最后打印到控制台)

这是我的代码:

public static void main(String[] args) throws Exception {
    // create structure on file system
    IndexWriter writer = createOrGetIndexWriter(LocalDate.now());
    writer.close();

    // open for writing
    writer = createOrGetIndexWriter(LocalDate.now());

    Document document = new Document();
    document.add(new IntField("test_field", 1, Field.Store.YES));

    // write document and close.
    writer.addDocument(document);
    writer.commit();
    writer.close();

    // open reader
    IndexReader reader = getIndexReader(LocalDate.now());
    IndexSearcher indexSearcher = new IndexSearcher(reader);
    Query q = new TermQuery(new Term("test_field", "1"));

    // callback should be synchronous
    indexSearcher.search(q, new SimpleCollector() {
        @Override
        public void collect(int i) throws IOException {
            System.out.println(i);
        }

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

    System.out.println("Done");
}

public static IndexWriter createOrGetIndexWriter(LocalDate date) throws Exception {
    Directory directory = FSDirectory.open(Paths.get(date.toString()));
    IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
    iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
    return new IndexWriter(directory, iwc);
}

public static IndexReader getIndexReader(LocalDate date) throws Exception {
    return DirectoryReader.open(FSDirectory.open(Paths.get(date.toString())));
}

0 个答案:

没有答案