Search in sources :

Example 31 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SignatureUpdateProcessorFactoryTest method testNonStringFieldsValues.

@Test
public void testNonStringFieldsValues() throws Exception {
    this.chain = "dedupe-allfields";
    SolrCore core = h.getCore();
    UpdateRequestProcessorChain chained = core.getUpdateProcessingChain(chain);
    SignatureUpdateProcessorFactory factory = ((SignatureUpdateProcessorFactory) chained.getProcessors().get(0));
    factory.setEnabled(true);
    Map<String, String[]> params = new HashMap<>();
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    params.put(UpdateParams.UPDATE_CHAIN, new String[] { chain });
    UpdateRequest ureq = new UpdateRequest();
    {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 1.0f);
        doc.addField("ints_is", 34);
        doc.addField("ints_is", 42);
        ureq.add(doc);
    }
    {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 2.0f);
        doc.addField("ints_is", 42);
        doc.addField("ints_is", 66);
        ureq.add(doc);
    }
    {
        // A and B should have same sig as eachother
        // even though the particulars of how the the ints_is list are built
        SolrInputDocument docA = new SolrInputDocument();
        SolrInputDocument docB = new SolrInputDocument();
        UnusualList<Integer> ints = new UnusualList<>(3);
        for (int val : new int[] { 42, 66, 34 }) {
            docA.addField("ints_is", new Integer(val));
            ints.add(val);
        }
        docB.addField("ints_is", ints);
        for (SolrInputDocument doc : new SolrInputDocument[] { docA, docB }) {
            doc.addField("v_t", "same");
            doc.addField("weight", 3.0f);
            ureq.add(doc);
        }
    }
    {
        // now add another doc with the same values as A & B above, 
        // but diff ints_is collection (diff order)
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("v_t", "same");
        doc.addField("weight", 3.0f);
        for (int val : new int[] { 66, 42, 34 }) {
            doc.addField("ints_is", new Integer(val));
        }
        ureq.add(doc);
    }
    ArrayList<ContentStream> streams = new ArrayList<>(2);
    streams.add(new BinaryRequestWriter().getContentStream(ureq));
    LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), mmparams);
    try {
        req.setContentStreams(streams);
        UpdateRequestHandler h = new UpdateRequestHandler();
        h.init(new NamedList());
        h.handleRequestBody(req, new SolrQueryResponse());
    } finally {
        req.close();
    }
    addDoc(commit());
    checkNumDocs(4);
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams) HashMap(java.util.HashMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrCore(org.apache.solr.core.SolrCore) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ContentStream(org.apache.solr.common.util.ContentStream) UpdateRequestHandler(org.apache.solr.handler.UpdateRequestHandler) Test(org.junit.Test)

Example 32 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class UniqFieldsUpdateProcessorFactoryTest method addDoc.

private void addDoc(String doc) throws Exception {
    Map<String, String[]> params = new HashMap<>();
    MultiMapSolrParams mmparams = new MultiMapSolrParams(params);
    params.put(UpdateParams.UPDATE_CHAIN, new String[] { "uniq-fields" });
    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) 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 33 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class DirectSolrConnection method request.

public String request(SolrRequestHandler handler, SolrParams params, String body) throws Exception {
    if (params == null)
        params = new MapSolrParams(new HashMap<String, String>());
    // Make a stream for the 'body' content
    List<ContentStream> streams = new ArrayList<>(1);
    if (body != null && body.length() > 0) {
        streams.add(new ContentStreamBase.StringStream(body));
    }
    SolrQueryRequest req = null;
    try {
        req = parser.buildRequestFrom(core, params, streams);
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        core.execute(handler, req, rsp);
        if (rsp.getException() != null) {
            throw rsp.getException();
        }
        // Now write it out
        QueryResponseWriter responseWriter = core.getQueryResponseWriter(req);
        StringWriter out = new StringWriter();
        responseWriter.write(out, req, rsp);
        return out.toString();
    } finally {
        if (req != null) {
            req.close();
        }
        SolrRequestInfo.clearRequestInfo();
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) ArrayList(java.util.ArrayList) ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) MapSolrParams(org.apache.solr.common.params.MapSolrParams) StringWriter(java.io.StringWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 34 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class SolrRequestParsers method parse.

public SolrQueryRequest parse(SolrCore core, String path, HttpServletRequest req) throws Exception {
    SolrRequestParser parser = standard;
    // TODO -- in the future, we could pick a different parser based on the request
    // Pick the parser from the request...
    ArrayList<ContentStream> streams = new ArrayList<>(1);
    SolrParams params = parser.parseParamsAndFillStreams(req, streams);
    SolrQueryRequest sreq = buildRequestFrom(core, params, streams, getRequestTimer(req), req);
    // Handlers and login will want to know the path. If it contains a ':'
    // the handler could use it for RESTful URLs
    sreq.getContext().put(PATH, RequestHandlers.normalize(path));
    sreq.getContext().put("httpMethod", req.getMethod());
    if (addHttpRequestToContext) {
        sreq.getContext().put("httpRequest", req);
    }
    return sreq;
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) ArrayList(java.util.ArrayList) SolrParams(org.apache.solr.common.params.SolrParams) MultiMapSolrParams(org.apache.solr.common.params.MultiMapSolrParams)

Example 35 with ContentStream

use of org.apache.solr.common.util.ContentStream in project lucene-solr by apache.

the class DocumentAnalysisRequestHandlerTest method testResolveAnalysisRequest.

/**
   * Tests the {@link DocumentAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.request.SolrQueryRequest)}
   */
@Test
public void testResolveAnalysisRequest() throws Exception {
    String docsInput = "<docs>" + "<doc>" + "<field name=\"id\">1</field>" + "<field name=\"whitetok\">The Whitetok</field>" + "<field name=\"text\">The Text</field>" + "</doc>" + "</docs>";
    final ContentStream cs = new ContentStreamBase.StringStream(docsInput);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add("analysis.query", "The Query String");
    params.add("analysis.showmatch", "true");
    SolrQueryRequest req = new SolrQueryRequestBase(h.getCore(), params) {

        @Override
        public Iterable<ContentStream> getContentStreams() {
            return Collections.singleton(cs);
        }
    };
    DocumentAnalysisRequest request = handler.resolveAnalysisRequest(req);
    assertNotNull(request);
    assertTrue(request.isShowMatch());
    assertNotNull(request.getQuery());
    assertEquals("The Query String", request.getQuery());
    List<SolrInputDocument> documents = request.getDocuments();
    assertNotNull(documents);
    assertEquals(1, documents.size());
    SolrInputDocument document = documents.get(0);
    SolrInputField field = document.getField("id");
    assertNotNull(field);
    assertEquals("1", field.getFirstValue());
    field = document.getField("whitetok");
    assertNotNull(field);
    assertEquals("The Whitetok", field.getFirstValue());
    field = document.getField("text");
    assertNotNull(field);
    assertEquals("The Text", field.getFirstValue());
    req.close();
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrInputField(org.apache.solr.common.SolrInputField) SolrQueryRequestBase(org.apache.solr.request.SolrQueryRequestBase) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocumentAnalysisRequest(org.apache.solr.client.solrj.request.DocumentAnalysisRequest) Test(org.junit.Test)

Aggregations

ContentStream (org.apache.solr.common.util.ContentStream)45 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)15 SolrException (org.apache.solr.common.SolrException)13 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)12 SolrParams (org.apache.solr.common.params.SolrParams)11 MultiMapSolrParams (org.apache.solr.common.params.MultiMapSolrParams)10 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)10 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)8 HashMap (java.util.HashMap)7 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 SolrCore (org.apache.solr.core.SolrCore)7 SolrQueryRequestBase (org.apache.solr.request.SolrQueryRequestBase)7 IOException (java.io.IOException)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 SolrInputDocument (org.apache.solr.common.SolrInputDocument)6 NamedList (org.apache.solr.common.util.NamedList)6 Reader (java.io.Reader)5 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 FormDataRequestParser (org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser)5