Typically, writing a user deletion collector is the fastest way to count the number of hits using a bit set, as shown in the javadoc Collector .
Another method is to get TopDocs with the number of results indicated as one.
TopDocs topDocs = searcher.search(query, filter, 1);
topDocs.totalHits . , , , .
Java. Python.