I'm working on a project for which I want to build a tag cloud by reading a Lucene index and pruning it down. I didn't set up the L开发者_如何学运维ucene engine, it was someone else in the team, now I just want to read its index. Do you how to do that in Java?
Not sure what you mean by "reading" an Index:
If you want to query it you can use IndexSearcher class.
IndexReader allows you to open the index in read mode.
If you want to view the contents of the index, you can use Luke
You do it like this -
IndexReader r = IndexReader.open( "prdb_index");
int num = r.numDocs();
for ( int i = 0; i < num; i++)
{
if ( ! r.isDeleted( i))
{
Document d = r.document( i);
System.out.println( "d=" +d);
}
}
r.close();
what you need to look for is how to use IndexReader class, the .terms() method will give you back all the terms in the index.
Just do this:
File indexDirectory = new File("YourIndexLocation");
IndexReader reader = IndexReader.open(FSDirectory.open(indexDirectory));
return reader.maxDoc(); //return total docs in index
精彩评论