fasseg
fasseg

Reputation: 17761

Apache Lucene: How to get the first matching substring from a Document

I could not find any info on the web and stackoverflow on how to get the first matching character subsequence from a Lucene Document.

ATM i'm using this logic to retrieve results from Lucene:

        Document doc=searcher.doc(hit.doc);
        String text=doc.get("text");
        if (text.length() > 80){
            text=text.substring(0,80);
        }
        results.add(new SearchResult(doc.get("url"), doc.get("title"), text));

As you can see this just takes the first 80 chars of the searched text and wraps it together with some other data into a SearchResult object.

Is it somehow possible to retrieve the first or even highest scoring subsequence of the text which actually contains any searchterms?

Upvotes: 0

Views: 1026

Answers (2)

Eugene Kuleshov
Eugene Kuleshov

Reputation: 31795

It is called hit highlighter. This is probably a duplicate of another highlighter question

Upvotes: 1

ffriend
ffriend

Reputation: 28552

You need Lucene Highlighter. Here and here you can find some more info on it.

Upvotes: 2

Related Questions