Search code examples
lucenehighlightingfragment

lucene highlighter: how can I get locations of fragments?


I know how to get relevant highlighted fragments together with some surrounding text using Lucene highlighter, namely, using

Highlighter highlighter = new Highlighter(scorer);
String[] fragments = highlighter.getBestFragments(stream, fieldContents, fragmentNumber);

But can I instead get pointers to these fragments in the original contents? In other words, I need to know where these fragments start and, if possible, end.


Solution

  • If you use the GetBestTextFragments method instead, you will get back an array of TextFragments. These have properties textStartPos and textEndPos.

    (They are marked internal in Lucene.NET, which will require you to make some code changes to get access to them. I'm not sure about Java Lucene.)