How to get the matching spans of a Span Term Query in Lucene 5?

前端 未结 1 1424
慢半拍i
慢半拍i 2021-02-10 03:18

In Lucene to get the words around a term it is advised to use Span Queries. There is good walkthrough in http://lucidworks.com/blog/accessing-words-around-a-positional-match-in-

相关标签:
1条回答
  • 2021-02-10 03:54

    The way to do it would be as follows.

    LeafReader pseudoAtomicReader = SlowCompositeReaderWrapper.wrap(reader);
    Term term = new Term("field", "fox");
    SpanTermQuery spanTermQuery = new SpanTermQuery(term);
    SpanWeight spanWeight = spanTermQuery.createWeight(is, false);
    Spans spans = spanWeight.getSpans(pseudoAtomicReader.getContext(), Postings.POSITIONS);
    

    The support for iterating over the spans via span.next() is also gone in version 5.3 of Lucene. To iterate over the spans you can do

    int nxtDoc = 0;
    while((nxtDoc = spans.nextDoc()) != spans.NO_MORE_DOCS){
      System.out.println(spans.toString());
      int id = nxtDoc;
      System.out.println("doc_id="+id);
      Document doc = reader.document(id);
      System.out.println(doc.getField("field"));
      System.out.println(spans.nextStartPosition());
      System.out.println(spans.endPosition());
    }
    
    0 讨论(0)
提交回复
热议问题