Example usage for org.apache.solr.client.solrj.request UpdateRequest UpdateRequest

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

Introduction

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

Prototype

public UpdateRequest(String url) 

Source Link

Usage

From source file:org.apache.camel.component.solr.SolrProducer.java

License:Apache License

private void insert(Exchange exchange, boolean isStreaming) throws Exception {
    Object body = exchange.getIn().getBody();
    if (body instanceof WrappedFile) {
        body = ((WrappedFile<?>) body).getFile();
    }/*from  w  ww . j  a  v  a2s .c  o  m*/

    if (body instanceof File) {
        MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
        String mimeType = mimeTypesMap.getContentType((File) body);
        ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
        updateRequest.addFile((File) body, mimeType);

        for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
            if (entry.getKey().startsWith(SolrConstants.PARAM)) {
                String paramName = entry.getKey().substring(SolrConstants.PARAM.length());
                updateRequest.setParam(paramName, entry.getValue().toString());
            }
        }

        if (isStreaming) {
            updateRequest.process(streamingSolrServer);
        } else {
            updateRequest.process(solrServer);
        }

    } else if (body instanceof SolrInputDocument) {

        UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
        updateRequest.add((SolrInputDocument) body);

        if (isStreaming) {
            updateRequest.process(streamingSolrServer);
        } else {
            updateRequest.process(solrServer);
        }

    } else {

        boolean hasSolrHeaders = false;
        for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
            if (entry.getKey().startsWith(SolrConstants.FIELD)) {
                hasSolrHeaders = true;
                break;
            }
        }

        if (hasSolrHeaders) {

            UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());

            SolrInputDocument doc = new SolrInputDocument();
            for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
                if (entry.getKey().startsWith(SolrConstants.FIELD)) {
                    String fieldName = entry.getKey().substring(SolrConstants.FIELD.length());
                    doc.setField(fieldName, entry.getValue());
                }
            }
            updateRequest.add(doc);

            if (isStreaming) {
                updateRequest.process(streamingSolrServer);
            } else {
                updateRequest.process(solrServer);
            }

        } else if (body instanceof String) {

            String bodyAsString = (String) body;

            if (!bodyAsString.startsWith("<add")) {
                bodyAsString = "<add>" + bodyAsString + "</add>";
            }

            DirectXmlRequest xmlRequest = new DirectXmlRequest(getRequestHandler(), bodyAsString);

            if (isStreaming) {
                streamingSolrServer.request(xmlRequest);
            } else {
                solrServer.request(xmlRequest);
            }
        } else {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                    "unable to find data in Exchange to update Solr");
        }
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.solr.server.RemoteSolrServerProvider.java

License:Apache License

private void createCollectionIfNeeded(CloudSolrServer cloudSolrServer) throws SolrServerException {
    String solrCollection = remoteSolrServerConfiguration.getSolrCollection();
    try {//from ww w .  j  av a  2 s  .c o m
        ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
        SolrZkClient zkClient = zkStateReader.getZkClient();
        if (zkClient.isConnected() && !zkClient.exists("/configs/" + solrCollection, false)) {
            String solrConfDir = remoteSolrServerConfiguration.getSolrConfDir();
            File dir;
            if (solrConfDir != null && solrConfDir.length() > 0) {
                dir = new File(solrConfDir);
            } else {
                dir = new File(getClass().getResource("/solr/oak/conf").getFile());
            }
            ZkController.uploadConfigDir(zkClient, dir, solrCollection);
            UpdateRequest req = new UpdateRequest("/admin/collections");
            req.setParam("action", "CREATE");
            req.setParam("numShards", String.valueOf(remoteSolrServerConfiguration.getSolrShardsNo()));
            req.setParam("replicationFactor",
                    String.valueOf(remoteSolrServerConfiguration.getSolrReplicationFactor()));
            req.setParam("collection.configName", solrCollection);
            req.setParam("name", solrCollection);
            cloudSolrServer.request(req);
        }
    } catch (Exception e) {
        log.warn("could not create collection {}", solrCollection);
        throw new SolrServerException(e);
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.solr.server.RemoteSolrServerProviderIT.java

License:Apache License

private boolean canCreateCollections(String host) throws Exception {
    UpdateRequest req = new UpdateRequest("/admin/collections");
    req.setParam("action", "CREATE");
    String solrCollection = "solr_" + System.nanoTime();
    req.setParam("name", solrCollection);
    req.setParam("numShards", "2");
    req.setParam("replicationFactor", "2");
    req.setParam("collection.configName", "myconf");
    CloudSolrServer cloudSolrServer = new CloudSolrServer(host);
    cloudSolrServer.setZkConnectTimeout(1000);
    NamedList<Object> request = cloudSolrServer.request(req);
    return request != null && request.get("success") != null;
}

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

License:Apache License

private void index(boolean isSolrCloud, String collection, Long commitWithin, String contentStreamPath,
        MultiMapSolrParams requestParams, List<SolrInputDocument> inputDocumentList)
        throws IOException, SolrServerException, SolrException {
    UpdateRequest request = new UpdateRequest(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);
        }//from ww w  . j a  v a  2s . c o  m
    }

    // 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());
    }
    request.add(inputDocumentList);
    UpdateResponse response = request.process(getSolrClient());
    getLogger().debug("Got {} response from Solr", new Object[] { response.getStatus() });
    inputDocumentList.clear();
}