Path localIndexPath = new Path("/localindex");
fileSystem.mkdirs(localIndexPath);
fileSystem.copyFromLocalFile(new Path(localDir.getAbsolutePath() + "/"), localIndexPath);
Path hdfsIndexPath = fileSystem.listStatus(localIndexPath)[0].getPath();
HdfsDirectory hdfsDirectory = new HdfsDirectory(conf, hdfsIndexPath);
Directory directory = new BlockCacheDirectoryFactoryV2(new Configuration(), 1000000).newDirectory(
"index", "shard1", hdfsDirectory, null
);
try (DirectoryReader reader = DirectoryReader.open(directory)) {
IndexSearcher indexSearcher = new IndexSearcher(reader);
long start = System.currentTimeMillis();
Query query = new QueryParser(Version.LUCENE_43, "name", analyzer).parse("r");
TopDocs search = indexSearcher.search(query, 1000);
ScoreDoc[] scoreDocs = search.scoreDocs;
System.out.println("Found " + scoreDocs.length + " num of documents from search in " +
(System.currentTimeMillis() - start) +
" ms. Total[" + search.totalHits + "]");
start = System.currentTimeMillis();
for (ScoreDoc scoreDoc : scoreDocs) {
Document doc = indexSearcher.doc(scoreDoc.doc);
assert doc != null;
}
System.out.println("Took [" + (System.currentTimeMillis() - start) + "] ms to retrieve all docs");
}
hdfsDirectory.close();
directory.close();
} finally {
localDirectory.close();
FileDeleteStrategy.FORCE.delete(localDir);
}