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

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

Introduction

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

Prototype

public SolrQuery addHighlightField(String f) 

Source Link

Document

add highlight field

Usage

From source file:org.dspace.discovery.SolrServiceImpl.java

License:BSD License

protected SolrQuery resolveToSolrQuery(Context context, DiscoverQuery discoveryQuery,
        boolean includeUnDiscoverable) {
    SolrQuery solrQuery = new SolrQuery();

    String query = "*:*";
    if (discoveryQuery.getQuery() != null) {
        query = discoveryQuery.getQuery();
    }/*from   w w w . j  a v a 2 s  .  co  m*/

    solrQuery.setQuery(query);
    if (discoveryQuery.isSpellCheck()) {
        solrQuery.setParam(SpellingParams.SPELLCHECK_Q, query);
        solrQuery.setParam(SpellingParams.SPELLCHECK_COLLATE, Boolean.TRUE);
        solrQuery.setParam("spellcheck", Boolean.TRUE);
    }

    if (!includeUnDiscoverable) {
        solrQuery.addFilterQuery("NOT(withdrawn:true)");
        solrQuery.addFilterQuery("NOT(discoverable:false)");
    }

    for (int i = 0; i < discoveryQuery.getFilterQueries().size(); i++) {
        String filterQuery = discoveryQuery.getFilterQueries().get(i);
        solrQuery.addFilterQuery(filterQuery);
    }
    if (discoveryQuery.getDSpaceObjectFilter() != -1) {
        solrQuery.addFilterQuery("search.resourcetype:" + discoveryQuery.getDSpaceObjectFilter());
    }

    for (int i = 0; i < discoveryQuery.getFieldPresentQueries().size(); i++) {
        String filterQuery = discoveryQuery.getFieldPresentQueries().get(i);
        solrQuery.addFilterQuery(filterQuery + ":[* TO *]");
    }

    if (discoveryQuery.getStart() != -1) {
        solrQuery.setStart(discoveryQuery.getStart());
    }

    if (discoveryQuery.getMaxResults() != -1) {
        solrQuery.setRows(discoveryQuery.getMaxResults());
    }

    if (discoveryQuery.getSortField() != null) {
        SolrQuery.ORDER order = SolrQuery.ORDER.asc;
        if (discoveryQuery.getSortOrder().equals(DiscoverQuery.SORT_ORDER.desc))
            order = SolrQuery.ORDER.desc;

        solrQuery.addSortField(discoveryQuery.getSortField(), order);
    }

    for (String property : discoveryQuery.getProperties().keySet()) {
        List<String> values = discoveryQuery.getProperties().get(property);
        solrQuery.add(property, values.toArray(new String[values.size()]));
    }

    List<DiscoverFacetField> facetFields = discoveryQuery.getFacetFields();
    if (0 < facetFields.size()) {
        //Only add facet information if there are any facets
        for (DiscoverFacetField facetFieldConfig : facetFields) {
            String field = transformFacetField(facetFieldConfig, facetFieldConfig.getField(), false);
            solrQuery.addFacetField(field);

            // Setting the facet limit in this fashion ensures that each facet can have its own max
            solrQuery.add("f." + field + "." + FacetParams.FACET_LIMIT,
                    String.valueOf(facetFieldConfig.getLimit()));
            String facetSort;
            if (DiscoveryConfigurationParameters.SORT.COUNT.equals(facetFieldConfig.getSortOrder())) {
                facetSort = FacetParams.FACET_SORT_COUNT;
            } else {
                facetSort = FacetParams.FACET_SORT_INDEX;
            }
            solrQuery.add("f." + field + "." + FacetParams.FACET_SORT, facetSort);
            if (facetFieldConfig.getOffset() != -1) {
                solrQuery.setParam("f." + field + "." + FacetParams.FACET_OFFSET,
                        String.valueOf(facetFieldConfig.getOffset()));
            }
            if (facetFieldConfig.getPrefix() != null) {
                solrQuery.setFacetPrefix(field, facetFieldConfig.getPrefix());
            }
        }

        List<String> facetQueries = discoveryQuery.getFacetQueries();
        for (String facetQuery : facetQueries) {
            solrQuery.addFacetQuery(facetQuery);
        }

        if (discoveryQuery.getFacetMinCount() != -1) {
            solrQuery.setFacetMinCount(discoveryQuery.getFacetMinCount());
        }

        solrQuery.setParam(FacetParams.FACET_OFFSET, String.valueOf(discoveryQuery.getFacetOffset()));
    }

    if (0 < discoveryQuery.getHitHighlightingFields().size()) {
        solrQuery.setHighlight(true);
        solrQuery.add(HighlightParams.USE_PHRASE_HIGHLIGHTER, Boolean.TRUE.toString());
        for (DiscoverHitHighlightingField highlightingField : discoveryQuery.getHitHighlightingFields()) {
            solrQuery.addHighlightField(highlightingField.getField() + "_hl");
            solrQuery.add("f." + highlightingField.getField() + "_hl." + HighlightParams.FRAGSIZE,
                    String.valueOf(highlightingField.getMaxChars()));
            solrQuery.add("f." + highlightingField.getField() + "_hl." + HighlightParams.SNIPPETS,
                    String.valueOf(highlightingField.getMaxSnippets()));
        }

    }

    //Add any configured search plugins !
    List<SolrServiceSearchPlugin> solrServiceSearchPlugins = new DSpace().getServiceManager()
            .getServicesByType(SolrServiceSearchPlugin.class);
    for (SolrServiceSearchPlugin searchPlugin : solrServiceSearchPlugins) {
        searchPlugin.additionalSearchParameters(context, discoveryQuery, solrQuery);
    }
    return solrQuery;
}

From source file:org.eclipse.rdf4j.sail.solr.SolrIndex.java

License:Open Source License

/**
 * Parse the passed query./*from w w  w .j  a v a2  s . co  m*/
 * 
 * @param query
 *        string
 * @return the parsed query
 * @throws ParseException
 *         when the parsing brakes
 */
@Override
protected Iterable<? extends DocumentScore> query(Resource subject, String query, URI propertyURI,
        boolean highlight) throws MalformedQueryException, IOException {
    SolrQuery q = prepareQuery(propertyURI, new SolrQuery(query));
    if (highlight) {
        q.setHighlight(true);
        String field = (propertyURI != null) ? SearchFields.getPropertyField(propertyURI) : "*";
        q.addHighlightField(field);
        q.setHighlightSimplePre(SearchFields.HIGHLIGHTER_PRE_TAG);
        q.setHighlightSimplePost(SearchFields.HIGHLIGHTER_POST_TAG);
        q.setHighlightSnippets(2);
    }

    QueryResponse response;
    if (q.getHighlight()) {
        q.addField("*");
    } else {
        q.addField(SearchFields.URI_FIELD_NAME);
    }
    q.addField("score");
    try {
        if (subject != null) {
            response = search(subject, q);
        } else {
            response = search(q);
        }
    } catch (SolrServerException e) {
        throw new IOException(e);
    }
    SolrDocumentList results = response.getResults();
    final Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
    return Iterables.transform(results, new Function<SolrDocument, DocumentScore>() {

        @Override
        public DocumentScore apply(SolrDocument document) {
            SolrSearchDocument doc = new SolrSearchDocument(document);
            Map<String, List<String>> docHighlighting = (highlighting != null) ? highlighting.get(doc.getId())
                    : null;
            return new SolrDocumentScore(doc, docHighlighting);
        }
    });
}

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

License:Apache License

/**
 * Helper method that sets the highlighting parameters.
 * //from   w w  w .jav a2s . c om
 * @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.ofbiz.solr.SolrProductSearch.java

License:Apache License

/**
 * Runs a query on the Solr Search Engine and returns the results.
 * <p>/*  w  w  w  .  jav a 2s. c  om*/
 * This function only returns an object of type QueryResponse, so it is probably not a good idea to call it directly from within the
 * groovy files (As a decent example on how to use it, however, use keywordSearch instead).
 */
public static Map<String, Object> runSolrQuery(DispatchContext dctx, Map<String, Object> context) {
    // get Connection
    HttpSolrServer server = null;
    Map<String, Object> result;

    Integer viewIndex = (Integer) context.get("viewIndex");
    Integer viewSize = (Integer) context.get("viewSize");
    if (viewIndex < 1) {
        viewIndex = 1;
    }
    try {
        server = new HttpSolrServer(SolrUtil.solrUrl);
        // create Query Object
        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery((String) context.get("query"));
        // solrQuery.setQueryType("dismax");
        boolean faceted = (Boolean) context.get("facet");
        if (faceted) {
            solrQuery.setFacet(faceted);
            //solrQuery.addFacetField("manu");
            solrQuery.addFacetField("features");
            solrQuery.addFacetField("cat");
            solrQuery.setFacetMinCount(1);
            solrQuery.setFacetLimit(8);

            solrQuery.addFacetQuery("listPrice:[0 TO 50]");
            solrQuery.addFacetQuery("listPrice:[50 TO 100]");
            solrQuery.addFacetQuery("listPrice:[100 TO 250]");
            solrQuery.addFacetQuery("listPrice:[250 TO 500]");
            solrQuery.addFacetQuery("listPrice:[500 TO 1000]");
            solrQuery.addFacetQuery("listPrice:[1000 TO 2500]");
            solrQuery.addFacetQuery("listPrice:[2500 TO 5000]");
            solrQuery.addFacetQuery("listPrice:[5000 TO 10000]");
            solrQuery.addFacetQuery("listPrice:[10000 TO 50000]");
            solrQuery.addFacetQuery("listPrice:[50000 TO *]");
        }

        boolean spellCheck = (Boolean) context.get("spellcheck");
        if (spellCheck) {
            solrQuery.setParam("spellcheck", spellCheck);
        }

        boolean highLight = (Boolean) context.get("highlight");
        if (highLight) {
            solrQuery.setHighlight(highLight);
            solrQuery.setHighlightSimplePre("<span class=\"highlight\">");
            solrQuery.addHighlightField("description");
            solrQuery.setHighlightSimplePost("</span>");
            solrQuery.setHighlightSnippets(2);
        }

        // Set additional Parameter
        // SolrQuery.ORDER order = SolrQuery.ORDER.desc;

        if (viewIndex != null && viewIndex > 0) {
            solrQuery.setStart((viewIndex - 1) * viewSize);
        }
        if (viewSize != null && viewSize > 0) {
            solrQuery.setRows(viewSize);
        }

        // if ((List) context.get("queryFilter") != null && ((ArrayList<SolrDocument>) context.get("queryFilter")).size() > 0) {
        // List filter = (List) context.get("queryFilter");
        // String[] tn = new String[filter.size()];
        // Iterator it = filter.iterator();
        // for (int i = 0; i < filter.size(); i++) {
        // tn[i] = (String) filter.get(i);
        // }
        // solrQuery.setFilterQueries(tn);
        // }
        String queryFilter = (String) context.get("queryFilter");
        if (UtilValidate.isNotEmpty(queryFilter))
            solrQuery.setFilterQueries(queryFilter.split(" "));
        if ((String) context.get("returnFields") != null) {
            solrQuery.setFields((String) context.get("returnFields"));
        }

        // if((Boolean)context.get("sortByReverse"))order.reverse();
        if ((String) context.get("sortBy") != null && ((String) context.get("sortBy")).length() > 0) {
            SolrQuery.ORDER order;
            if (!((Boolean) context.get("sortByReverse")))
                order = SolrQuery.ORDER.asc;
            else
                order = SolrQuery.ORDER.desc;
            solrQuery.setSort(((String) context.get("sortBy")).replaceFirst("-", ""), order);
        }

        if ((String) context.get("facetQuery") != null) {
            solrQuery.addFacetQuery((String) context.get("facetQuery"));
        }

        QueryResponse rsp = server.query(solrQuery);
        result = ServiceUtil.returnSuccess();
        result.put("queryResult", rsp);
    } catch (Exception e) {
        Debug.logError(e, e.getMessage(), module);
        result = ServiceUtil.returnError(e.toString());
    }
    return result;
}

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 {/*ww  w .jav  a2s  . com*/
        // 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 {/*w  w w.  j a va2s  . 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.AccountsText.java

License:Open Source License

@Override
@NbBundle.Messages({ "AccountsText.getMarkup.noMatchMsg="
        + "<html><pre><span style\\\\='background\\\\:yellow'>There were no keyword hits on this page. <br />"
        + "The keyword could have been in the file name."
        + " <br />Advance to another page if present, or to view the original text, choose File Text"
        + " <br />in the drop down menu to the right...</span></pre></html>",
        "AccountsText.getMarkup.queryFailedMsg="
                + "<html><pre><span style\\\\='background\\\\:yellow'>Failed to retrieve keyword hit results."
                + " <br />Confirm that Autopsy can connect to the Solr server. "
                + "<br /></span></pre></html>" })
public String getText() {
    loadPageInfo(); //inits once

    SolrQuery q = new SolrQuery();
    q.setShowDebugInfo(DEBUG); //debug
    q.addHighlightField(HIGHLIGHT_FIELD);
    q.setQuery(queryString);/*from w  w  w  .  j a  va 2 s.  com*/

    //set the documentID filter
    String queryDocumentID = this.solrObjectId + Server.CHUNK_ID_SEPARATOR + this.currentPage;
    q.addFilterQuery(Server.Schema.ID.toString() + ":" + queryDocumentID);

    //configure the highlighter
    q.setParam("hl.useFastVectorHighlighter", "true"); //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
    q.setParam("hl.maxAnalyzedChars", Server.HL_ANALYZE_CHARS_UNLIMITED); //docs says makes sense for the original Highlighter only, but not really //NON-NLS

    try {
        //extract highlighting and bail early on null responses
        Map<String, Map<String, List<String>>> highlightingPerDocument = solrServer.query(q, METHOD.POST)
                .getHighlighting();
        Map<String, List<String>> highlightingPerField = highlightingPerDocument.get(queryDocumentID);
        if (highlightingPerField == null) {
            return Bundle.AccountsText_getMarkup_noMatchMsg();
        }
        List<String> highlights = highlightingPerField.get(HIGHLIGHT_FIELD);
        if (highlights == null) {
            return Bundle.AccountsText_getMarkup_noMatchMsg();
        }

        //There should only be one item
        String highlighting = highlights.get(0).trim();

        /*
         * use regex matcher to iterate over occurences of HIGHLIGHT_PRE,
         * and prepend them with an anchor tag.
         */
        Matcher m = ANCHOR_DETECTION_PATTERN.matcher(highlighting);
        StringBuffer sb = new StringBuffer(highlighting.length());
        int count = 0;
        while (m.find()) {
            count++;
            m.appendReplacement(sb, INSERT_PREFIX + count + INSERT_POSTFIX);
        }
        m.appendTail(sb);

        //store total hits for this page, now that we know it
        this.numberOfHitsPerPage.put(this.currentPage, count);
        if (this.currentItem() == 0 && this.hasNextItem()) {
            this.nextItem();
        }

        // extracted content (minus highlight tags) is HTML-escaped
        return "<html><pre>" + sb.toString() + "</pre></html>"; //NON-NLS
    } catch (Exception ex) {
        LOGGER.log(Level.WARNING, "Error executing Solr highlighting query: " + keywords, ex); //NON-NLS
        return Bundle.AccountsText_getMarkup_queryFailedMsg();
    }
}

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 www .  ja v a2s  . 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);

    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 {// w  w w  .  ja va  2  s  .  c o  m
        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  w  w .  jav  a 2s.  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);

    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 "";
    }
}