Search in sources :

Example 6 with SolrQueryRequestBase

use of org.apache.solr.request.SolrQueryRequestBase in project lucene-solr by apache.

the class AutoCommitTest method testCommitWithin.

public void testCommitWithin() throws Exception {
    SolrCore core = h.getCore();
    NewSearcherListener trigger = new NewSearcherListener();
    core.registerNewSearcherListener(trigger);
    DirectUpdateHandler2 updater = (DirectUpdateHandler2) core.getUpdateHandler();
    CommitTracker tracker = updater.softCommitTracker;
    tracker.setTimeUpperBound(0);
    tracker.setDocsUpperBound(-1);
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    MapSolrParams params = new MapSolrParams(new HashMap<String, String>());
    // Add a single document with commitWithin == 4 second
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    req.setContentStreams(toContentStreams(adoc(4000, "id", "529", "field_t", "what's inside?", "subject", "info"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Check it isn't in the index
    assertQ("shouldn't find any", req("id:529"), "//result[@numFound=0]");
    // Wait longer than the commitWithin time
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    // Add one document without commitWithin
    req.setContentStreams(toContentStreams(adoc("id", "530", "field_t", "what's inside?", "subject", "info"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Check it isn't in the index
    assertQ("shouldn't find any", req("id:530"), "//result[@numFound=0]");
    // Delete one document with commitWithin
    trigger.pause();
    req.setContentStreams(toContentStreams(delI("529", "commitWithin", "2000"), null));
    trigger.reset();
    handler.handleRequest(req, rsp);
    // Now make sure we can find it
    assertQ("should find one", req("id:529"), "//result[@numFound=1]");
    trigger.unpause();
    // Wait for the commit to happen
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    // Now we shouldn't find it
    assertQ("should find none", req("id:529"), "//result[@numFound=0]");
    // ... but we should find the new one
    assertQ("should find one", req("id:530"), "//result[@numFound=1]");
    trigger.reset();
    // now make the call 10 times really fast and make sure it 
    // only commits once
    req.setContentStreams(toContentStreams(adoc(2000, "id", "500"), null));
    for (int i = 0; i < 10; i++) {
        handler.handleRequest(req, rsp);
    }
    assertQ("should not be there yet", req("id:500"), "//result[@numFound=0]");
    // the same for the delete
    req.setContentStreams(toContentStreams(delI("530", "commitWithin", "1000"), null));
    for (int i = 0; i < 10; i++) {
        handler.handleRequest(req, rsp);
    }
    assertQ("should be there", req("id:530"), "//result[@numFound=1]");
    assertTrue("commitWithin failed to commit", trigger.waitForNewSearcher(30000));
    assertQ("should be there", req("id:500"), "//result[@numFound=1]");
    assertQ("should not be there", req("id:530"), "//result[@numFound=0]");
    assertEquals(3, tracker.getCommitCount());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler)

Example 7 with SolrQueryRequestBase

use of org.apache.solr.request.SolrQueryRequestBase in project lucene-solr by apache.

the class AutoCommitTest method testMaxDocs.

public void testMaxDocs() throws Exception {
    SolrCore core = h.getCore();
    NewSearcherListener trigger = new NewSearcherListener();
    DirectUpdateHandler2 updateHandler = (DirectUpdateHandler2) core.getUpdateHandler();
    CommitTracker tracker = updateHandler.softCommitTracker;
    tracker.setTimeUpperBound(-1);
    tracker.setDocsUpperBound(14);
    core.registerNewSearcherListener(trigger);
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    MapSolrParams params = new MapSolrParams(new HashMap<String, String>());
    // Add documents
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequestBase req = new SolrQueryRequestBase(core, params) {
    };
    for (int i = 0; i < 14; i++) {
        req.setContentStreams(toContentStreams(adoc("id", Integer.toString(i), "subject", "info"), null));
        handler.handleRequest(req, rsp);
    }
    // It should not be there right away
    assertQ("shouldn't find any", req("id:1"), "//result[@numFound=0]");
    assertEquals(0, tracker.getCommitCount());
    req.setContentStreams(toContentStreams(adoc("id", "14", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    assertTrue(trigger.waitForNewSearcher(15000));
    req.setContentStreams(toContentStreams(adoc("id", "15", "subject", "info"), null));
    handler.handleRequest(req, rsp);
    // Now make sure we can find it
    assertQ("should find one", req("id:14"), "//result[@numFound=1]");
    assertEquals(1, tracker.getCommitCount());
    // But not the one added afterward
    assertQ("should not find one", req("id:15"), "//result[@numFound=0]");
    assertEquals(1, tracker.getCommitCount());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler)

Example 8 with SolrQueryRequestBase

use of org.apache.solr.request.SolrQueryRequestBase in project lucene-solr by apache.

the class SolrTestCaseJ4 method addDoc.

public static void addDoc(String doc, String updateRequestProcessorChain) throws Exception {
    Map<String, String[]> params = new HashMap<>();
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    params.put(UpdateParams.UPDATE_CHAIN, new String[] { updateRequestProcessorChain });
    SolrQueryRequestBase req = new SolrQueryRequestBase(h.getCore(), (SolrParams) mmparams) {
    };
    UpdateRequestHandler handler = new UpdateRequestHandler();
    handler.init(null);
    ArrayList<ContentStream> streams = new ArrayList<>(2);
    streams.add(new ContentStreamBase.StringStream(doc));
    req.setContentStreams(streams);
    handler.handleRequestBody(req, new SolrQueryResponse());
    req.close();
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 9 with SolrQueryRequestBase

use of org.apache.solr.request.SolrQueryRequestBase in project lucene-solr by apache.

the class SolrRequestParsers method buildRequestFrom.

private SolrQueryRequest buildRequestFrom(SolrCore core, SolrParams params, Collection<ContentStream> streams, RTimerTree requestTimer, final HttpServletRequest req) throws Exception {
    // The content type will be applied to all streaming content
    String contentType = params.get(CommonParams.STREAM_CONTENTTYPE);
    // Handle anything with a remoteURL
    String[] strs = params.getParams(CommonParams.STREAM_URL);
    if (strs != null) {
        if (!enableRemoteStreams) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Remote Streaming is disabled.");
        }
        for (final String url : strs) {
            ContentStreamBase stream = new ContentStreamBase.URLStream(new URL(url));
            if (contentType != null) {
                stream.setContentType(contentType);
            }
            streams.add(stream);
        }
    }
    // Handle streaming files
    strs = params.getParams(CommonParams.STREAM_FILE);
    if (strs != null) {
        if (!enableRemoteStreams) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Remote Streaming is disabled.");
        }
        for (final String file : strs) {
            ContentStreamBase stream = new ContentStreamBase.FileStream(new File(file));
            if (contentType != null) {
                stream.setContentType(contentType);
            }
            streams.add(stream);
        }
    }
    // Check for streams in the request parameters
    strs = params.getParams(CommonParams.STREAM_BODY);
    if (strs != null) {
        for (final String body : strs) {
            ContentStreamBase stream = new ContentStreamBase.StringStream(body);
            if (contentType != null) {
                stream.setContentType(contentType);
            }
            streams.add(stream);
        }
    }
    final HttpSolrCall httpSolrCall = req == null ? null : (HttpSolrCall) req.getAttribute(HttpSolrCall.class.getName());
    SolrQueryRequestBase q = new SolrQueryRequestBase(core, params, requestTimer) {

        @Override
        public Principal getUserPrincipal() {
            return req == null ? null : req.getUserPrincipal();
        }

        @Override
        public List<CommandOperation> getCommands(boolean validateInput) {
            if (httpSolrCall != null) {
                return httpSolrCall.getCommands(validateInput);
            }
            return super.getCommands(validateInput);
        }

        @Override
        public Map<String, String> getPathTemplateValues() {
            if (httpSolrCall != null && httpSolrCall instanceof V2HttpCall) {
                return ((V2HttpCall) httpSolrCall).getUrlParts();
            }
            return super.getPathTemplateValues();
        }

        @Override
        public HttpSolrCall getHttpSolrCall() {
            return httpSolrCall;
        }
    };
    if (streams != null && streams.size() > 0) {
        q.setContentStreams(streams);
    }
    return q;
}
Also used : CommandOperation(org.apache.solr.common.util.CommandOperation) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) URL(java.net.URL) V2HttpCall(org.apache.solr.api.V2HttpCall) File(java.io.File) SolrException(org.apache.solr.common.SolrException) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 10 with SolrQueryRequestBase

use of org.apache.solr.request.SolrQueryRequestBase in project lucene-solr by apache.

the class HttpSolrCall method sendError.

protected void sendError(Throwable ex) throws IOException {
    Exception exp = null;
    SolrCore localCore = null;
    try {
        SolrQueryResponse solrResp = new SolrQueryResponse();
        if (ex instanceof Exception) {
            solrResp.setException((Exception) ex);
        } else {
            solrResp.setException(new RuntimeException(ex));
        }
        localCore = core;
        if (solrReq == null) {
            final SolrParams solrParams;
            if (req != null) {
                // use GET parameters if available:
                solrParams = SolrRequestParsers.parseQueryString(req.getQueryString());
            } else {
                // we have no params at all, use empty ones:
                solrParams = new MapSolrParams(Collections.<String, String>emptyMap());
            }
            solrReq = new SolrQueryRequestBase(core, solrParams) {
            };
        }
        QueryResponseWriter writer = core.getQueryResponseWriter(solrReq);
        writeResponse(solrResp, writer, Method.GET);
    } catch (Exception e) {
        // This error really does not matter
        exp = e;
    } finally {
        try {
            if (exp != null) {
                SimpleOrderedMap info = new SimpleOrderedMap();
                int code = ResponseUtils.getErrorInfo(ex, info, log);
                sendError(code, info.toString());
            }
        } finally {
            if (core == null && localCore != null) {
                localCore.close();
            }
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrCore(org.apache.solr.core.SolrCore) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) EOFException(java.io.EOFException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)15 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)11 SolrCore (org.apache.solr.core.SolrCore)8 ContentStream (org.apache.solr.common.util.ContentStream)7 UpdateRequestHandler (org.apache.solr.handler.UpdateRequestHandler)7 MapSolrParams (org.apache.solr.common.params.MapSolrParams)6 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)5 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)5 ArrayList (java.util.ArrayList)4 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)4 Test (org.junit.Test)4 DocumentAnalysisRequest (org.apache.solr.client.solrj.request.DocumentAnalysisRequest)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 File (java.io.File)2 HashMap (java.util.HashMap)2 SolrException (org.apache.solr.common.SolrException)2 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)2 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1