Search in sources :

Example 1 with DirectXmlRequest

use of org.apache.solr.client.solrj.request.DirectXmlRequest in project lucene-solr by apache.

the class TestContentStreamDataSource method testSimple.

@Test
public void testSimple() throws Exception {
    DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("command", "full-import");
    params.set("clean", "false");
    req.setParams(params);
    try (HttpSolrClient solrClient = getHttpSolrClient(buildUrl(jetty.getLocalPort(), "/solr/collection1"))) {
        solrClient.request(req);
        ModifiableSolrParams qparams = new ModifiableSolrParams();
        qparams.add("q", "*:*");
        QueryResponse qres = solrClient.query(qparams);
        SolrDocumentList results = qres.getResults();
        assertEquals(2, results.getNumFound());
        SolrDocument doc = results.get(0);
        assertEquals("1", doc.getFieldValue("id"));
        assertEquals("Hello C1", ((List) doc.getFieldValue("desc")).get(0));
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 2 with DirectXmlRequest

use of org.apache.solr.client.solrj.request.DirectXmlRequest in project lucene-solr by apache.

the class TestContentStreamDataSource method testCommitWithin.

@Test
public void testCommitWithin() throws Exception {
    DirectXmlRequest req = new DirectXmlRequest("/dataimport", xml);
    ModifiableSolrParams params = params("command", "full-import", "clean", "false", UpdateParams.COMMIT, "false", UpdateParams.COMMIT_WITHIN, "1000");
    req.setParams(params);
    try (HttpSolrClient solrServer = getHttpSolrClient(buildUrl(jetty.getLocalPort(), "/solr/collection1"))) {
        solrServer.request(req);
        Thread.sleep(100);
        ModifiableSolrParams queryAll = params("q", "*", "df", "desc");
        QueryResponse qres = solrServer.query(queryAll);
        SolrDocumentList results = qres.getResults();
        assertEquals(0, results.getNumFound());
        Thread.sleep(1000);
        for (int i = 0; i < 10; i++) {
            qres = solrServer.query(queryAll);
            results = qres.getResults();
            if (2 == results.getNumFound()) {
                return;
            }
            Thread.sleep(500);
        }
    }
    fail("Commit should have occured but it did not");
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test)

Example 3 with DirectXmlRequest

use of org.apache.solr.client.solrj.request.DirectXmlRequest in project camel by apache.

the class SolrProducer method insert.

private void insert(Exchange exchange, SolrClient solrServer) throws Exception {
    Object body = exchange.getIn().getBody();
    boolean invalid = false;
    if (body instanceof WrappedFile) {
        body = ((WrappedFile<?>) body).getFile();
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class))) {
        String mimeType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        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());
            }
        }
        updateRequest.process(solrServer);
    } else {
        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());
                }
            }
            updateRequest.process(solrServer);
        } else if (body instanceof SolrInputDocument) {
            UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
            updateRequest.add((SolrInputDocument) body);
            updateRequest.process(solrServer);
        } else if (body instanceof List<?>) {
            List<?> list = (List<?>) body;
            if (list.size() > 0 && list.get(0) instanceof SolrInputDocument) {
                UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
                updateRequest.add((List<SolrInputDocument>) list);
                updateRequest.process(solrServer);
            } else {
                invalid = true;
            }
        } 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);
                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);
                solrServer.request(xmlRequest);
            } else {
                invalid = true;
            }
        }
    }
    if (invalid) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unable to find data in Exchange to update Solr");
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WrappedFile(org.apache.camel.WrappedFile) List(java.util.List) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Map(java.util.Map) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) SolrException(org.apache.solr.common.SolrException)

Aggregations

DirectXmlRequest (org.apache.solr.client.solrj.request.DirectXmlRequest)3 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 Test (org.junit.Test)2 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)1 WrappedFile (org.apache.camel.WrappedFile)1 ContentStreamUpdateRequest (org.apache.solr.client.solrj.request.ContentStreamUpdateRequest)1 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrException (org.apache.solr.common.SolrException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1