Example usage for org.apache.solr.client.solrj.request ContentStreamUpdateRequest setParams

List of usage examples for org.apache.solr.client.solrj.request ContentStreamUpdateRequest setParams

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.request ContentStreamUpdateRequest setParams.

Prototype

public void setParams(ModifiableSolrParams params) 

Source Link

Document

Sets the parameters for this update request, overwriting any previous

Usage

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

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();/*  ww w  . j a  v  a 2 s .  co  m*/
    if (flowFile == null) {
        return;
    }

    final AtomicReference<Exception> error = new AtomicReference<>(null);
    final AtomicReference<Exception> connectionError = new AtomicReference<>(null);

    final boolean isSolrCloud = SOLR_TYPE_CLOUD.equals(context.getProperty(SOLR_TYPE).getValue());
    final String collection = context.getProperty(COLLECTION).evaluateAttributeExpressions(flowFile).getValue();
    final Long commitWithin = context.getProperty(COMMIT_WITHIN).evaluateAttributeExpressions(flowFile)
            .asLong();
    final String contentStreamPath = context.getProperty(CONTENT_STREAM_PATH)
            .evaluateAttributeExpressions(flowFile).getValue();
    final MultiMapSolrParams requestParams = new MultiMapSolrParams(getRequestParams(context, flowFile));

    StopWatch timer = new StopWatch(true);
    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(final InputStream in) throws IOException {
            ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(contentStreamPath);
            request.setParams(new ModifiableSolrParams());

            // add the extra params, don't use 'set' in case of repeating params
            Iterator<String> paramNames = requestParams.getParameterNamesIterator();
            while (paramNames.hasNext()) {
                String paramName = paramNames.next();
                for (String paramValue : requestParams.getParams(paramName)) {
                    request.getParams().add(paramName, paramValue);
                }
            }

            // specify the collection for SolrCloud
            if (isSolrCloud) {
                request.setParam(COLLECTION_PARAM_NAME, collection);
            }

            if (commitWithin != null && commitWithin > 0) {
                request.setParam(COMMIT_WITHIN_PARAM_NAME, commitWithin.toString());
            }

            // if a username and password were provided then pass them for basic auth
            if (isBasicAuthEnabled()) {
                request.setBasicAuthCredentials(getUsername(), getPassword());
            }

            try (final BufferedInputStream bufferedIn = new BufferedInputStream(in)) {
                // add the FlowFile's content on the UpdateRequest
                request.addContentStream(new ContentStreamBase() {
                    @Override
                    public InputStream getStream() throws IOException {
                        return bufferedIn;
                    }

                    @Override
                    public String getContentType() {
                        return context.getProperty(CONTENT_TYPE).evaluateAttributeExpressions().getValue();
                    }
                });

                UpdateResponse response = request.process(getSolrClient());
                getLogger().debug("Got {} response from Solr", new Object[] { response.getStatus() });
            } catch (SolrException e) {
                error.set(e);
            } catch (SolrServerException e) {
                if (causedByIOException(e)) {
                    connectionError.set(e);
                } else {
                    error.set(e);
                }
            } catch (IOException e) {
                connectionError.set(e);
            }
        }
    });
    timer.stop();

    if (error.get() != null) {
        getLogger().error("Failed to send {} to Solr due to {}; routing to failure",
                new Object[] { flowFile, error.get() });
        session.transfer(flowFile, REL_FAILURE);
    } else if (connectionError.get() != null) {
        getLogger().error("Failed to send {} to Solr due to {}; routing to connection_failure",
                new Object[] { flowFile, connectionError.get() });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_CONNECTION_FAILURE);
    } else {
        StringBuilder transitUri = new StringBuilder("solr://");
        transitUri.append(getSolrLocation());
        if (isSolrCloud) {
            transitUri.append(":").append(collection);
        }

        final long duration = timer.getDuration(TimeUnit.MILLISECONDS);
        session.getProvenanceReporter().send(flowFile, transitUri.toString(), duration, true);
        getLogger().info("Successfully sent {} to Solr in {} millis", new Object[] { flowFile, duration });
        session.transfer(flowFile, REL_SUCCESS);
    }
}

From source file:org.craftercms.search.service.impl.SolrSearchService.java

License:Open Source License

@Override
public void updateContent(String indexId, String site, String id, File file,
        Map<String, List<String>> additionalFields) throws SearchException {
    if (StringUtils.isEmpty(indexId)) {
        indexId = defaultIndexId;// ww  w .j av  a 2  s.  c  o  m
    }

    String finalId = site + ":" + id;
    String fileName = FilenameUtils.getName(id);
    String contentType = mimeTypesMap.getContentType(fileName);
    ContentStreamUpdateRequest request = new ContentStreamUpdateRequest(SOLR_CONTENT_STREAM_UPDATE_URL);
    NamedList<Object> response;

    try {
        ModifiableSolrParams params = solrDocumentBuilder.buildParams(site, id,
                ExtractingParams.LITERALS_PREFIX, null, additionalFields);
        params.set(ExtractingParams.LITERALS_PREFIX + fileNameFieldName, fileName);

        request.setParams(params);
        request.addFile(file, contentType);
        request.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);

        response = solrClient.request(request, indexId);
    } catch (SolrServerException e) {
        logger.warn("{}Unable to update file through content stream request: {}. Attempting to perform just "
                + "the metadata update", getIndexPrefix(indexId), e.getMessage());

        try {
            SolrInputDocument inputDocument = solrDocumentBuilder.build(site, id, additionalFields);
            inputDocument.setField(fileNameFieldName, fileName);

            response = solrClient.add(indexId, inputDocument).getResponse();
        } catch (IOException e1) {
            throw new SearchException(indexId, "I/O error while executing update file for " + finalId, e1);
        } catch (SolrServerException e1) {
            throw new SearchException(indexId, e1.getMessage(), e1);
        }
    } catch (IOException e) {
        throw new SearchException(indexId, "I/O error while executing update file for " + finalId, e);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(getSuccessfulMessage(indexId, finalId, "Update file", response));
    }
}

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

License:BSD License

/**
 * Write the document to the index under the appropriate handle.
 *
 * @param doc the solr document to be written to the server
 * @param streams//w ww.j  a v a 2 s.c  o m
 * @throws IOException IO exception
 */
protected void writeDocument(SolrInputDocument doc, List<BitstreamContentStream> streams) throws IOException {

    try {
        if (getSolr() != null) {
            if (CollectionUtils.isNotEmpty(streams)) {
                ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update/extract");

                for (BitstreamContentStream bce : streams) {
                    req.addContentStream(bce);
                }

                ModifiableSolrParams params = new ModifiableSolrParams();

                //req.setParam(ExtractingParams.EXTRACT_ONLY, "true");
                for (String name : doc.getFieldNames()) {
                    for (Object val : doc.getFieldValues(name)) {
                        params.add(ExtractingParams.LITERALS_PREFIX + name, val.toString());
                    }
                }

                req.setParams(params);
                req.setParam(ExtractingParams.UNKNOWN_FIELD_PREFIX, "attr_");
                req.setParam(ExtractingParams.MAP_PREFIX + "content", "fulltext");
                req.setParam(ExtractingParams.EXTRACT_FORMAT, "text");
                req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
                req.process(getSolr());
            } else {
                getSolr().add(doc);
            }
        }
    } catch (SolrServerException e) {
        log.error(e.getMessage(), e);
    }
}

From source file:org.opensextant.matching.DataLoader.java

License:Open Source License

public static void main(String[] args) throws Exception {

    if (args.length < 3 || args.length > 4) {
        usage();/*from  w w w.j  ava 2s  .c o m*/
    }

    String scheme = args[0];
    String inputForm = args[1];
    String csvFilePath = args[2];
    String solrhome = "";
    if (args.length == 4) {
        solrhome = args[3];
    }

    // get a SolrServer with the proper core
    SolrServer solrServer = getSolrServer(scheme, solrhome);

    // convert indexed content to flat list
    // currently creates a temp file, could stream?
    if (inputForm.equalsIgnoreCase("index")) {
        csvFilePath = flatten(csvFilePath);
    }

    try {

        // set the fieldnames param for the selected schema
        final ModifiableSolrParams params = new ModifiableSolrParams(loadParams);
        if (scheme.equalsIgnoreCase("gazetteer")) {
            params.set("fieldnames", MatcherFactory.getGazetteerFieldNamesLoader());
        } else {
            params.set("fieldnames", MatcherFactory.getVocabFieldNames());
        }

        // build the update request
        final ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(requestHandler);
        updateRequest.setParams(params);

        ContentStream inStream = new ContentStreamBase.FileStream(new File(csvFilePath));

        // add the input file as a stream to the request
        updateRequest.addContentStream(inStream);

        // make the call
        SolrResponseBase response = null;
        try {
            response = (SolrResponseBase) updateRequest.process(solrServer);
            // see what happened
            printResponse(response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    } finally {
        // cleanup
        solrServer.shutdown();
    }
}