问题
Using Java how would you find out the number of documents in an lucene index?
回答1:
IndexReader contains the methods you need, in particular, numDocs
http://lucene.apache.org/core/3_6_0/api/all/org/apache/lucene/index/IndexReader.html#numDocs()
回答2:
The official documentation: http://lucene.apache.org/java/2_4_0/api/org/apache/lucene/index/IndexReader.html#numDocs()
回答3:
Using java you can find the number of documents like this :
IndexReader reader = IndexReader.open(FSDirectory.open(indexDirectory));
System.out.println(reader.maxDoc()); //this will give ya what you need.
回答4:
When using Hibernate Search, it is possible to obtain a Lucene IndexReader instance through the Hibernate Search API and then use reader.numDocs() as already mentioned in previous answers.
FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(getEntityManager());
IndexReader reader = fullTextEntityManager.getSearchFactory().getIndexReaderAccessor().open(MyEntity1.class, MyEntity2.class ...);
int numDocs = reader.numDocs();
来源:https://stackoverflow.com/questions/442463/finding-the-number-of-documents-in-a-lucene-index