Search in sources :

Example 1 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.

the class UpdateRequestHandler method createDefaultLoaders.

protected Map<String, ContentStreamLoader> createDefaultLoaders(NamedList args) {
    SolrParams p = null;
    if (args != null) {
        p = SolrParams.toSolrParams(args);
    }
    Map<String, ContentStreamLoader> registry = new HashMap<>();
    registry.put("application/xml", new XMLLoader().init(p));
    registry.put("application/json", new JsonLoader().init(p));
    registry.put("application/csv", new CSVLoader().init(p));
    registry.put("application/javabin", new JavabinLoader().init(p));
    registry.put("text/csv", registry.get("application/csv"));
    registry.put("text/xml", registry.get("application/xml"));
    registry.put("text/json", registry.get("application/json"));
    pathVsLoaders.put(JSON_PATH, registry.get("application/json"));
    pathVsLoaders.put(DOC_PATH, registry.get("application/json"));
    pathVsLoaders.put(CSV_PATH, registry.get("application/csv"));
    pathVsLoaders.put(BIN_PATH, registry.get("application/javabin"));
    return registry;
}
Also used : ContentStreamLoader(org.apache.solr.handler.loader.ContentStreamLoader) HashMap(java.util.HashMap) CSVLoader(org.apache.solr.handler.loader.CSVLoader) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams) JsonLoader(org.apache.solr.handler.loader.JsonLoader) JavabinLoader(org.apache.solr.handler.loader.JavabinLoader) XMLLoader(org.apache.solr.handler.loader.XMLLoader)

Example 2 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.

the class JsonLoaderTest method testSimpleFormat.

public void testSimpleFormat() throws Exception {
    String str = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
    SolrQueryRequest req = req("commitWithin", "100", "overwrite", "false");
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    assertEquals(2, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("id");
    assertEquals("1", f.getValue());
    assertEquals(add.commitWithin, 100);
    assertEquals(add.overwrite, false);
    add = p.addCommands.get(1);
    d = add.solrDoc;
    f = d.getField("id");
    assertEquals("2", f.getValue());
    assertEquals(add.commitWithin, 100);
    assertEquals(add.overwrite, false);
    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)

Example 3 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.

the class JsonLoaderTest method testExtendedFieldValues.

public void testExtendedFieldValues() throws Exception {
    String str = "[{'id':'1', 'val_s':{'add':'foo'}}]".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);
    assertEquals(add.commitWithin, -1);
    assertEquals(add.overwrite, true);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("id");
    assertEquals("1", f.getValue());
    f = d.getField("val_s");
    Map<String, Object> map = (Map<String, Object>) f.getValue();
    assertEquals("foo", map.get("add"));
    req.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) Map(java.util.Map) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 4 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.

the class JsonLoaderTest method testGrandChildDocs.

@Test
public void testGrandChildDocs() throws Exception {
    String str = "{\n" + "    \"add\": {\n" + "        \"doc\": {\n" + "            \"id\": \"1\",\n" + "            \"_childDocuments_\": [\n" + "                {\n" + "                    \"id\": \"2\",\n" + "                    \"_childDocuments_\": [\n" + "                        {\n" + "                           \"id\": \"4\",\n" + "                           \"foo_s\": \"Baz\"\n" + "                        }\n" + "                    ],\n" + "                    \"foo_s\": \"Yaz\"\n" + "                },\n" + "                {\n" + "                    \"id\": \"3\",\n" + "                    \"foo_s\": \"Bar\"\n" + "                }\n" + "            ]\n" + "        }\n" + "    }\n" + "}";
    SolrQueryRequest req = req("commit", "true");
    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 one = add.solrDoc;
    assertEquals("1", one.getFieldValue("id"));
    SolrInputDocument two = one.getChildDocuments().get(0);
    assertEquals("2", two.getFieldValue("id"));
    assertEquals("Yaz", two.getFieldValue("foo_s"));
    SolrInputDocument four = two.getChildDocuments().get(0);
    assertEquals("4", four.getFieldValue("id"));
    assertEquals("Baz", four.getFieldValue("foo_s"));
    SolrInputDocument three = one.getChildDocuments().get(1);
    assertEquals("3", three.getFieldValue("id"));
    assertEquals("Bar", three.getFieldValue("foo_s"));
    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) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase) Test(org.junit.Test)

Example 5 with JsonLoader

use of org.apache.solr.handler.loader.JsonLoader in project lucene-solr by apache.

the class JsonLoaderTest method testParsing.

public void testParsing() 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(input), p);
    assertEquals(2, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("boosted");
    assertEquals(2, f.getValues().size());
    // 
    add = p.addCommands.get(1);
    d = add.solrDoc;
    f = d.getField("f1");
    assertEquals(2, f.getValues().size());
    assertEquals(false, add.overwrite);
    assertEquals(0, d.getField("f2").getValueCount());
    // parse the commit commands
    assertEquals(2, p.commitCommands.size());
    CommitUpdateCommand commit = p.commitCommands.get(0);
    assertFalse(commit.optimize);
    assertTrue(commit.waitSearcher);
    assertTrue(commit.openSearcher);
    commit = p.commitCommands.get(1);
    assertTrue(commit.optimize);
    assertFalse(commit.waitSearcher);
    assertFalse(commit.openSearcher);
    // DELETE COMMANDS
    assertEquals(4, p.deleteCommands.size());
    DeleteUpdateCommand delete = p.deleteCommands.get(0);
    assertEquals(delete.id, "ID");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(1);
    assertEquals(delete.id, "ID");
    assertEquals(delete.query, null);
    assertEquals(delete.commitWithin, 500);
    delete = p.deleteCommands.get(2);
    assertEquals(delete.id, null);
    assertEquals(delete.query, "QUERY");
    assertEquals(delete.commitWithin, -1);
    delete = p.deleteCommands.get(3);
    assertEquals(delete.id, null);
    assertEquals(delete.query, "QUERY");
    assertEquals(delete.commitWithin, 500);
    // ROLLBACK COMMANDS
    assertEquals(1, p.rollbackCommands.size());
    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) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) CommitUpdateCommand(org.apache.solr.update.CommitUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) 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