Example usage for org.apache.solr.client.solrj SolrQuery setHighlightFragsize

List of usage examples for org.apache.solr.client.solrj SolrQuery setHighlightFragsize

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setHighlightFragsize.

Prototype

public SolrQuery setHighlightFragsize(int num) 

Source Link

Usage

From source file:org.gbif.common.search.builder.SolrQueryBuilder.java

License:Apache License

/**
 * Helper method that sets the highlighting parameters.
 * //from ww  w  .j av a2  s.c  o m
 * @param searchRequest the searchRequest used to extract the parameters
 * @param solrQuery this object is modified by adding the facets parameters
 */
private void setHighLightParams(SearchRequest<P> searchRequest, SolrQuery solrQuery) {
    solrQuery.setHighlight(searchRequest.isHighlight());
    solrQuery.setHighlightSnippets(NUM_HL_SNIPPETS);
    solrQuery.setHighlightFragsize(HL_FRAGMENT_SIZE);
    if (searchRequest.isHighlight()) {
        for (String hlField : queryBuilder.getHighlightedFields()) {
            solrQuery.addHighlightField(hlField);
        }
    }
}

From source file:org.rsc.liferay.solr.SolrIndexSearcher.java

License:Open Source License

protected SolrQuery translateQuery(long companyId, Query query, Sort[] sorts, int start, int end)
        throws Exception {

    QueryConfig queryConfig = query.getQueryConfig();

    SolrQuery solrQuery = new SolrQuery();

    if (queryConfig.isHighlightEnabled()) {
        solrQuery.setHighlight(true);/*from   w ww . j  a  v  a2  s .c o m*/
        solrQuery.setHighlightFragsize(queryConfig.getHighlightFragmentSize());
        solrQuery.setHighlightSnippets(queryConfig.getHighlightSnippetSize());

        String localizedContentName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.CONTENT);

        String localizedTitleName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.TITLE);

        solrQuery.setParam("hl.fl", Field.CONTENT, localizedContentName, Field.TITLE, localizedTitleName);
    }

    solrQuery.setIncludeScore(queryConfig.isScoreEnabled());

    QueryTranslatorUtil.translateForSolr(query);

    String queryString = query.toString();

    StringBundler sb = new StringBundler(6);

    sb.append(queryString);
    sb.append(StringPool.SPACE);
    sb.append(StringPool.PLUS);
    sb.append(Field.COMPANY_ID);
    sb.append(StringPool.COLON);
    sb.append(companyId);

    solrQuery.setQuery(sb.toString());

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
        solrQuery.setRows(0);
    } else {
        solrQuery.setRows(end - start);
        solrQuery.setStart(start);
    }

    if (sorts != null) {
        for (Sort sort : sorts) {
            if (sort == null) {
                continue;
            }

            String sortFieldName = sort.getFieldName();

            if (DocumentImpl.isSortableTextField(sortFieldName)) {
                sortFieldName = DocumentImpl.getSortableFieldName(sortFieldName);
            }

            ORDER order = ORDER.asc;

            if (Validator.isNull(sortFieldName) || !sortFieldName.endsWith("sortable")) {

                sortFieldName = "score";

                order = ORDER.desc;
            }

            if (sort.isReverse()) {
                order = ORDER.desc;
            }

            solrQuery.addSort(new SortClause(sortFieldName, order));
        }
    }

    return solrQuery;
}

From source file:org.segrada.search.solr.SolrSearchEngine.java

License:Apache License

@Override
public PaginationInfo<SearchHit> search(String searchTerm, Map<String, String> filters) {
    // to avoid NPEs
    if (filters == null)
        filters = new HashMap<>();

    // set defaults
    int page = 1;
    int entriesPerPage = 20;

    try {/*w  w  w . j  av a 2s  .  c  o m*/
        // Parse a simple query that searches for "text":
        MultiFieldQueryParser parser;
        String[] containFields;
        // do we have a filter to contain to certain fields?
        if (filters.containsKey("fields")) {
            String fields = filters.get("fields");
            if (fields.isEmpty())
                containFields = new String[] { this.title, this.subTitles, this.content };
            else if (fields.equalsIgnoreCase(this.title))
                containFields = new String[] { this.title };
            else if (fields.equalsIgnoreCase(this.subTitles))
                containFields = new String[] { this.subTitles };
            else if (fields.equalsIgnoreCase(this.content))
                containFields = new String[] { this.content };
            else if (fields.equalsIgnoreCase("allTitles"))
                containFields = new String[] { this.title, this.subTitles };
            else
                throw new RuntimeException("fields-Filter " + fields + " is not known.");
        } else
            containFields = new String[] { this.title, this.subTitles, this.content };
        parser = new MultiFieldQueryParser(Version.LUCENE_47, containFields, analyzer);

        // which operator do we use?
        parser.setDefaultOperator(QueryParser.Operator.AND);
        if (filters.containsKey("operator")) {
            String operator = filters.get("operator");
            if (operator.equalsIgnoreCase("or"))
                parser.setDefaultOperator(QueryParser.Operator.OR);
            else if (!operator.isEmpty() && !operator.equalsIgnoreCase("and"))
                throw new RuntimeException("operator-Filter " + operator + " is not and/or.");
        }

        // filters for query
        SolrQuery query = new SolrQuery();
        // class filter
        if (filters.containsKey("class") && !filters.get("class").isEmpty()) {
            // multiple classes?
            String[] classes = filters.get("class").split(",");

            // single class
            if (classes.length <= 1) {
                query.addFilterQuery(this.className, filters.get("class"));
            } else { // multiple classes
                StringBuilder chained = new StringBuilder("(");
                for (int i = 0; i < classes.length; i++) {
                    if (i > 0)
                        chained.append(" OR ");
                    chained.append("className:").append(classes[i].trim());
                }
                query.addFilterQuery(this.className, chained + ")");
            }
        }

        // tag filter
        if (filters.containsKey("tags") && !filters.get("tags").isEmpty()) {
            // split tags into array
            String[] tags = filters.get("tags").split(",");
            BooleanQuery booleanQuery = new BooleanQuery();
            for (String tagLocal : tags) {
                booleanQuery.add(new TermQuery(new Term("tag", tagLocal.trim())), BooleanClause.Occur.SHOULD);
            }
            query.addFilterQuery(this.tag, booleanQuery.toString());
        }

        // define query
        Query queryTerm = null;
        if (searchTerm != null)
            queryTerm = parser.parse(searchTerm);
        if (queryTerm == null)
            queryTerm = new MatchAllDocsQuery(); // fallback to match all documents
        query.setQuery(queryTerm.toString());

        // get hits per page
        if (filters.containsKey("limit")) {
            try {
                entriesPerPage = Integer.valueOf(filters.get("limit"));
                if (entriesPerPage <= 0 || entriesPerPage > 1000)
                    entriesPerPage = 20;
            } catch (NumberFormatException e) {
                logger.warn("Could not parse limit " + filters.get("limit") + " to integer", e);
            }
        }

        // get page number
        if (filters.containsKey("page")) {
            try {
                page = Integer.valueOf(filters.get("page"));
            } catch (NumberFormatException e) {
                logger.warn("Could not parse page " + filters.get("page") + " to integer", e);
            }
        }

        // calculate start index
        int startIndex = (page - 1) * entriesPerPage;

        query.setStart(startIndex);
        query.setRows(entriesPerPage);

        query.setFields("*", "score");

        // define highlighting
        query.setHighlight(true);
        query.addHighlightField(this.content);
        query.setHighlightFragsize(18);
        query.setHighlightSnippets(10);
        query.setHighlightSimplePre("<b>");
        query.setHighlightSimplePost("</b>");

        // do query
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        // how many pages do we have?
        int pages = (int) (results.getNumFound() / entriesPerPage + 1);

        // cycle trough hits
        List<SearchHit> hits = new ArrayList<>();

        for (SolrDocument doc : results) {
            SearchHit searchHit = createHitFromDocument(doc);

            // add score
            Object score = doc.get("score");
            if (score != null && score instanceof Float)
                searchHit.setRelevance((float) score);

            // get highlighted components
            if (searchTerm != null && response.getHighlighting().get(searchHit.getId()) != null) {
                List<String> fragments = response.getHighlighting().get(searchHit.getId()).get(this.content);
                if (fragments != null) {
                    String[] bestFragments = new String[fragments.size() > 10 ? 10 : fragments.size()];
                    for (int i = 0; i < bestFragments.length; i++)
                        bestFragments[i] = fragments.get(i);
                    searchHit.setHighlightText(bestFragments);
                }
            }

            // add hit
            hits.add(searchHit);
        }

        // return pagination info
        return new PaginationInfo<>(page, pages, (int) results.getNumFound(), entriesPerPage, hits);
    } catch (Throwable e) {
        logger.error("Error in search.", e);
    }

    // return empty list result in order to avoid NPEs
    return new PaginationInfo<>(page, 1, 0, entriesPerPage, new ArrayList<>());
}

From source file:org.segrada.search.solr.SolrSearchEngine.java

License:Apache License

@Override
public String[] searchInDocument(String searchTerm, String id) {
    // sanity check
    if (searchTerm == null || id == null || searchTerm.isEmpty() || id.isEmpty())
        return new String[] {};

    try {/*  ww  w .jav  a2 s  . co  m*/
        // only search content
        MultiFieldQueryParser parser = new MultiFieldQueryParser(Version.LUCENE_47, new String[] { "content" },
                analyzer);

        SolrQuery query = new SolrQuery();

        // set operator and contain by id
        parser.setDefaultOperator(QueryParser.Operator.AND);
        query.setQuery(parser.parse(searchTerm).toString());

        // filter by id
        query.addFilterQuery("id:" + id);
        query.setRows(1);

        // define highlighting
        query.setHighlight(true);
        query.addHighlightField(this.content);
        query.setHighlightFragsize(100);
        query.setHighlightSnippets(100);
        query.setHighlightSimplePre("<b>");
        query.setHighlightSimplePost("</b>");

        // do query
        QueryResponse response = solr.query(query);
        SolrDocumentList results = response.getResults();

        if (!results.isEmpty() && response.getHighlighting().get(id) != null) {
            List<String> fragments = response.getHighlighting().get(id).get(this.content);
            String[] bestFragments = new String[fragments.size() > 100 ? 100 : fragments.size()];
            for (int i = 0; i < bestFragments.length; i++)
                bestFragments[i] = fragments.get(i);
            return bestFragments;
        }
    } catch (Throwable e) {
        logger.error("Error in search.", e);
    }

    return new String[] {};
}

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedMatchesSource.java

License:Open Source License

@Override
public String getMarkup() {
    init(); //inits once

    String highLightField = null;

    String highlightQuery = keywordHitQuery;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
        //escape special lucene chars if not already escaped (if not a compound query)
        //TODO a better way to mark it a compound highlight query
        final String findSubstr = LuceneQuery.HIGHLIGHT_FIELD_REGEX + ":";
        if (!highlightQuery.contains(findSubstr)) {
            highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
        }//from  w w  w  .ja  va 2  s. com
    } else {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
        //escape special lucene chars always for literal queries query
        highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    String queryStr = null;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highLightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(highlightQuery);
        if (group) {
            sb.append("\"");
        }
        queryStr = sb.toString();
    } else {
        //use default field, simplifies query
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(highlightQuery);
    }

    q.setQuery(queryStr);

    final long contentId = content.getId();

    String contentIdStr = Long.toString(contentId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":" + contentIdStr;
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED);

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NO_MATCHES;
        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NO_MATCHES;
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>";
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Couldn't query markup for page: " + currentPage, ex);
        return "";
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Could not query markup for page: " + currentPage, ex);
        return "";
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedText.java

License:Open Source License

@Override
public String getText() {
    loadPageInfo(); //inits once

    String highLightField = null;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
    } else {//from w  w  w.  j av  a  2  s.  com
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    // input query has already been properly constructed and escaped
    q.setQuery(keywordHitQuery);

    String contentIdStr = Long.toString(this.objectId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":"
            + KeywordSearchUtil.escapeLuceneQuery(contentIdStr);
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>"; //NON-NLS
        }
    } catch (Exception ex) {
        logger.log(Level.WARNING, "Error executing Solr highlighting query: " + keywordHitQuery, ex); //NON-NLS
        return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.queryFailedMsg");
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.HighlightedTextMarkup.java

License:Open Source License

@Override
public String getMarkup() {
    loadPageInfo(); //inits once

    String highLightField = null;

    String highlightQuery = keywordHitQuery;

    if (isRegex) {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
        //escape special lucene chars if not already escaped (if not a compound query)
        //TODO a better way to mark it a compound highlight query
        final String findSubstr = LuceneQuery.HIGHLIGHT_FIELD_REGEX + ":";
        if (!highlightQuery.contains(findSubstr)) {
            highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
        }//from w ww .  j a v a 2  s  .c o  m
    } else {
        highLightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
        //escape special lucene chars always for literal queries query
        highlightQuery = KeywordSearchUtil.escapeLuceneQuery(highlightQuery);
    }

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug

    String queryStr = null;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highLightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(highlightQuery);
        if (group) {
            sb.append("\"");
        }
        queryStr = sb.toString();
    } else {
        //use default field, simplifies query
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(highlightQuery);
    }

    q.setQuery(queryStr);

    String contentIdStr = Long.toString(this.objectId);
    if (hasChunks) {
        contentIdStr += "_" + Integer.toString(this.currentPage);
    }

    final String filterQuery = Server.Schema.ID.toString() + ":"
            + KeywordSearchUtil.escapeLuceneQuery(contentIdStr);
    q.addFilterQuery(filterQuery);
    q.addHighlightField(highLightField); //for exact highlighting, try content_ws field (with stored="true" in Solr schema)

    //q.setHighlightSimplePre(HIGHLIGHT_PRE); //original highlighter only
    //q.setHighlightSimplePost(HIGHLIGHT_POST); //original highlighter only
    q.setHighlightFragsize(0); // don't fragment the highlight, works with original highlighter, or needs "single" list builder with FVH

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", HIGHLIGHT_PRE); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", HIGHLIGHT_POST); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "single"); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();

        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIdStr);
        if (responseHighlightID == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");

        }
        List<String> contentHighlights = responseHighlightID.get(highLightField);
        if (contentHighlights == null) {
            return NbBundle.getMessage(this.getClass(), "HighlightedMatchesSource.getMarkup.noMatchMsg");
        } else {
            // extracted content (minus highlight tags) is HTML-escaped
            String highlightedContent = contentHighlights.get(0).trim();
            highlightedContent = insertAnchors(highlightedContent);

            return "<html><pre>" + highlightedContent + "</pre></html>"; //NON-NLS
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Couldn't query markup for page: " + currentPage, ex); //NON-NLS
        return "";
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Could not query markup for page: " + currentPage, ex); //NON-NLS
        return "";
    }
}

From source file:org.sleuthkit.autopsy.keywordsearch.LuceneQuery.java

License:Open Source License

/**
 * Create the query object for the stored keyword
 *
 * @param snippets True if query should request snippets
 * @return//from w  w  w .j a  va 2s. c  o m
 */
private SolrQuery createAndConfigureSolrQuery(boolean snippets) {
    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug
    //set query, force quotes/grouping around all literal queries
    final String groupedQuery = KeywordSearchUtil.quoteQuery(keywordStringEscaped);
    String theQueryStr = groupedQuery;
    if (field != null) {
        //use the optional field
        StringBuilder sb = new StringBuilder();
        sb.append(field).append(":").append(groupedQuery);
        theQueryStr = sb.toString();
    }
    q.setQuery(theQueryStr);
    q.setRows(MAX_RESULTS);

    if (snippets) {
        q.setFields(Server.Schema.ID.toString());
    } else {
        q.setFields(Server.Schema.ID.toString());
    }

    for (KeywordQueryFilter filter : filters) {
        q.addFilterQuery(filter.toString());
    }

    if (snippets) {
        q.addHighlightField(Server.Schema.TEXT.toString());
        //q.setHighlightSimplePre("&laquo;"); //original highlighter only
        //q.setHighlightSimplePost("&raquo;");  //original highlighter only
        q.setHighlightSnippets(1);
        q.setHighlightFragsize(SNIPPET_LENGTH);

        //tune the highlighter
        q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
        q.setParam("hl.tag.pre", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
        q.setParam("hl.tag.post", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
        q.setParam("hl.fragListBuilder", "simple"); //makes sense for FastVectorHighlighter only NON-NLS

        //Solr bug if fragCharSize is smaller than Query string, StringIndexOutOfBoundsException is thrown.
        q.setParam("hl.fragCharSize", Integer.toString(theQueryStr.length())); //makes sense for FastVectorHighlighter only NON-NLS

        //docs says makes sense for the original Highlighter only, but not really
        //analyze all content SLOW! consider lowering
        q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS
    }

    return q;
}

From source file:org.sleuthkit.autopsy.keywordsearch.LuceneQuery.java

License:Open Source License

/**
 * return snippet preview context//  w w w.  j a  va  2 s  .  c  o m
 *
 * @param query the keyword query for text to highlight. Lucene special
 * cahrs should already be escaped.
 * @param solrObjectId Solr object id associated with the hit
 * @param chunkID chunk id associated with the content hit, or 0 if no
 * chunks
 * @param isRegex whether the query is a regular expression (different Solr
 * fields are then used to generate the preview)
 * @param group whether the query should look for all terms grouped together
 * in the query order, or not
 * @return
 */
public static String querySnippet(String query, long solrObjectId, int chunkID, boolean isRegex, boolean group)
        throws NoOpenCoreException {
    Server solrServer = KeywordSearch.getServer();

    String highlightField;
    if (isRegex) {
        highlightField = LuceneQuery.HIGHLIGHT_FIELD_REGEX;
    } else {
        highlightField = LuceneQuery.HIGHLIGHT_FIELD_LITERAL;
    }

    SolrQuery q = new SolrQuery();

    String queryStr;

    if (isRegex) {
        StringBuilder sb = new StringBuilder();
        sb.append(highlightField).append(":");
        if (group) {
            sb.append("\"");
        }
        sb.append(query);
        if (group) {
            sb.append("\"");
        }

        queryStr = sb.toString();
    } else {
        //simplify query/escaping and use default field
        //always force grouping/quotes
        queryStr = KeywordSearchUtil.quoteQuery(query);
    }

    q.setQuery(queryStr);

    String contentIDStr;

    if (chunkID == 0) {
        contentIDStr = Long.toString(solrObjectId);
    } else {
        contentIDStr = Server.getChunkIdString(solrObjectId, chunkID);
    }

    String idQuery = Server.Schema.ID.toString() + ":" + KeywordSearchUtil.escapeLuceneQuery(contentIDStr);
    q.setShowDebugInfo(DEBUG); //debug
    q.addFilterQuery(idQuery);
    q.addHighlightField(highlightField);
    //q.setHighlightSimplePre("&laquo;"); //original highlighter only
    //q.setHighlightSimplePost("&raquo;");  //original highlighter only
    q.setHighlightSnippets(1);
    q.setHighlightFragsize(SNIPPET_LENGTH);

    //tune the highlighter
    q.setParam("hl.useFastVectorHighlighter", "on"); //fast highlighter scales better than standard one NON-NLS
    q.setParam("hl.tag.pre", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.tag.post", "&laquo;"); //makes sense for FastVectorHighlighter only NON-NLS
    q.setParam("hl.fragListBuilder", "simple"); //makes sense for FastVectorHighlighter only NON-NLS

    //Solr bug if fragCharSize is smaller than Query string, StringIndexOutOfBoundsException is thrown.
    q.setParam("hl.fragCharSize", Integer.toString(queryStr.length())); //makes sense for FastVectorHighlighter only NON-NLS

    //docs says makes sense for the original Highlighter only, but not really
    //analyze all content SLOW! consider lowering
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //NON-NLS

    try {
        QueryResponse response = solrServer.query(q, METHOD.POST);
        Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();
        Map<String, List<String>> responseHighlightID = responseHighlight.get(contentIDStr);
        if (responseHighlightID == null) {
            return "";
        }
        List<String> contentHighlights = responseHighlightID.get(highlightField);
        if (contentHighlights == null) {
            return "";
        } else {
            // extracted content is HTML-escaped, but snippet goes in a plain text field
            return EscapeUtil.unEscapeHtml(contentHighlights.get(0)).trim();
        }
    } catch (NoOpenCoreException ex) {
        logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + query, ex); //NON-NLS
        throw ex;
    } catch (KeywordSearchModuleException ex) {
        logger.log(Level.WARNING, "Error executing Lucene Solr Query: " + query, ex); //NON-NLS
        return "";
    }
}