Q:

Java Lucene: Use spans to get number of matches in a document

How can use spans object to get all matches in a document for a spanNearQuery, I got it upto here, but not sure how to proceed
     for(int i =0; i < splitwords.length ; i++)
                     {
                         sQuery[i] = new SpanTermQuery(new Term(field,splitwords[i]));
                     }
                     SpanQuery queryCount = new SpanNearQuery(sQuery, 0, true);
                     int numspans = 0;
                     Spans span = queryCount.getSpans(reader);
                     int docId;
                     while(span.next())
                     {
                         numspans++;
                         docId = span.doc();
                         System.out.println(span.end() - span.start());
                     }

Would I be able to get all matches(the count of matches) in current document?

A:

This will give you a hashtable containing the number of matches for each doc ID:
Hashtable<Integer, Integer> hits = new Hashtable<Integer, Integer>();
while (spans.next() == true)
{
     int docID = spans.doc();
     int hit = hits.get(docID) != null ? hits.get(docID) : 0;
     hit++;
     hits.put(docID, hit);
}

