Search in sources :

Example 21 with BufferingRequestProcessor

use of org.apache.solr.update.processor.BufferingRequestProcessor in project lucene-solr by apache.

the class JsonLoaderTest method testInvalidJsonProducesBadRequestSolrException.

@Test
public void testInvalidJsonProducesBadRequestSolrException() throws Exception {
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    String invalidJsonString = "}{";
    try (SolrQueryRequest req = req()) {
        try {
            loader.load(req, rsp, new ContentStreamBase.StringStream(invalidJsonString), p);
            fail("Expected invalid JSON to produce a SolrException.");
        } catch (SolrException expectedException) {
            assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, expectedException.code());
            assertTrue(expectedException.getMessage().contains("Cannot parse"));
            assertTrue(expectedException.getMessage().contains("JSON"));
        }
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) JsonLoader(org.apache.solr.handler.loader.JsonLoader) SolrException(org.apache.solr.common.SolrException) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 22 with BufferingRequestProcessor

use of org.apache.solr.update.processor.BufferingRequestProcessor in project lucene-solr by apache.

the class JsonLoaderTest method testBigDecimalValuesInAdd.

@Test
public void testBigDecimalValuesInAdd() throws Exception {
    String str = ("{'add':[{'id':'1','bd1':0.12345678901234567890123456789012345," + "'bd2':12345678901234567890.12345678901234567890,'bd3':0.012345678901234567890123456789012345," + "'bd3':123456789012345678900.012345678901234567890}]}").replace('\'', '"');
    SolrQueryRequest req = req();
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    assertEquals(1, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("bd1");
    assertTrue(f.getValue() instanceof String);
    assertEquals("0.12345678901234567890123456789012345", f.getValue());
    f = d.getField("bd2");
    assertTrue(f.getValue() instanceof String);
    assertEquals("12345678901234567890.12345678901234567890", f.getValue());
    f = d.getField("bd3");
    assertEquals(2, ((List) f.getValue()).size());
    assertTrue(((List) f.getValue()).contains("0.012345678901234567890123456789012345"));
    assertTrue(((List) f.getValue()).contains("123456789012345678900.012345678901234567890"));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 23 with BufferingRequestProcessor

use of org.apache.solr.update.processor.BufferingRequestProcessor in project lucene-solr by apache.

the class JsonLoaderTest method testFewParentsJsonDoc.

public void testFewParentsJsonDoc() throws Exception {
    String json = PARENT_TWO_CHILDREN_JSON;
    SolrQueryRequest req;
    SolrQueryResponse rsp;
    BufferingRequestProcessor p;
    JsonLoader loader;
    {
        //multichild test case
        final boolean array = random().nextBoolean();
        StringBuilder b = new StringBuilder();
        if (array) {
            b.append("[");
        }
        final int passes = atLeast(2);
        for (int i = 1; i <= passes; i++) {
            b.append(json.replace("1", "" + i));
            if (array) {
                b.append(i < passes ? "," : "]");
            }
        }
        req = req(PARENT_TWO_CHILDREN_PARAMS);
        req.getContext().put("path", "/update/json/docs");
        rsp = new SolrQueryResponse();
        p = new BufferingRequestProcessor(null);
        loader = new JsonLoader();
        loader.load(req, rsp, new ContentStreamBase.StringStream(b.toString()), p);
        for (int i = 1; i <= passes; i++) {
            final int ii = i;
            UnaryOperator<String> s = (v) -> v.replace("1", "" + ii);
            final SolrInputDocument parent = p.addCommands.get(i - 1).solrDoc;
            assertOnlyValue(s.apply("1"), parent, "id");
            assertOnlyValue("i am the parent", parent, "name");
            assertOnlyValue("parent", parent, "cat");
            assertEquals(2, parent.getChildDocuments().size());
            {
                final SolrInputDocument child1 = parent.getChildDocuments().get(0);
                assertOnlyValue(s.apply("1.1"), child1, "id");
                assertOnlyValue(s.apply("i am the 1st child"), child1, "name");
                assertOnlyValue("child", child1, "cat");
            }
            {
                final SolrInputDocument child2 = parent.getChildDocuments().get(1);
                assertOnlyValue(s.apply("1.2"), child2, "id");
                assertOnlyValue("i am the 2nd child", child2, "name");
                assertOnlyValue("child", child2, "cat");
                assertEquals(1, child2.getChildDocuments().size());
                final SolrInputDocument grandChild = child2.getChildDocuments().get(0);
                assertOnlyValue(s.apply("1.2.1"), grandChild, "id");
                assertOnlyValue("i am the grandchild", grandChild, "name");
                assertOnlyValue("grandchild", grandChild, "cat");
            }
        }
    }
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) JsonLoader(org.apache.solr.handler.loader.JsonLoader) UnaryOperator(java.util.function.UnaryOperator)

Example 24 with BufferingRequestProcessor

use of org.apache.solr.update.processor.BufferingRequestProcessor in project lucene-solr by apache.

the class JsonLoaderTest method checkFieldValueOrdering.

private void checkFieldValueOrdering(String rawJson, float fBoost) throws Exception {
    SolrQueryRequest req = req();
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(rawJson), p);
    assertEquals(2, p.addCommands.size());
    SolrInputDocument d = p.addCommands.get(0).solrDoc;
    assertEquals(2, d.getFieldNames().size());
    assertEquals("1", d.getFieldValue("id"));
    assertEquals(new Object[] { 45L, 67L, 89L }, d.getFieldValues("f").toArray());
    d = p.addCommands.get(1).solrDoc;
    assertEquals(1, d.getFieldNames().size());
    assertEquals("2", d.getFieldValue("id"));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) JsonLoader(org.apache.solr.handler.loader.JsonLoader) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Aggregations

SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)24 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)24 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)24 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)21 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)19 SolrInputDocument (org.apache.solr.common.SolrInputDocument)17 JsonLoader (org.apache.solr.handler.loader.JsonLoader)17 Test (org.junit.Test)15 SolrInputField (org.apache.solr.common.SolrInputField)11 XMLLoader (org.apache.solr.handler.loader.XMLLoader)3 Map (java.util.Map)2 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)2 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)2 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 UnaryOperator (java.util.function.UnaryOperator)1 BinaryRequestWriter (org.apache.solr.client.solrj.impl.BinaryRequestWriter)1 JavaBinUpdateRequestCodec (org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec)1 SolrException (org.apache.solr.common.SolrException)1