开发者

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())
                开发者_如何学Python     {
                         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?


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜