2

I would like to find the start and end positions of a match from a lucene (Version 3.0.2 for Java) query. It seems like I should be able to get this info from Highlighter or FastVectorHighligher, but these classes seem only return a text fragment with the relevant text highlighted. Is there any way to get this info, either with a Highlighter or from the ScoreDoc itself?

Update: I found this related question: Finding the position of search hits from Lucene

But I think the answer by Allasso won't work for me because my queries are phrases, not individual terms.

Community
  • 1
  • 1
Mike T
  • 1,163
  • 1
  • 11
  • 27

1 Answers1

5

If I were you I'd just take code from FastVectorHighlighter. Relevant code is in FieldTermStack:

        List<string> termSet = fieldQuery.getTermSet(fieldName);
        VectorHighlightMapper tfv = new VectorHighlightMapper(termSet);    
        reader.GetTermFreqVector(docId, fieldName, tfv);  // <-- look at this line

        string[] terms = tfv.GetTerms();
        foreach (String term in terms)
        {
            if (!termSet.Contains(term)) continue;
            int index = tfv.IndexOf(term);
            TermVectorOffsetInfo[] tvois = tfv.GetOffsets(index);
            if (tvois == null) return; // just return to make null snippets
            int[] poss = tfv.GetTermPositions(index);
            if (poss == null) return; // just return to make null snippets
            for (int i = 0; i < tvois.Length; i++)
                termList.AddLast(new TermInfo(term, tvois[i].GetStartOffset(), tvois[i].GetEndOffset(), poss[i]));

The major thing there is reader.GetTermFreqVector(). Like I said, FastVectorHighlighter already does some legwork that I would just copy, but if you want, that GetTermPositions call should do everything you need.

Xodarap
  • 11,581
  • 11
  • 56
  • 94
  • I should have specified that I'm using Lucene Java 3.0.2. Still, I will look at the code for FastVectorHighlighter is see if I can get what I need from there. – Mike T Jul 27 '10 at 06:25
  • @Mike: Sorry, I figured c# syntax was close enough to java. In any case, the TermPositionsVector should do what you want. Since you want to highlight phrases it will be a bit tougher (you'll need to find ones which are right next to each other) but it shouldn't be too bad. – Xodarap Jul 27 '10 at 14:23