tags:

views:

88

answers:

1

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: http://stackoverflow.com/questions/1311199/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.

A: 

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
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
@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