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

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

Introduction

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

Prototype

public SolrQuery setSorts(List<SortClause> value) 

Source Link

Document

Replaces the current sort information.

Usage

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/* w w  w  .j  a v  a2  s . co m*/
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    parseAndConfigureFacets(facets, query);

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException
            | org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException e) {
        String message = e.getMessage();
        String bodytag = "<body>";
        if (message != null && message.contains(bodytag)) {
            message = message.substring(message.indexOf(bodytag) + bodytag.length(),
                    message.indexOf("</body>"));
            message = message.replaceAll("\\<[^>]*?>", "");
        }
        throw new GenericException("Could not query index, message: " + message, e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        Filter filter, Sorter sorter, Sublist sublist, Facets facets, RodaUser user, boolean showInactive)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*  w  w w  . ja va 2  s .co m*/
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());
    parseAndConfigureFacets(facets, query);
    if (hasPermissionFilters(classToRetrieve)) {
        query.addFilterQuery(getFilterQueries(user, showInactive));
    }

    try {
        QueryResponse response = index.query(getIndexName(classToRetrieve), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static <T extends Serializable> IndexResult<T> find(SolrClient index, Class<T> classToRetrieve,
        String tableUUID, Filter filter, Sorter sorter, Sublist sublist, Facets facets)
        throws GenericException, RequestNotValidException {
    IndexResult<T> ret;/*from   w w  w  .  jav a 2 s. co m*/
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    query.setStart(sublist.getFirstElementIndex());
    query.setRows(sublist.getMaximumElementCount());

    parseAndConfigureFacets(facets, query);

    try {
        QueryResponse response = index.query(getTableCollectionName(tableUUID), query);
        ret = queryResponseToIndexResult(response, classToRetrieve, facets);
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }

    return ret;
}

From source file:com.databasepreservation.visualization.utils.SolrUtils.java

public static InputStream findCSV(SolrClient index, String collection, Filter filter, Sorter sorter,
        Sublist sublist, List<String> fields) throws GenericException, RequestNotValidException {
    SolrQuery query = new SolrQuery();
    query.setQuery(parseFilter(filter));
    query.setSorts(parseSorter(sorter));
    if (sublist != null) {
        query.setStart(sublist.getFirstElementIndex());
        query.setRows(sublist.getMaximumElementCount());
    }//from   ww w  .  j  a  v  a 2s  . com
    query.setFields(fields.toArray(new String[0]));

    LOGGER.debug("CSV export query object: " + query.toString());
    LOGGER.debug("CSV export query: " + query.toQueryString());

    try {
        QueryRequest queryRequest = new QueryRequest(query);
        queryRequest.setResponseParser(new InputStreamResponseParser("csv"));
        QueryResponse response = queryRequest.process(index, collection);

        Object stream = response.getResponse().get("stream");
        if (stream instanceof InputStream) {
            return (InputStream) stream;
        } else {
            throw new GenericException(
                    "Result was not an input stream. Its string representation was: " + stream.toString());
        }
    } catch (SolrServerException | IOException e) {
        throw new GenericException("Could not query index", e);
    }
}

From source file:edu.harvard.liblab.ecru.rs.ResourceUtils.java

License:Open Source License

public static SolrQuery createSolrQuery(String q, List<String> fqs, String fl, String start, String rows,
        HashMap<String, List<String>> facetMap, String sort) {
    SolrQuery query = new SolrQuery();
    if (q == null || q.trim().isEmpty()) {
        q = QUERY_ALL;//  w  ww.j a  v a2s.co m
    }
    query.setQuery(q);
    if (fqs != null && !fqs.isEmpty()) {
        query.setFilterQueries(fqs.toArray(new String[fqs.size()]));
    }
    try {
        Integer s = new Integer(start);
        query.setStart(s);

    } catch (NumberFormatException e) {
        if (start != null)
            System.out.println("start not integer: " + start);
    }
    try {
        Integer r = new Integer(rows);
        query.setRows(r);

    } catch (NumberFormatException e) {
        if (rows != null)
            System.out.println("rows not integer: " + rows);
    }
    if (fl != null && !fl.isEmpty()) {
        if (fl.indexOf("type") < 0) {
            fl += ",type";
        }
        String[] fields = fl.split(",");
        for (int i = 0; i < fields.length; i++) {
            fields[i] = fields[i].trim();
        }

        query.setFields(fields);

    }
    if (sort != null && !sort.trim().isEmpty()) {
        String[] sortArr = sort.split(",");
        ArrayList<SortClause> clauses = new ArrayList<SortClause>();
        for (String s : sortArr) {
            if (!s.isEmpty()) {
                String[] sc = s.split(" ");
                String v = sc[0];
                String order = "asc";
                if (sc.length == 2) {
                    order = sc[1];
                }
                clauses.add(new SortClause(v, (order.equals("asc") ? ORDER.asc : ORDER.desc)));
            }
        }
        query.setSorts(clauses);
    }
    query = addFacetsToQuery(query, facetMap);
    return query;
}

From source file:nl.knaw.huygens.timbuctoo.index.solr.SolrIndex.java

License:Open Source License

private void addResultsOfPartialQuery(List<String> ids, List<Map<String, Object>> results,
        List<SolrQuery.SortClause> sortClauses) throws SearchException {
    StringBuilder queryBuilder = new StringBuilder(Entity.INDEX_FIELD_ID);
    queryBuilder.append(" : (");
    boolean isFirst = true;
    for (String id : ids) {
        if (!isFirst) {
            queryBuilder.append(" OR ");
        }/* ww w .  j  a v a 2  s.  c o  m*/
        queryBuilder.append(id);
        isFirst = false;
    }
    queryBuilder.append(")");

    SolrQuery query = new SolrQuery(queryBuilder.toString());

    query.setRows(ids.size());
    query.setSorts(sortClauses);

    results.addAll(getMultiRawResults(query));
}

From source file:org.apache.nifi.processors.solr.QuerySolr.java

License:Apache License

@Override
public void doOnTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();

    FlowFile flowFileOriginal = session.get();
    FlowFile flowFileResponse;//from  ww  w  .  j  a v a  2s  .co  m

    if (flowFileOriginal == null) {
        if (context.hasNonLoopConnection()) {
            return;
        }
        flowFileResponse = session.create();
    } else {
        flowFileResponse = session.create(flowFileOriginal);
    }

    final SolrQuery solrQuery = new SolrQuery();
    final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue());
    final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFileResponse)
            .getValue();

    final StringBuilder transitUri = new StringBuilder("solr://");
    transitUri.append(getSolrLocation());
    if (isSolrCloud) {
        transitUri.append(":").append(collection);
    }
    final StopWatch timer = new StopWatch(false);

    try {
        solrQuery.setQuery(context.getProperty(SOLR_PARAM_QUERY).evaluateAttributeExpressions(flowFileResponse)
                .getValue());
        solrQuery.setRequestHandler(context.getProperty(SOLR_PARAM_REQUEST_HANDLER)
                .evaluateAttributeExpressions(flowFileResponse).getValue());

        if (context.getProperty(SOLR_PARAM_FIELD_LIST).isSet()) {
            for (final String field : context.getProperty(SOLR_PARAM_FIELD_LIST)
                    .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) {
                solrQuery.addField(field.trim());
            }
        }

        // Avoid ArrayIndexOutOfBoundsException due to incorrectly configured sorting
        try {
            if (context.getProperty(SOLR_PARAM_SORT).isSet()) {
                final List<SolrQuery.SortClause> sortings = new ArrayList<>();
                for (final String sorting : context.getProperty(SOLR_PARAM_SORT)
                        .evaluateAttributeExpressions(flowFileResponse).getValue().split(",")) {
                    final String[] sortEntry = sorting.trim().split(" ");
                    sortings.add(new SolrQuery.SortClause(sortEntry[0], sortEntry[1]));
                }
                solrQuery.setSorts(sortings);
            }
        } catch (Exception e) {
            throw new ProcessException("Error while parsing the sort clauses for the Solr query");
        }

        final Integer startParam = context.getProperty(SOLR_PARAM_START).isSet()
                ? Integer.parseInt(context.getProperty(SOLR_PARAM_START)
                        .evaluateAttributeExpressions(flowFileResponse).getValue())
                : CommonParams.START_DEFAULT;

        solrQuery.setStart(startParam);

        final Integer rowParam = context.getProperty(SOLR_PARAM_ROWS).isSet()
                ? Integer.parseInt(context.getProperty(SOLR_PARAM_ROWS)
                        .evaluateAttributeExpressions(flowFileResponse).getValue())
                : CommonParams.ROWS_DEFAULT;

        solrQuery.setRows(rowParam);

        final Map<String, String[]> additionalSolrParams = SolrUtils.getRequestParams(context,
                flowFileResponse);

        final Set<String> searchComponents = extractSearchComponents(additionalSolrParams);
        solrQuery.add(new MultiMapSolrParams(additionalSolrParams));

        final Map<String, String> attributes = new HashMap<>();
        attributes.put(ATTRIBUTE_SOLR_CONNECT, getSolrLocation());
        if (isSolrCloud) {
            attributes.put(ATTRIBUTE_SOLR_COLLECTION, collection);
        }
        attributes.put(ATTRIBUTE_SOLR_QUERY, solrQuery.toString());
        if (flowFileOriginal != null) {
            flowFileOriginal = session.putAllAttributes(flowFileOriginal, attributes);
        }

        flowFileResponse = session.putAllAttributes(flowFileResponse, attributes);

        final boolean getEntireResults = RETURN_ALL_RESULTS
                .equals(context.getProperty(AMOUNT_DOCUMENTS_TO_RETURN).getValue());
        boolean processFacetsAndStats = true;
        boolean continuePaging = true;

        while (continuePaging) {

            timer.start();

            Map<String, String> responseAttributes = new HashMap<>();
            responseAttributes.put(ATTRIBUTE_SOLR_START, solrQuery.getStart().toString());
            responseAttributes.put(ATTRIBUTE_SOLR_ROWS, solrQuery.getRows().toString());

            if (solrQuery.getStart() > UPPER_LIMIT_START_PARAM) {
                logger.warn(
                        "The start parameter of Solr query {} exceeded the upper limit of {}. The query will not be processed "
                                + "to avoid performance or memory issues on the part of Solr.",
                        new Object[] { solrQuery.toString(), UPPER_LIMIT_START_PARAM });
                flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes);
                timer.stop();
                break;
            }

            final QueryRequest req = new QueryRequest(solrQuery);
            if (isBasicAuthEnabled()) {
                req.setBasicAuthCredentials(getUsername(), getPassword());
            }

            final QueryResponse response = req.process(getSolrClient());
            timer.stop();

            final Long totalNumberOfResults = response.getResults().getNumFound();

            responseAttributes.put(ATTRIBUTE_SOLR_NUMBER_RESULTS, totalNumberOfResults.toString());
            responseAttributes.put(ATTRIBUTE_CURSOR_MARK, response.getNextCursorMark());
            responseAttributes.put(ATTRIBUTE_SOLR_STATUS, String.valueOf(response.getStatus()));
            responseAttributes.put(ATTRIBUTE_QUERY_TIME, String.valueOf(response.getQTime()));
            flowFileResponse = session.putAllAttributes(flowFileResponse, responseAttributes);

            if (response.getResults().size() > 0) {

                if (context.getProperty(RETURN_TYPE).getValue().equals(MODE_XML.getValue())) {
                    flowFileResponse = session.write(flowFileResponse,
                            SolrUtils.getOutputStreamCallbackToTransformSolrResponseToXml(response));
                    flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(),
                            MIME_TYPE_XML);
                } else {
                    final RecordSetWriterFactory writerFactory = context.getProperty(RECORD_WRITER)
                            .evaluateAttributeExpressions(flowFileResponse)
                            .asControllerService(RecordSetWriterFactory.class);
                    final RecordSchema schema = writerFactory.getSchema(flowFileResponse.getAttributes(), null);
                    final RecordSet recordSet = SolrUtils.solrDocumentsToRecordSet(response.getResults(),
                            schema);
                    final StringBuffer mimeType = new StringBuffer();
                    final FlowFile flowFileResponseRef = flowFileResponse;
                    flowFileResponse = session.write(flowFileResponse, out -> {
                        try (final RecordSetWriter writer = writerFactory.createWriter(getLogger(), schema, out,
                                flowFileResponseRef)) {
                            writer.write(recordSet);
                            writer.flush();
                            mimeType.append(writer.getMimeType());
                        } catch (SchemaNotFoundException e) {
                            throw new ProcessException("Could not parse Solr response", e);
                        }
                    });
                    flowFileResponse = session.putAttribute(flowFileResponse, CoreAttributes.MIME_TYPE.key(),
                            mimeType.toString());
                }

                if (processFacetsAndStats) {
                    if (searchComponents.contains(FacetParams.FACET)) {
                        FlowFile flowFileFacets = session.create(flowFileResponse);
                        flowFileFacets = session.write(flowFileFacets, out -> {
                            try (final OutputStreamWriter osw = new OutputStreamWriter(out);
                                    final JsonWriter writer = new JsonWriter(osw)) {
                                addFacetsFromSolrResponseToJsonWriter(response, writer);
                            }
                        });
                        flowFileFacets = session.putAttribute(flowFileFacets, CoreAttributes.MIME_TYPE.key(),
                                MIME_TYPE_JSON);
                        session.getProvenanceReporter().receive(flowFileFacets, transitUri.toString(),
                                timer.getDuration(TimeUnit.MILLISECONDS));
                        session.transfer(flowFileFacets, FACETS);
                    }

                    if (searchComponents.contains(StatsParams.STATS)) {
                        FlowFile flowFileStats = session.create(flowFileResponse);
                        flowFileStats = session.write(flowFileStats, out -> {
                            try (final OutputStreamWriter osw = new OutputStreamWriter(out);
                                    final JsonWriter writer = new JsonWriter(osw)) {
                                addStatsFromSolrResponseToJsonWriter(response, writer);
                            }
                        });
                        flowFileStats = session.putAttribute(flowFileStats, CoreAttributes.MIME_TYPE.key(),
                                MIME_TYPE_JSON);
                        session.getProvenanceReporter().receive(flowFileStats, transitUri.toString(),
                                timer.getDuration(TimeUnit.MILLISECONDS));
                        session.transfer(flowFileStats, STATS);
                    }
                    processFacetsAndStats = false;
                }
            }

            if (getEntireResults) {
                final Integer totalDocumentsReturned = solrQuery.getStart() + solrQuery.getRows();
                if (totalDocumentsReturned < totalNumberOfResults) {
                    solrQuery.setStart(totalDocumentsReturned);
                    session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(),
                            timer.getDuration(TimeUnit.MILLISECONDS));
                    session.transfer(flowFileResponse, RESULTS);
                    flowFileResponse = session.create(flowFileResponse);
                } else {
                    continuePaging = false;
                }
            } else {
                continuePaging = false;
            }
        }

    } catch (Exception e) {
        flowFileResponse = session.penalize(flowFileResponse);
        flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION, e.getClass().getName());
        flowFileResponse = session.putAttribute(flowFileResponse, EXCEPTION_MESSAGE, e.getMessage());
        session.transfer(flowFileResponse, FAILURE);
        logger.error("Failed to execute query {} due to {}. FlowFile will be routed to relationship failure",
                new Object[] { solrQuery.toString(), e }, e);
        if (flowFileOriginal != null) {
            flowFileOriginal = session.penalize(flowFileOriginal);
        }
    }

    if (!flowFileResponse.isPenalized()) {
        session.getProvenanceReporter().receive(flowFileResponse, transitUri.toString(),
                timer.getDuration(TimeUnit.MILLISECONDS));
        session.transfer(flowFileResponse, RESULTS);
    }

    if (flowFileOriginal != null) {
        if (!flowFileOriginal.isPenalized()) {
            session.transfer(flowFileOriginal, ORIGINAL);
        } else {
            session.remove(flowFileOriginal);
        }
    }
}

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  w  w  . ja  v a  2 s.  co  m*/
    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;// w w  w . j  a va  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;
}