Search in sources :

Example 16 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader 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 17 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader 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 18 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader 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 19 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader 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

JsonLoader (org.apache.solr.handler.loader.JsonLoader)19 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)17 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)17 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)17 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)16 SolrInputDocument (org.apache.solr.common.SolrInputDocument)15 AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)12 SolrInputField (org.apache.solr.common.SolrInputField)11 Test (org.junit.Test)9 Map (java.util.Map)2 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)2 CSVLoader (org.apache.solr.handler.loader.CSVLoader)2 JavabinLoader (org.apache.solr.handler.loader.JavabinLoader)2 XMLLoader (org.apache.solr.handler.loader.XMLLoader)2 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)2 HashMap (java.util.HashMap)1 UnaryOperator (java.util.function.UnaryOperator)1 SolrException (org.apache.solr.common.SolrException)1 MapSolrParams (org.apache.solr.common.params.MapSolrParams)1 SolrParams (org.apache.solr.common.params.SolrParams)1