Example usage for org.apache.lucene.search.highlight Highlighter getBestFragments

List of usage examples for org.apache.lucene.search.highlight Highlighter getBestFragments

Introduction

In this page you can find the example usage for org.apache.lucene.search.highlight Highlighter getBestFragments.

Prototype

public final String getBestFragments(TokenStream tokenStream, String text, int maxNumFragments,
        String separator) throws IOException, InvalidTokenOffsetsException 

Source Link

Document

Highlights terms in the text , extracting the most relevant sections and concatenating the chosen fragments with a separator (typically "...").

Usage

From source file:org.sakaiproject.search.component.service.impl.SearchResultResponseImpl.java

License:Educational Community License

public String getSearchResult() {
    try {//  w  ww. j  a  v  a 2  s .  co m
        Scorer scorer = new QueryScorer(query);
        Highlighter hightlighter = new Highlighter(new SimpleHTMLFormatter(), new SimpleHTMLEncoder(), scorer);
        StringBuilder sb = new StringBuilder();
        // contents no longer contains the digested contents, so we need to
        // fetch it from the EntityContentProducer

        EntityContentProducer sep = searchIndexBuilder.newEntityContentProducer(getReference());
        if (sep != null) {
            sb.append(sep.getContent(getReference()));
        }
        String text = sb.toString();
        TokenStream tokenStream = analyzer.tokenStream(SearchService.FIELD_CONTENTS, new StringReader(text));
        return hightlighter.getBestFragments(tokenStream, text, 5, " ... "); //$NON-NLS-1$
    } catch (IOException e) {
        return Messages.getString("SearchResultResponseImpl.11") + e.getMessage(); //$NON-NLS-1$
    } catch (InvalidTokenOffsetsException e) {
        return Messages.getString("SearchResultResponseImpl.11") + e.getMessage();
    }
}

From source file:org.shredzone.cilla.service.search.strategy.LuceneSearchStrategy.java

License:Open Source License

/**
 * Highlight the contents by search result.
 *
 * @param content//  ww  w.  java  2  s.co  m
 *            Plain text content to highlight
 * @param hilighter
 *            {@link Highlighter} to use
 * @return Highlighted content
 */
private String highlight(String content, Highlighter hilighter) {
    try (SimpleAnalyzer analyzer = new SimpleAnalyzer(Version.LUCENE_36)) {
        TokenStream tokenStream = analyzer.tokenStream("text", new StringReader(content));

        StringBuilder sb = new StringBuilder();
        sb.append(searchResultRenderer.getHeader());
        sb.append(hilighter.getBestFragments(tokenStream, content, searchResultRenderer.getMaxResults(),
                searchResultRenderer.getSeparator()));
        sb.append(searchResultRenderer.getFooter());
        return sb.toString();
    } catch (Exception ex) {
        return content;
    }
}

From source file:searchEngine.SearchFiles.java

License:Apache License

public static QueryResult doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query,
        int hitsPerPage, int pageNumber, boolean raw, boolean interactive)
        throws IOException, InvalidTokenOffsetsException {

    QueryResult queryResults = new QueryResult();
    TopDocs results = searcher.search(query, pageNumber * hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<font color=\"red\">", "</font>"),
            new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(20));

    int start = 0;
    int end = Math.min(numTotalHits, pageNumber * hitsPerPage);
    for (int i = start; i < end; i++) {
        if (raw) {
            System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score);
            continue;
        }//from   www . j a  v a  2s .  c o  m

        Document doc = searcher.doc(hits[i].doc);
        Analyzer analyzer = new SmartChineseAnalyzer();
        String text = doc.get("contents");
        TokenStream tokenStream = analyzer.tokenStream("contents", new StringReader(text));
        String highlighterResult = highlighter.getBestFragments(tokenStream, text, 2, "");
        System.out.println("########### " + highlighterResult);

        String path = doc.get("path");
        if (path != null) {
            System.out.println((i + 1) + ". " + path + " Score: " + hits[i].score);
            //System.out.println(results[i].)
            queryResults.addUrl(path);
            String title = doc.get("title");
            queryResults.addTitle(title);
            queryResults.addContent(highlighterResult);
        } else {
            System.out.println((i + 1) + ". " + "No path for this document");
        }
    }
    return queryResults;
}