Example usage for org.apache.solr.client.solrj SolrClient query

List of usage examples for org.apache.solr.client.solrj SolrClient query

Introduction

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

Prototype

public QueryResponse query(SolrParams params, METHOD method) throws SolrServerException, IOException 

Source Link

Document

Performs a query to the Solr server

Usage

From source file:org.roda.core.index.utils.SolrUtils.java

public static <T extends IsIndexed> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets, List<String> fieldsToReturn)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*from   w ww.  j  a va 2 s .c om*/
    SolrQuery query = new SolrQuery();
    query.setParam("q.op", DEFAULT_QUERY_PARSER_OPERATOR);
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    if (!fieldsToReturn.isEmpty()) {
        query.setFields(fieldsToReturn.toArray(new String[fieldsToReturn.size()]));
    }
    parseAndConfigureFacets(facets, query);

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve).get(0), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets, fieldsToReturn);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    } catch (SolrException e) {
        throw new RequestNotValidException(e);
    } catch (RuntimeException e) {
        throw new GenericException("Unexpected exception while querying index", e);
    }

    return ret;
}

From source file:org.roda.core.index.utils.SolrUtils.java

public static <T extends IsIndexed> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets, User user, boolean justActive,
        List<String> fieldsToReturn) throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*from   w w w  .  j a v a  2 s.com*/
    SolrQuery query = new SolrQuery();
    query.setParam("q.op", DEFAULT_QUERY_PARSER_OPERATOR);
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    query.setFields(fieldsToReturn.toArray(new String[fieldsToReturn.size()]));
    parseAndConfigureFacets(facets, query);
    if (hasPermissionFilters(classToRetrieve)) {
        query.addFilterQuery(getFilterQueries(user, justActive, classToRetrieve));
    }

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve).get(0), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets, fieldsToReturn);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    } catch (SolrException e) {
        throw new RequestNotValidException(e.getMessage());
    } catch (RuntimeException e) {
        throw new GenericException("Unexpected exception while querying index", e);
    }

    return ret;
}

From source file:org.roda.core.index.utils.SolrUtils.java

/**
 * WARNING: this should only be used to debug/tests only
 * //from   w w  w  .j  a  va 2  s.  c om
 * @return
 * @throws IOException
 * @throws SolrServerException
 */
public static QueryResponse executeSolrQuery(SolrClient index, String collection, String solrQueryString)
        throws SolrServerException, IOException {
    LOGGER.trace("query string: {}", solrQueryString);
    SolrQuery query = new SolrQuery();
    for (String string : solrQueryString.split("&")) {
        String[] split = string.split("=");
        query.add(split[0], split[1]);
    }
    LOGGER.trace("executeSolrQuery: {}", query);
    return index.query(collection, query);
}

From source file:org.roda.core.index.utils.SolrUtils.java

public static <T extends IsIndexed> List<String> suggest(SolrClient index, Class<T> classToRetrieve,
        String field, String queryString, boolean justActive, User user, boolean allowPartial)
        throws GenericException {
    StringBuilder queryBuilder = new StringBuilder();
    appendKeyValue(queryBuilder, field + RodaConstants.INDEX_SEARCH_SUFFIX, queryString + "*");
    SolrQuery query = new SolrQuery();
    query.setParam("q.op", DEFAULT_QUERY_PARSER_OPERATOR);
    query.setQuery(queryBuilder.toString());
    if (hasPermissionFilters(classToRetrieve)) {
        query.addFilterQuery(getFilterQueries(user, justActive, classToRetrieve));
    }//w  ww  .  j  a  v  a  2 s  .  com
    parseAndConfigureFacets(new Facets(new SimpleFacetParameter(field)), query);
    List<String> suggestions = new ArrayList<>();
    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve).get(0), query);
        response.getFacetField(field).getValues().forEach(count -> suggestions.add(count.getName()));
    } catch (SolrServerException | IOException | SolrException e) {
        throw new GenericException("Could not get suggestions", e);
    } catch (RuntimeException e) {
        throw new GenericException("Unexpected exception while querying index", e);
    }

    return suggestions;
}