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

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

Introduction

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

Prototype

public SolrQuery setHighlight(boolean b) 

Source Link

Usage

From source file:com.plugtree.solrmeter.model.service.impl.QueryServiceSolrJImpl.java

License:Apache License

protected SolrQuery createQuery(String q, String fq, String qt, boolean highlight, String facetFields,
        String sort, String sortOrder, Integer rows, Integer start, String otherParams) throws QueryException {
    SolrQuery query = new SolrQuery();
    if (q != null) {
        query.setQuery(q);//from  w w w. j av a  2  s.  c  om
    }
    if (fq != null) {
        List<String> filterQueries = this.getFilterQueries(fq);
        for (String filterQuery : filterQueries) {
            query.addFilterQuery(filterQuery);
        }
    }
    if (qt != null) {
        query.setQueryType(qt);
    }
    query.setHighlight(highlight);
    if (facetFields == null || "".equals(facetFields)) {
        query.setFacet(false);
    } else {
        query.setFacet(true);
        List<String> facets = this.getFacets(facetFields);
        for (String facet : facets) {
            query.addFacetField(facet);
        }
    }
    if (sort != null && !"".equals(sort)) {
        query.setSortField(sort, ORDER.valueOf(sortOrder));
    }
    if (rows != null && rows < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (rows != null) {
        query.setRows(rows);
    }
    if (start != null && start < 0) {
        throw new QueryException("Rows can't be less than 0");
    } else if (start != null) {
        query.setStart(start);
    }

    if (otherParams != null) {
        List<String> params = this.getOtherParams(otherParams);
        for (String param : params) {
            query.add(getParamName(param), getParamValue(param));
        }
    }
    return query;
}

From source file:com.seajas.search.attender.service.search.SolrSearchService.java

License:Open Source License

/**
 * {@inheritDoc}/* www . ja  v  a 2  s  .  c  om*/
 */
@Override
public List<SearchResult> performSearch(final String query, final Date startDate, final Date endDate,
        final Map<String, String> parameters, final Integer maxResults, final String taxonomyLanguage) {
    List<SearchResult> results = new ArrayList<SearchResult>(maxResults);

    SolrQuery solrQuery = new SolrQuery();
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

    // Combine the shards into a Solr-acceptable list

    String shardList = "";

    for (String shard : shards)
        shardList += (shardList.length() > 0 ? "," : "") + shard;

    solrQuery.setQuery(query);
    solrQuery.setRows(maxResults);
    solrQuery.setParam("shards", shardList);
    solrQuery.addFilterQuery("dcterms_created:[" + dateFormatter.format(startDate) + " TO "
            + dateFormatter.format(endDate) + "]");

    // Construct a field list for highlighting

    String[] fieldList = new String[(searchLanguages.size() * 2) + 2];

    fieldList[0] = "content";
    fieldList[1] = "title";

    for (int i = 2, j = 0; i < fieldList.length; i += 2, j++) {
        fieldList[i] = "content-" + searchLanguages.get(j);
        fieldList[i + 1] = "title-" + searchLanguages.get(j);
    }

    // Enable highlighting for the content (and leaving the title for now)

    solrQuery.setHighlight(true);
    solrQuery.setHighlightSnippets(2);
    solrQuery.setParam("hl.fl", fieldList);

    solrQuery.setParam("f.title.hl.fragsize", "0");
    solrQuery.setParam("f.content.hl.fragsize", "100");

    for (String language : searchLanguages) {
        solrQuery.setParam("f.title-" + language + ".hl.fragsize", "0");
        solrQuery.setParam("f.content-" + language + ".hl.fragsize", "100");
    }

    for (Entry<String, String> parameter : parameters.entrySet()) {
        if (parameter.getKey().equalsIgnoreCase("dcterms_coverage")
                || parameter.getKey().equalsIgnoreCase("dcterms_format"))
            solrQuery.addFilterQuery(parameter.getKey() + ":(" + parameter.getValue() + ")");
        else if (parameter.getKey().equalsIgnoreCase("dcterms_type"))
            solrQuery.addFilterQuery("-" + parameter.getKey() + ":(" + parameter.getValue() + ")");
        else
            solrQuery.addFilterQuery(parameter.getKey() + ":\"" + parameter.getValue() + "\"");
    }

    try {
        QueryResponse response = solrServer.query(solrQuery);
        Iterator<SolrDocument> iterator = response.getResults().iterator();

        for (int i = 0; i < maxResults && iterator.hasNext(); i++) {
            SolrDocument document = iterator.next();

            // Retrieve the (potentially) highlighted summary

            String id = (String) document.getFieldValue("id");
            String language = (String) document.getFieldValue("dcterms_language");
            String author = (String) document.getFieldValue("author");
            String sourceId = null;

            // Simply take the last source ID

            if (document.getFieldValues("dcterms_coverage") != null
                    && document.getFieldValues("dcterms_coverage").size() > 0)
                for (Object coverageId : document.getFieldValues("dcterms_coverage"))
                    sourceId = coverageId.toString();

            String contentField = StringUtils.hasText(language) ? "content-" + language : "content";
            String titleField = StringUtils.hasText(language) ? "title-" + language : "title";

            String summary = (String) document.getFieldValue(contentField);

            if (summary.length() > 300)
                summary = summary.substring(0, 300) + " &hellip;";

            if (response.getHighlighting().get(id) != null) {
                List<String> highlightSnippets = response.getHighlighting().get(id).get(contentField);

                if (highlightSnippets != null && highlightSnippets.size() > 0) {
                    String fragmentPrefixOne = highlightSnippets.get(0).length() > 99 ? " .. " : "";

                    summary = fragmentPrefixOne + highlightSnippets.get(0) + fragmentPrefixOne;

                    if (highlightSnippets.size() > 1) {
                        String fragmentSuffixTwo = highlightSnippets.get(1).length() > 99 ? " .. " : "";

                        summary += highlightSnippets.get(1) + fragmentSuffixTwo;
                    }
                }
            }

            results.add(new SearchResult((String) document.get("url"), (String) document.get(titleField),
                    author, sourceId, summary, (Date) document.get("dcterms_created"),
                    (String) document.get("dcterms_alternative")));
        }
    } catch (SolrServerException e) {
        logger.error("Could not retrieve Solr results for query '" + query + "'", e);
    }

    // Now collect and update the source IDs to their actual sources

    return adjustSourceIds(results, taxonomyLanguage);
}

From source file:com.thinkbiganalytics.search.SolrSearchService.java

License:Apache License

private QueryResponse executeSearch(String query, int size, int start) {
    final String COLLECTION_LIST = "kylo-data," + SearchIndex.DATASOURCES; //Solr admin to configure beforehand
    final String ALL_FIELDS = "*";
    final String BOLD_HIGHLIGHT_START = "<font style='font-weight:bold'>";
    final String BOLD_HIGHLIGHT_END = "</font>";

    SolrQuery solrQuery = new SolrQuery();
    solrQuery.set("q", query);
    solrQuery.setRows(size);/*from  ww  w . j a v  a 2  s. c om*/
    solrQuery.setStart(start);
    solrQuery.setParam("collection", COLLECTION_LIST);
    solrQuery.setHighlight(true);
    solrQuery.set("hl.fl", ALL_FIELDS);
    solrQuery.setHighlightSimplePre(BOLD_HIGHLIGHT_START);
    solrQuery.setHighlightSimplePost(BOLD_HIGHLIGHT_END);
    solrQuery.setHighlightRequireFieldMatch(false);

    try {
        return client.query("kylo-data", solrQuery);
    } catch (SolrServerException | IOException e) {
        throw new RuntimeException("Error encountered during search.");
    }
}

From source file:com.tripod.solr.query.StandardSolrQueryTransformer.java

License:Apache License

@Override
public SolrQuery transform(final Q query) {
    final SolrQuery solrQuery = new SolrQuery(query.getQuery());
    solrQuery.setStart(query.getOffset());
    solrQuery.setRows(query.getRows());//w  w w .ja v a  2  s . c  o m
    solrQuery.setParam("q.op", query.getDefaultOperator().name());

    if (query.getReturnFields() != null) {
        query.getReturnFields().stream().forEach(f -> solrQuery.addField(f.getName()));
    }

    if (query.getHighlightFields() != null && !query.getHighlightFields().isEmpty()) {
        solrQuery.setHighlight(true);
        query.getHighlightFields().stream().forEach(hf -> solrQuery.addHighlightField(hf.getName()));
    }

    if (query.getFacetFields() != null) {
        query.getFacetFields().stream().forEach(ff -> solrQuery.addFacetField(ff.getName()));
    }

    if (query.getSorts() != null) {
        for (Sort sort : query.getSorts()) {
            SolrQuery.ORDER solrOrder = sort.getSortOrder() == SortOrder.ASC ? SolrQuery.ORDER.asc
                    : SolrQuery.ORDER.desc;
            SolrQuery.SortClause sortClause = new SolrQuery.SortClause(sort.getField().getName(), solrOrder);
            solrQuery.addSort(sortClause);
        }
    }

    if (query.getFilterQueries() != null) {
        query.getFilterQueries().stream().forEach(fq -> solrQuery.addFilterQuery(fq));
    }

    if (query.getParams() != null) {
        query.getParams().entrySet().stream().forEach(e -> solrQuery.add(e.getKey(), e.getValue()));
    }

    return solrQuery;
}

From source file:com.villemos.ispace.solr.SolrProducer.java

License:Open Source License

private void configureQuery(SolrQuery query)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    /** Set the default values. May be overridden by later settings. */
    query.setRows(endpoint.getRows());/*w  w w .j  a  v  a  2  s. co m*/
    query.setSortField(endpoint.getSortField(), endpoint.getSortOrder());

    /** We per default always set highlighting. */
    query.setHighlight(true).setHighlightSnippets(1);
    query.setParam("hl.fl", "withRawText");

    /** Configure facets. */
    query.setFacet(endpoint.getFacets());
    if (endpoint.getFacets() == true) {
        query.setQuery(endpoint.getQuery());
        query.setFacetSort(endpoint.getFacetsort());
        query.setFacetLimit(endpoint.getFacetlimit());
        query.setFacetPrefix(endpoint.getFacetprefix());
        query.setFacetMinCount(endpoint.getMinCount());
        query.setFacetMissing(endpoint.isFacetMissing());
    }

    query.addFacetField(endpoint.getFacetField());
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Gets values to autocomplete for the input string.
 * The autocomplete feature works for multivalued fields and is based on a highlighting trick.
 * See http://solr.pl/en/2013/02/25/autocomplete-on-multivalued-fields-using-highlighting/
 * @param keywordStart/* w w w  .  ja va  2 s  . co m*/
 * @return Autocomplete values.
 * @throws SolrServerException
 */
public Set<String> getTextToAutocomplete(String keywordStart) throws SolrServerException {
    SolrQuery query = new SolrQuery();
    query.setQuery(IndexField.AUTOCOMPLETE.getValue() + ":" + keywordStart);
    query.setFields(IndexField.AUTOCOMPLETE.getValue());
    query.setHighlight(true);
    query.setParam("hl.fl", IndexField.AUTOCOMPLETE.getValue());
    query.setHighlightSimplePre("");
    query.setHighlightSimplePost("");
    query.setRows(FullTextSearchUtils.AUTOCOMPLETE_ROWS);

    Map<String, Integer> map = new TreeMap<String, Integer>();
    QueryResponse response = solrServer.query(query);

    Set<String> foundIds = response.getHighlighting().keySet();
    for (String id : foundIds) {

        List<String> resultsPerDocument = response.getHighlighting().get(id)
                .get(IndexField.AUTOCOMPLETE.getValue());
        if (resultsPerDocument != null) {
            for (String result : resultsPerDocument) {
                String resultValue;
                int resultFrequency;
                int delimiterPosition = result.lastIndexOf('#');
                if (delimiterPosition == -1) { // autocomplete phrase was copied from title
                    resultValue = result;
                    resultFrequency = 0;
                } else {
                    resultValue = result.substring(0, delimiterPosition);
                    try {
                        resultFrequency = Integer
                                .valueOf(result.substring(delimiterPosition + 1, result.length()));
                    } catch (NumberFormatException e) {
                        resultFrequency = 0;
                    }
                }

                map.put(resultValue.toLowerCase(), resultFrequency);
            }
        }

        if (map.size() == FullTextSearchUtils.AUTOCOMPLETE_ROWS) {
            break;
        }
    }

    map = sortByValue(map);
    return map.keySet();
}

From source file:cz.zcu.kiv.eegdatabase.logic.search.FulltextSearchService.java

License:Apache License

/**
 * Given the input query, finds out the total count of full text results for each full text result type.
 * @param solrQuery The search query.//from  w  w  w .  j a v a2  s  . co m
 * @return Map containg category-count pairs.
 */
public Map<String, Long> getCategoryFacets(String solrQuery) {
    SolrQuery query = new SolrQuery(solrQuery);
    query.setParam("fl", IndexField.UUID.getValue());
    query.setHighlight(false);
    query.setFacet(true);
    query.addFacetField(IndexField.CLASS.getValue());

    Map<String, Long> results = new HashMap<String, Long>();
    QueryResponse response = null;
    try {
        response = solrServer.query(query);
    } catch (SolrServerException e) {
        log.error(e);
    }
    long totalCount = 0;
    List<FacetField> facets = response.getFacetFields();
    for (FacetField field : facets) {
        log.info("count: " + field.getValueCount());
        List<FacetField.Count> facetEntries = field.getValues();
        for (FacetField.Count count : facetEntries) {
            long countValue = count.getCount();
            results.put(count.getName(), countValue);
            log.info(count.getName() + ", " + countValue);
            totalCount += countValue;
        }
    }

    // add a "facet" for all results
    results.put(ResultCategory.ALL.getValue(), totalCount);

    return results;
}

From source file:de.dlr.knowledgefinder.webapp.webservice.solr.query.AbstractSolrQueryFactory.java

License:Apache License

private void setSolrQueryParameter(SolrQuery solrQuery) {
    solrQuery.setHighlight(true);
    solrQuery.setParam("hl.fragsize", "0");
    solrQuery.setParam("hl.preserveMulti", "true");
}

From source file:eu.prestoprime.search.util.SolrQueryUtils.java

License:Open Source License

/**
 * Enable Solr's Highlighting Component on this query object. Search terms
 * in the map (as values) are only highlighted in the belonging search
 * fields.//from ww w.java 2s.  c o  m
 * 
 * @param solrQuery
 * @param fieldMap
 */
public static void enableHighlight(SolrQuery solrQuery, final Map<Schema.searchField, String> fieldMap) {
    // enable highlighting on Query
    solrQuery.setHighlight(true);

    // unlimited fragment size and hightlights
    solrQuery.setHighlightFragsize(0);

    StringBuilder logMsg = new StringBuilder("Enable highlighting on fields: ");

    // use these to set a start/end tag for highlights other than <em>
    // (defined in Solr Config)
    // solrQuery.setHighlightSimplePre(Constants.getString("HIGHLIGHT_PRE"));
    // solrQuery.setHighlightSimplePost(Constants.getString("HIGHLIGHT_POST"));

    if (fieldMap == null || fieldMap.isEmpty()) { // dismax query -> enable
        // highlighting on all
        // fields
        solrQuery.set("hl.highlightMultiTerm", true);

        for (searchField s : Schema.catchAll) {
            solrQuery.addHighlightField(s.getFieldName());
            logMsg.append(s.getFieldName() + ", ");
        }

        LOGGER.debug(logMsg.toString().substring(0, logMsg.length() - 2));
    } else {
        // highlight matching words in queried fields only
        solrQuery.set("hl.usePhraseHighlighter", true);
        solrQuery.setHighlightRequireFieldMatch(true);
        for (Entry<Schema.searchField, String> e : fieldMap.entrySet()) {
            // highlighting on range fields does no work. Just use text
            // fields
            if (e.getKey().getType().equals(Schema.FieldType.P4_TEXT)
                    || e.getKey().getType().equals(Schema.FieldType.STRING)) {
                logMsg.append(e.getKey().getFieldName() + ", ");
                solrQuery.addHighlightField(e.getKey().getFieldName());
            }
        }
        LOGGER.debug(logMsg.toString().substring(0, logMsg.length() - 2));
    }
}

From source file:eumetsat.pn.solr.webapp.SolrApp.java

@Override
protected Map<String, Object> search(String searchTerms, String filterString, int from, int size) {
    Map<String, Object> data = new HashMap<>();
    // put "session" parameters here rightaway so it can be used in template even when empty result
    data.put("search_terms", searchTerms == null ? "*:*" : searchTerms);
    data.put("filter_terms", filterString == null ? "" : filterString);

    Stopwatch stopwatch = Stopwatch.createStarted();

    try {/* w ww. j  a v  a  2s . co m*/
        SolrQuery query = new SolrQuery();

        query.setQuery(searchTerms);
        query.setStart(from == -1 ? 0 : from);
        query.setRows(size);
        query.setFields("id", "title", "description", "thumbnail_s", "status_s", "score"); // "exclude" xmldoc
        query.setParam("qt", "edismax"); // probably default already

        // boosting
        query.setParam("qf", "title^10 description status^2 keywords");

        // set highlight, see also https://cwiki.apache.org/confluence/display/solr/Standard+Highlighter
        query.setHighlight(true).setHighlightSnippets(17).setHighlightFragsize(0); // http://wiki.apache.org/solr/HighlightingParameters
        query.setParam("hl.preserveMulti", "true"); // preserve non-matching keywords
        query.setParam("hl.fl", "id", "title", "description", "keywords"); // "*"); // select fields to highlight
        // override defaults:
        query.setParam("hl.simple.pre", "<em><strong>");
        query.setParam("hl.simple.post", "</strong></em>");

        // configure faceting, see also http://wiki.apache.org/solr/SolrFacetingOverview and http://wiki.apache.org/solr/Solrj and https://wiki.apache.org/solr/SimpleFacetParameters and 
        query.setFacet(true).setFacetLimit(4).setFacetMissing(true);
        // not in API, probably normally set in schema.xml:
        query.setParam("facet.field", "satellite_s", "instrument_s", "category", "societalBenefitArea_ss",
                "distribution_ss");

        // filtering
        Set<String> hiddenFacets = new HashSet<>(); // hiding no facets yet
        if (filterString != null && !filterString.isEmpty()) {
            Multimap<String, String> filterTermsMap = parseFiltersTerms(filterString);

            if (filterTermsMap.size() > 0) {
                for (Map.Entry<String, String> entry : filterTermsMap.entries()) {
                    String filter = " +" + entry.getKey() + ":" + entry.getValue();
                    query.addFilterQuery(filter);

                    hiddenFacets.add(entry.getKey() + ":" + entry.getValue());
                }
            }
        }
        data.put("tohide", hiddenFacets);

        log.debug("Solr query: {}", query);
        QueryResponse response = solr.query(query);

        if (response == null) {
            log.error("Response from {} is null!", this.name);
            data.put("total_hits", 0);
            data = addMessage(data, MessageLevel.danger, "Response is null from " + this.name);
        } else {
            log.trace("Got response: {}", response);

            if (response.getStatus() == 0) {
                List<Map<String, Object>> resHits = new ArrayList<>();
                SolrDocumentList results = response.getResults();
                Map<String, Map<String, List<String>>> highlights = response.getHighlighting();

                data.put("total_hits", results.getNumFound());
                if (results.getNumFound() < 1) {
                    addMessage(data, MessageLevel.info, "No results found!");
                }

                data.put("max_score", results.getMaxScore());
                Map<String, Object> pagination = computePaginationParams(
                        ((Long) (data.get("total_hits"))).intValue(), from);
                data.put("pagination", pagination);

                for (SolrDocument result : results) {
                    HashMap<String, Object> resHit = new HashMap<>();

                    String currentId = (String) result.getFieldValue("id");
                    Map<String, List<String>> currentHighlights = highlights.get(currentId);
                    resHit.put("id", currentId);
                    resHit.put("score", String.format("%.4g", result.getFieldValue("score")));

                    resHit.put("abstract", hightlightIfGiven(result, currentHighlights, "description"));

                    resHit.put("title", hightlightIfGiven(result, currentHighlights, "title"));
                    resHit.put("keywords", Joiner.on(", ").join(
                            (Collection<String>) hightlightIfGiven(result, currentHighlights, "keywords")));
                    resHit.put("satellite", result.get("satellite_s"));
                    resHit.put("thumbnail", result.get("thumbnail_s"));
                    resHit.put("status", result.get("status_s"));
                    resHit.put("distribution", result.get("distribution_ss"));

                    resHits.add(resHit);
                }

                data.put("hits", resHits);

                // faceting information:
                List<FacetField> facets = response.getFacetFields();
                log.trace("Facets ({}): {}", facets.size(), facets);

                //jsObj.get("facets").get("categories").get("terms") - then term und count
                // convert to format of Elasticsearch:
                Map<String, Object> facetsJson = new HashMap<>();
                for (FacetField facet : facets) {
                    Map<String, Object> facetMap = new HashMap<>();
                    facetMap.put("total", facet.getValueCount());
                    List<Map<String, Object>> terms = new ArrayList<>();
                    for (Count count : facet.getValues()) {
                        if (count.getCount() > 0) {
                            Map<String, Object> termMap = new HashMap<>();
                            termMap.put("count", count.getCount());
                            termMap.put("term", count.getName() == null ? "N/A" : count.getName());
                            terms.add(termMap);
                        }
                    }
                    facetMap.put("terms", terms);
                    facetsJson.put(facet.getName(), facetMap);
                }
                data.put("facets", facetsJson);
            } else { // non-OK resonse
                log.error("Received non-200 response: {}", response);
                data = addMessage(data, MessageLevel.danger, "Non 200 response: " + response.toString());
            }
        }

        data.put("elapsed", (double) (stopwatch.elapsed(TimeUnit.MILLISECONDS)) / (double) 1000);
        log.trace("Prepared data for template: {}", data);
    } catch (SolrServerException e) {
        log.error("Error querying Solr", e);
        addMessage(data, MessageLevel.danger, "Error during search: " + e.getMessage());
        //            errorResponse(e);
    }

    stopwatch.stop();

    return data;
}