Example usage for org.apache.lucene.search.highlight SimpleFragmenter SimpleFragmenter

List of usage examples for org.apache.lucene.search.highlight SimpleFragmenter SimpleFragmenter

Introduction

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

Prototype

public SimpleFragmenter(int fragmentSize) 

Source Link

Usage

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * // ww w  .  j a va2  s  . c  o  m
 * @param query 
 * @param list 
 * @param fields ??
 */
public List<T> keywordsHighlight(BooleanQuery query, List<T> list, String... fields) {
    Analyzer analyzer = new IKAnalyzer();
    Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>");
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(130));
    for (T entity : list) {
        try {
            for (String field : fields) {
                String text = StringUtils.replaceHtml((String) Reflections.invokeGetter(entity, field));
                String description = highlighter.getBestFragment(analyzer, field, text);
                if (description != null) {
                    Reflections.invokeSetter(entity, fields[0], description);
                    break;
                }
                Reflections.invokeSetter(entity, fields[0], StringUtils.abbr(text, 130));
            }
            //Reflections.invokeSetter(entity, fields[1], "sdfkjsdlkfjklsdjf");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
    }
    return list;
}

From source file:com.adanac.module.blog.search.LuceneHelper.java

License:Apache License

private static List<Map<String, String>> search(String searchText, String path, String title,
        LoadQuery loadQuery) {//  w ww  . j  av a2s.c  o m
    try {
        IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_PATH + path)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new SmartChineseAnalyzer();
        QueryParser parser = new QueryParser("indexedContent", analyzer);
        Query query = parser.parse(searchText);
        TopDocs resultDocs = searcher.search(query, 100);
        ScoreDoc[] scoreDocs = resultDocs.scoreDocs;
        //
        SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter("<span style=\"color:red;\">",
                "</span>");
        Highlighter highlighter = new Highlighter(simpleHtmlFormatter, new QueryScorer(query));
        highlighter.setTextFragmenter(new SimpleFragmenter(150));
        List<Map<String, String>> result = new ArrayList<>();
        List<Integer> idList = new ArrayList<>();
        for (int i = 0; i < scoreDocs.length; i++) {
            Document doc = searcher.doc(scoreDocs[i].doc);
            Integer id = Integer.valueOf(doc.get("id"));
            if (!idList.contains(id)) {
                String indexedContent = doc.get("indexedContent");
                TokenStream tokenStream = analyzer.tokenStream("indexedContent", indexedContent);
                Map<String, String> data = loadQuery.getById(id);
                String highlighterString = highlighter.getBestFragment(tokenStream, indexedContent);
                if (highlighterString.contains(SEPARATOR)) {
                    String[] array = highlighterString.split(SEPARATOR);
                    data.put(title, array[0]);
                    if (array.length > 1) {
                        data.put("summary", array[1]);
                    }
                } else {
                    data.put("summary", highlighterString);
                }
                result.add(data);
                idList.add(id);
            }
        }
        return result;
    } catch (Exception e) {
        logger.error("search failed ...", e);
    }
    return new ArrayList<>();
}

From source file:com.aistor.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * /*from   w  w  w. j  a va2  s  .co m*/
 * @param query 
 * @param list 
 * @param fields ??
 */
public List<T> keywordsHighlight(BooleanQuery query, List<T> list, String... fields) {
    Analyzer analyzer = new IKAnalyzer();
    Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>");
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(130));
    for (T entity : list) {
        try {
            for (String field : fields) {
                String text = StringUtils.replaceHtml((String) Reflections.invokeGetter(entity, field));
                String desciption = highlighter.getBestFragment(analyzer, field, text);
                if (desciption != null) {
                    Reflections.invokeSetter(entity, fields[0], desciption);
                    break;
                }
                Reflections.invokeSetter(entity, fields[0], StringUtils.abbr(text, 130));
            }
            //Reflections.invokeSetter(entity, fields[1], "sdfkjsdlkfjklsdjf");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
    }
    return list;
}

From source file:com.bewsia.script.safe.lucene.SEntity.java

License:Open Source License

public String highlight(Query query, String text, String field, int fragmentSize, int maxNumFragments,
        String separator) throws Exception {
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
    CachingTokenFilter tokenStream = new CachingTokenFilter(
            analyzer.tokenStream(field, new StringReader(text)));
    SimpleHTMLFormatter formatter = new SimpleHTMLFormatter();
    Scorer scorer = new org.apache.lucene.search.highlight.QueryScorer(query);
    Highlighter highlighter = new Highlighter(formatter, scorer);
    highlighter.setTextFragmenter(new SimpleFragmenter(fragmentSize));
    tokenStream.reset();//w w  w  .ja  va 2  s.  c  o m
    String rv = highlighter.getBestFragments(tokenStream, text, maxNumFragments, separator);
    return rv.length() == 0 ? text : rv;
}

From source file:com.bluedragon.search.search.QueryRun.java

License:Open Source License

private void addRow(IndexSearcher searcher, int docid, float score, int rank, int searchCount,
        int recordsSearched) throws CorruptIndexException, Exception {
    DocumentWrap document = new DocumentWrap(searcher.doc(docid));

    queryResultData.addRow(1);// w w  w. j a va 2  s .  c  o  m
    queryResultData.setCurrentRow(queryResultData.getSize());

    // Add in the standard columns that we know we have for every search
    queryResultData.setCell(1, new cfStringData(document.getId()));
    queryResultData.setCell(2, new cfStringData(document.getName()));
    queryResultData.setCell(3, new cfNumberData(score));
    queryResultData.setCell(4, new cfNumberData(searchCount));
    queryResultData.setCell(5, new cfNumberData(recordsSearched));
    queryResultData.setCell(6, new cfNumberData(rank + 1));

    String uC = queryAttributes.getUniqueColumn();

    // Now we do the custom ones
    List<IndexableField> fields = document.getDocument().getFields();
    Iterator<IndexableField> it = fields.iterator();
    while (it.hasNext()) {
        IndexableField fieldable = it.next();

        String fieldName = fieldable.name().toLowerCase();

        // Check for the unique
        if (uniqueSet != null && fieldName.equals(uC)) {
            if (uniqueSet.contains(fieldable.stringValue())) {
                queryResultData.deleteRow(queryResultData.getSize());
                return;
            } else
                uniqueSet.add(fieldable.stringValue());
        }

        // Check to see if we have this column
        if (fieldName.equals("contents") && !queryAttributes.getContentFlag())
            continue;

        if (!activeColumns.containsKey(fieldName)) {
            int newcolumn = queryResultData.addColumnData(fieldable.name().toUpperCase(),
                    cfArrayData.createArray(1), null);
            activeColumns.put(fieldName, newcolumn);
        }

        int column = activeColumns.get(fieldName);
        if (column <= 6)
            continue;

        queryResultData.setCell(column, new cfStringData(fieldable.stringValue()));
    }

    // Do the context stuff if enable
    if (queryAttributes.getContextPassages() > 0) {

        Scorer scorer = new QueryScorer(queryAttributes.getQuery());
        SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(queryAttributes.getContextHighlightStart(),
                queryAttributes.getContextHighlightEnd());
        Highlighter highlighter = new Highlighter(formatter, scorer);
        Fragmenter fragmenter = new SimpleFragmenter(queryAttributes.getContextBytes());
        highlighter.setTextFragmenter(fragmenter);

        String nextContext = "";
        String contents = document.getAttribute(DocumentWrap.CONTENTS);

        if (contents != null) {
            TokenStream tokenStream = AnalyzerFactory.get("simple").tokenStream(DocumentWrap.CONTENTS,
                    new StringReader(contents));
            String[] fragments = null;
            try {
                fragments = highlighter.getBestFragments(tokenStream, contents,
                        queryAttributes.getContextPassages());
                if (fragments.length == 1) {
                    nextContext = fragments[0] + "...";
                } else {
                    StringBuilder context = new StringBuilder();
                    for (int f = 0; f < fragments.length; f++) {
                        context.append("...");
                        context.append(fragments[f]);
                    }
                    context.append("...");
                    nextContext = context.toString();
                }
            } catch (Exception e) {
            }

            // Add in the context
            if (!activeColumns.containsKey("context")) {
                int newcolumn = queryResultData.addColumnData("CONTEXT", cfArrayData.createArray(1), null);
                activeColumns.put("context", newcolumn);
            }

            queryResultData.setCell(activeColumns.get("context"), new cfStringData(nextContext));
        }
    }
}

From source file:com.doculibre.constellio.lucene.BaseLuceneIndexHelper.java

License:Open Source License

public String highlight(String strToHighlight, String fieldName, Query luceneQuery) {
    String highlightedText;// w  ww  . j a  va  2 s  .c  o  m
    Analyzer analyzer = analyzerProvider.getAnalyzer(Locale.FRENCH);
    try {
        Directory directory = FSDirectory.open(indexDir);
        IndexReader indexReader = DirectoryReader.open(directory);
        Query rewrittenLuceneQuery = luceneQuery.rewrite(indexReader);
        QueryScorer luceneScorer = new QueryScorer(rewrittenLuceneQuery);
        SimpleHTMLFormatter luceneFormatter = new SimpleHTMLFormatter("<span class=\"hit\">", "</span>");
        Highlighter luceneHighlighter = new Highlighter(luceneFormatter, luceneScorer);

        Fragmenter luceneFragmenter;
        // Si la chaine  highlighter est sup  250 carac
        if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) {
            // Cration de best fragments de 100 carac chaque
            luceneFragmenter = new SimpleFragmenter(TAILLE_FRAGMENT);
        } else {
            // Toute la chaine est highlight
            luceneFragmenter = new SimpleFragmenter(Integer.MAX_VALUE);
        }
        luceneHighlighter.setTextFragmenter(luceneFragmenter);

        TokenStream luceneTokenStream = analyzer.tokenStream(fieldName, new StringReader(strToHighlight));
        String fragment = null;
        if (strToHighlight.length() > TAILLE_CHAINE_NON_FRAGMENTEE) {
            fragment = luceneHighlighter.getBestFragments(luceneTokenStream, strToHighlight, NB_BEST_FRAGMENT,
                    FRAGMENT_SEP);
        } else {
            fragment = luceneHighlighter.getBestFragment(luceneTokenStream, strToHighlight);
        }

        if (StringUtils.isBlank(fragment) && fieldName.equalsIgnoreCase("titre")) {
            fragment = strToHighlight;
        }
        indexReader.close();
        directory.close();

        highlightedText = fragment;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InvalidTokenOffsetsException e) {
        throw new RuntimeException(e);
    }
    return highlightedText;
}

From source file:com.duroty.application.bookmark.manager.BookmarkManager.java

License:Open Source License

/**
 * DOCUMENT ME!//w ww  . j a  v a2  s  . c  o m
 *
 * @param repositoryName DOCUMENT ME!
 * @param token DOCUMENT ME!
 * @param page DOCUMENT ME!
 * @param messagesByPage DOCUMENT ME!
 * @param order DOCUMENT ME!
 * @param orderType DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 *
 * @throws BookmarkException DOCUMENT ME!
 * @throws SearchException DOCUMENT ME!
 */
public SearchObj search(String repositoryName, String token, int page, int messagesByPage, int order,
        String orderType, boolean isNotebook) throws BookmarkException {
    String lucenePath = "";

    if (!defaultLucenePath.endsWith(File.separator)) {
        lucenePath = defaultLucenePath + File.separator + repositoryName + File.separator
                + Constants.BOOKMARK_LUCENE_BOOKMARK;
    } else {
        lucenePath = defaultLucenePath + repositoryName + File.separator + Constants.BOOKMARK_LUCENE_BOOKMARK;
    }

    Searcher searcher = null;
    SearchObj searchObj = new SearchObj();
    Highlighter highlighter = null;

    try {
        searcher = BookmarkIndexer.getSearcher(lucenePath);

        Query query = null;
        Hits hits = null;

        if (StringUtils.isBlank(token)) {
            if (isNotebook) {
                query = SimpleQueryParser.parse("notebook:true", new KeywordAnalyzer());
            } else {
                query = new MatchAllDocsQuery();
            }

            hits = searcher.search(query, new Sort(new SortField[] { SortField.FIELD_SCORE,
                    new SortField(Field_insert_date, SortField.STRING, true) }));
        } else {
            query = SimpleQueryParser.parse(token, analyzer);

            StringBuffer buffer = new StringBuffer();

            if (isNotebook) {
                buffer.append("(" + query.toString() + ") AND ");

                QueryParser parser = new QueryParser(Field_notebook, new KeywordAnalyzer());
                parser.setDefaultOperator(Operator.AND);

                Query aux = parser.parse(String.valueOf(true));

                buffer.append("(" + aux.toString() + ") ");
            }

            if (buffer.length() > 0) {
                QueryParser parser = new QueryParser("", new WhitespaceAnalyzer());
                query = parser.parse(buffer.toString());
            }

            hits = searcher.search(query);
        }

        Date searchStart = new Date();

        Date searchEnd = new Date();

        //time in seconds
        double time = ((double) (searchEnd.getTime() - searchStart.getTime())) / (double) 1000;

        int hitsLength = hits.length();

        if (hitsLength <= 0) {
            return null;
        }

        int start = page * messagesByPage;
        int end = start + messagesByPage;

        if (end > 0) {
            end = Math.min(hitsLength, end);
        } else {
            end = hitsLength;
        }

        if (start > end) {
            throw new SearchException("Search index of bound. start > end");
        }

        Vector bookmarks = new Vector();

        for (int j = start; j < end; j++) {
            Document doc = hits.doc(j);

            if (doc != null) {
                LuceneBookmark luceneBookmark = new LuceneBookmark(doc);

                SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<b>", "</b>");
                highlighter = new Highlighter(formatter, new QueryScorer(query));
                highlighter.setTextFragmenter(new SimpleFragmenter(150));

                BookmarkObj bookmarkObj = new BookmarkObj();
                bookmarkObj.setCacheDate(luceneBookmark.getCacheDate());
                bookmarkObj.setComments(luceneBookmark.getComments());
                bookmarkObj.setContents(luceneBookmark.getCotents());
                bookmarkObj.setDepth(luceneBookmark.getDepth());
                bookmarkObj.setFlagged(luceneBookmark.isFlagged());
                bookmarkObj.setIdint(luceneBookmark.getIdint());
                bookmarkObj.setInsertDate(luceneBookmark.getInsertDate());
                bookmarkObj.setKeywords(luceneBookmark.getKeywords());
                bookmarkObj.setNotebook(luceneBookmark.isNotebook());
                bookmarkObj.setParent(Long.parseLong(luceneBookmark.getParent()));
                bookmarkObj.setTitle(luceneBookmark.getTitle());
                bookmarkObj.setTitleHighlight(luceneBookmark.getTitle());
                bookmarkObj.setUrl(luceneBookmark.getUrl());

                String contents = luceneBookmark.getCotents();
                String hcontents = null;

                if ((contents != null) && (!contents.trim().equals(""))) {
                    contents = contents.replaceAll("\\s+", " ");

                    TokenStream tokenStream = analyzer.tokenStream(Field_contents, new StringReader(contents));
                    hcontents = highlighter.getBestFragment(tokenStream, contents);

                    if (hcontents != null) {
                        contents = hcontents;
                    } else {
                        contents = null;
                    }
                }

                bookmarkObj.setContentsHighlight(contents);

                String title = luceneBookmark.getTitle();
                String htitle = null;

                if ((title != null) && (!title.trim().equals(""))) {
                    title = title.replaceAll("\\s+", " ");

                    TokenStream tokenStream = analyzer.tokenStream(Field_title, new StringReader(title));
                    htitle = highlighter.getBestFragment(tokenStream, title);

                    if (htitle != null) {
                        title = htitle;
                    }
                }

                bookmarkObj.setTitleHighlight(title);

                bookmarks.addElement(bookmarkObj);
            }
        }

        searchObj.setHits(hitsLength);
        searchObj.setTime(time);
        searchObj.setBookmarks(bookmarks);
    } catch (Exception ex) {
        throw new SearchException(ex);
    } finally {
    }

    return searchObj;
}

From source file:com.edgenius.wiki.search.service.AbstractSearchService.java

License:Open Source License

private int detach(IndexSearcher searcher, List<SearchResultItem> viewableMatchedResults, TopDocs hits,
        Query hlQuery, int from, int to, User user) throws IOException {

    Assert.isTrue(from <= to && from >= 0 && (to >= 0 || to == -1));

    //For performance issue, we simply return total result set length without permission filter out.
    //This means is total length might be larger than the set that user can view, as some result will be filter out
    //if user doesn't have permission to see.
    int len = hits.totalHits;

    if (len > 0 && from < len) {
        to = to == -1 ? len : (len > to ? to : len);
        //security filter from return result

        List<Integer> resultIdx = new ArrayList<Integer>();
        for (int idx = from; idx < to; idx++) {
            //does not include "to" , For example, from:to is 0:10, then return index is 0-9

            //TODO: if page includes some result that invisible to user, it is better display message to tell user
            //some result is hidden for security reason.
            if (!isAllowView(searcher.doc(hits.scoreDocs[idx].doc), user))
                continue;

            resultIdx.add(idx);//ww  w .  j  a v a  2s.  c  o m
        }

        //create a highlighter for all fragment parser
        Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlighter\">", "</span>");
        Highlighter hl = null;
        if (hlQuery != null) {
            Scorer scorer = new QueryScorer(hlQuery);
            hl = new Highlighter(formatter, scorer);
            Fragmenter fragmenter = new SimpleFragmenter(FRAGMENT_LEN);
            hl.setTextFragmenter(fragmenter);
        }

        for (int idx : resultIdx) {
            SearchResultItem item = new SearchResultItem();

            Document doc = searcher.doc(hits.scoreDocs[idx].doc);
            String docType = doc.get(FieldName.DOC_TYPE);

            //common items in search results
            item.setType(NumberUtils.toInt(docType));
            item.setDatetime(doc.get(FieldName.UPDATE_DATE));
            if (userReadingService != null
                    && !new Integer(SharedConstants.SEARCH_USER).toString().equals(docType)) {
                String username = doc.get(FieldName.CONTRIBUTOR);
                User contirUser = userReadingService.getUserByName(username);
                if (contirUser != null) {
                    item.setContributor(contirUser.getFullname());
                    item.setContributorUsername(username);
                }
            }
            if (Integer.valueOf(SharedConstants.SEARCH_PAGE).toString().equals(docType)) {
                String content = doc.get(FieldName.PAGE_CONTENT);
                item.setTitle(doc.get(FieldName.PAGE_TITLE));
                item.setSpaceUname(doc.get(FieldName.UNSEARCH_SPACE_UNIXNAME));

                //does set item.desc() as content, which maybe very big string. no necessary returned
                item.setFragment(createFragment(hl, StringUtil.join(" ", item.getTitle(), content)));

            } else if (Integer.valueOf(SharedConstants.SEARCH_COMMENT).toString().equals(docType)) {
                String content = doc.get(FieldName.CONTENT);

                item.setItemUid(doc.get(FieldName.COMMENT_UID));
                item.setSpaceUname(doc.get(FieldName.UNSEARCH_SPACE_UNIXNAME));
                item.setTitle(doc.get(FieldName.UNSEARCH_PAGE_TITLE));
                //does set item.desc() as content, which maybe very big string. no necessary returned
                item.setFragment(createFragment(hl, content));

            } else if (Integer.valueOf(SharedConstants.SEARCH_SPACE).toString().equals(docType)) {
                String title = doc.get(FieldName.SPACE_NAME);
                item.setTitle(title);
                item.setSpaceUname(doc.get(FieldName.SPACE_UNIXNAME));
                item.setDesc(doc.get(FieldName.SPACE_DESC));
                item.setFragment(createFragment(hl, StringUtil.join(" ", item.getTitle(), item.getDesc())));

            } else if (Integer.valueOf(SharedConstants.SEARCH_WIDGET).toString().equals(docType)) {
                //wTitle-> title; wDesc-> desc; wTitle(could be pageTitle or markup title) ->spaceUname
                String widgetType = doc.get(FieldName.WIDGET_TYPE);
                String title = doc.get(FieldName.WIDGET_TITLE);

                //does content need transfer back?? so far no
                String content = doc.get(FieldName.WIDGET_CONTENT);
                if (WidgetModel.TYPE_PAGE_LINKER.equals(widgetType)) {
                    //don't use as Highlighter fragment
                    content = "";
                }

                String desc = doc.get(FieldName.WIDGET_DESC);

                item.setDesc(desc);
                item.setTitle(title);

                //add little confuse field mapping :(
                item.setSpaceUname(doc.get(FieldName.WIDGET_KEY));
                item.setItemUid(widgetType);

                item.setFragment(createFragment(hl, StringUtil.join(" ", item.getDesc(), content)));

            } else if (Integer.valueOf(SharedConstants.SEARCH_PAGE_TAG).toString().equals(docType)) {
                //page tag
                item.setTitle(doc.get(FieldName.PAGE_TAG_NAME));
                item.setSpaceUname(doc.get(FieldName.UNSEARCH_SPACE_UNIXNAME));
                item.setFragment(createFragment(hl, item.getTitle()));
            } else if (Integer.valueOf(SharedConstants.SEARCH_SPACE_TAG).toString().equals(docType)) {
                //space tag
                item.setTitle(doc.get(FieldName.SPACE_TAG_NAME));
                item.setFragment(createFragment(hl, item.getTitle()));

            } else if (Integer.valueOf(SharedConstants.SEARCH_USER).toString().equals(docType)) {
                String username = doc.get(FieldName.USER_NAME);
                item.setTitle(username);
                String fullname = doc.get(FieldName.USER_FULLNAME);
                //hacker - contributor is current user fullname
                item.setContributor(fullname);
                if (userReadingService != null)
                    item.setDesc(userReadingService.getUserByName(username).getSetting().getStatus());
                item.setFragment(createFragment(hl, fullname));

            } else if (Integer.valueOf(SharedConstants.SEARCH_ROLE).toString().equals(docType)) {
                item.setSpaceUname(doc.get(FieldName.ROLE_NAME));
                item.setTitle(doc.get(FieldName.ROLE_DISPLAY_NAME));
                item.setDesc(doc.get(FieldName.ROLE_DESC));
                //item.setFragment("");

            } else if (Integer.valueOf(SharedConstants.SEARCH_ATTACHMENT).toString().equals(docType)) {
                item.setTitle(doc.get(FieldName.FILE_NAME));
                item.setDesc(doc.get(FieldName.FILE_COMMENT));
                item.setItemUid(doc.get(FieldName.FILE_NODE_UUID));
                item.setSpaceUname(doc.get(FieldName.UNSEARCH_SPACE_UNIXNAME));
                String text = doc.get(FieldName.TEXT);
                //does not mark file content fragment, because it does not store in index
                String fragment = createFragment(hl, StringUtil.join(" ", item.getDesc(), text));
                item.setFragment(
                        (fragment == null || fragment.trim().length() == 0) ? ("Comment: " + item.getDesc())
                                : fragment);
            }
            viewableMatchedResults.add(item);
        }
    }
    return len;
}

From source file:com.green.common.persistence.BaseDao.java

License:Open Source License

/**
 * /*from  ww w  . j av a2 s  . c  o  m*/
 * @param query 
 * @param list 
 * @param subLength ?
 * @param fields ??
 */
public List<T> keywordsHighlight(BooleanQuery query, List<T> list, int subLength, String... fields) {
    Analyzer analyzer = new IKAnalyzer();
    Formatter formatter = new SimpleHTMLFormatter("<span class=\"highlight\">", "</span>");
    Highlighter highlighter = new Highlighter(formatter, new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(subLength));
    for (T entity : list) {
        try {
            for (String field : fields) {
                String text = StringUtils.replaceHtml((String) Reflections.invokeGetter(entity, field));
                String description = highlighter.getBestFragment(analyzer, field, text);
                if (description != null) {
                    Reflections.invokeSetter(entity, fields[0], description);
                    break;
                }
                Reflections.invokeSetter(entity, fields[0], StringUtils.abbr(text, subLength * 2));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidTokenOffsetsException e) {
            e.printStackTrace();
        }
    }
    return list;
}

From source file:com.knowledgetree.indexer.IndexerManager.java

/**
 * Returns a set of hits from lucene.//from   w  w w.jav a2s  .c  om
 * @param queryString
 * @param maxHits
 * @return
 * @throws Exception
 */
public QueryHit[] query(String queryString, int maxHits, boolean getText) throws Exception {
    synchronized (this) {
        this.queryCount++;
    }

    String tmp = queryString.toLowerCase();
    boolean queryContent = tmp.indexOf("content") != -1;
    boolean queryDiscussion = tmp.indexOf("discussion") != -1;

    QueryParser parser = new QueryParser("Content", this.analyzer);
    Query query = parser.parse(queryString);

    // rewriting is important for complex queries. this is a must-do according to sources!
    query = query.rewrite(this.queryReader);

    // run the search!
    Hits hits = this.querySearcher.search(query);

    // now we can apply the maximum hits to the results we return!
    int max = (maxHits == -1) ? hits.length() : maxHits;

    if (hits.length() < max) {
        max = hits.length();
    }

    QueryHit[] results = new QueryHit[max];

    Highlighter highlighter = new Highlighter(this, new QueryScorer(query));
    highlighter.setTextFragmenter(new SimpleFragmenter(this.resultFragmentSize));
    for (int i = 0; i < max; i++) {
        Document doc = hits.doc(i);

        QueryHit hit = new QueryHit();
        hit.DocumentID = IndexerManager.stringToLong(doc.get("DocumentID"));
        hit.Rank = hits.score(i);
        hit.Title = doc.get("Title");
        if (getText) {
            String text = "";
            if (queryContent) {
                text += doc.get("Content");
            }
            if (queryDiscussion) {
                text += doc.get("Discussion");
            }

            // TODO: we can create a field.getReader(). the fragmenting needs to
            // be updated to deal with the reader only. would prefer not having to
            // load the document into a string!
            TokenStream tokenStream = analyzer.tokenStream("contents", new StringReader(text));

            hit.Content = highlighter.getBestFragments(tokenStream, text, this.resultFragments,
                    this.resultSeperator);
        } else {
            hit.Content = "";
        }

        hit.Version = doc.get("Version");

        results[i] = hit;
    }

    return results;
}