Search in sources :

Example 1 with RequestWriter

use of org.apache.solr.client.solrj.request.RequestWriter in project lucene-solr by apache.

the class AddBlockUpdateTest method testXML.

//This is the same as testSolrJXML above but uses the XMLLoader
// to illustrate the structure of the XML documents
@Test
public void testXML() throws IOException, XMLStreamException {
    UpdateRequest req = new UpdateRequest();
    List<SolrInputDocument> docs = new ArrayList<>();
    String xml_doc1 = "<doc >" + "  <field name=\"id\">1</field>" + "  <field name=\"parent_s\">X</field>" + "<doc>  " + "  <field name=\"id\" >2</field>" + "  <field name=\"child_s\">y</field>" + "</doc>" + "<doc>  " + "  <field name=\"id\" >3</field>" + "  <field name=\"child_s\">z</field>" + "</doc>" + "</doc>";
    String xml_doc2 = "<doc >" + "  <field name=\"id\">4</field>" + "  <field name=\"parent_s\">A</field>" + "<doc>  " + "  <field name=\"id\" >5</field>" + "  <field name=\"child_s\">b</field>" + "</doc>" + "<doc>  " + "  <field name=\"id\" >6</field>" + "  <field name=\"child_s\">c</field>" + "</doc>" + "</doc>";
    XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(xml_doc1));
    // read the START document...
    parser.next();
    //null for the processor is all right here
    XMLLoader loader = new XMLLoader();
    SolrInputDocument document1 = loader.readDoc(parser);
    XMLStreamReader parser2 = inputFactory.createXMLStreamReader(new StringReader(xml_doc2));
    // read the START document...
    parser2.next();
    //null for the processor is all right here
    //XMLLoader loader = new XMLLoader();
    SolrInputDocument document2 = loader.readDoc(parser2);
    docs.add(document1);
    docs.add(document2);
    Collections.shuffle(docs, random());
    req.add(docs);
    RequestWriter requestWriter = new RequestWriter();
    OutputStream os = new ByteArrayOutputStream();
    requestWriter.write(req, os);
    assertBlockU(os.toString());
    assertU(commit());
    final SolrIndexSearcher searcher = getSearcher();
    assertSingleParentOf(searcher, one("yz"), "X");
    assertSingleParentOf(searcher, one("bc"), "A");
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) XMLStreamReader(javax.xml.stream.XMLStreamReader) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) XMLLoader(org.apache.solr.handler.loader.XMLLoader) Test(org.junit.Test)

Example 2 with RequestWriter

use of org.apache.solr.client.solrj.request.RequestWriter in project lucene-solr by apache.

the class AddBlockUpdateTest method testSolrJXML.

@SuppressWarnings("serial")
@Test
public void testSolrJXML() throws IOException {
    UpdateRequest req = new UpdateRequest();
    List<SolrInputDocument> docs = new ArrayList<>();
    SolrInputDocument document1 = new SolrInputDocument() {

        {
            final String id = id();
            addField("id", id);
            addField("parent_s", "X");
            ArrayList<SolrInputDocument> ch1 = new ArrayList<>(Arrays.asList(new SolrInputDocument() {

                {
                    addField("id", id());
                    addField("child_s", "y");
                }
            }, new SolrInputDocument() {

                {
                    addField("id", id());
                    addField("child_s", "z");
                }
            }));
            Collections.shuffle(ch1, random());
            addChildDocuments(ch1);
        }
    };
    SolrInputDocument document2 = new SolrInputDocument() {

        {
            final String id = id();
            addField("id", id);
            addField("parent_s", "A");
            addChildDocument(new SolrInputDocument() {

                {
                    addField("id", id());
                    addField("child_s", "b");
                }
            });
            addChildDocument(new SolrInputDocument() {

                {
                    addField("id", id());
                    addField("child_s", "c");
                }
            });
        }
    };
    docs.add(document1);
    docs.add(document2);
    Collections.shuffle(docs, random());
    req.add(docs);
    RequestWriter requestWriter = new RequestWriter();
    OutputStream os = new ByteArrayOutputStream();
    requestWriter.write(req, os);
    assertBlockU(os.toString());
    assertU(commit());
    final SolrIndexSearcher searcher = getSearcher();
    assertSingleParentOf(searcher, one("yz"), "X");
    assertSingleParentOf(searcher, one("bc"), "A");
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ArrayList(java.util.ArrayList) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Test(org.junit.Test)

Example 3 with RequestWriter

use of org.apache.solr.client.solrj.request.RequestWriter in project lucene-solr by apache.

the class BasicHttpSolrClientTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    DebugServlet.clear();
    try (HttpSolrClient client = getHttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
        UpdateRequest req = new UpdateRequest();
        req.add(new SolrInputDocument());
        req.setParam("a", "ሴ");
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        //default method
        assertEquals("post", DebugServlet.lastMethod);
        //agent
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        //default wt
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        //default version
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        //content type
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        //parameter encoding
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //XML response and writer
        client.setParser(new XMLResponseParser());
        client.setRequestWriter(new RequestWriter());
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("xml", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/xml; charset=UTF-8", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
        //javabin request
        client.setParser(new BinaryResponseParser());
        client.setRequestWriter(new BinaryRequestWriter());
        DebugServlet.clear();
        try {
            client.request(req);
        } catch (ParseException ignored) {
        }
        assertEquals("post", DebugServlet.lastMethod);
        assertEquals("Solr[" + HttpSolrClient.class.getName() + "] 1.0", DebugServlet.headers.get("User-Agent"));
        assertEquals(1, DebugServlet.parameters.get(CommonParams.WT).length);
        assertEquals("javabin", DebugServlet.parameters.get(CommonParams.WT)[0]);
        assertEquals(1, DebugServlet.parameters.get(CommonParams.VERSION).length);
        assertEquals(client.getParser().getVersion(), DebugServlet.parameters.get(CommonParams.VERSION)[0]);
        assertEquals("application/javabin", DebugServlet.headers.get("Content-Type"));
        assertEquals(1, DebugServlet.parameters.get("a").length);
        assertEquals("ሴ", DebugServlet.parameters.get("a")[0]);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ParseException(org.apache.http.ParseException) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) Test(org.junit.Test)

Example 4 with RequestWriter

use of org.apache.solr.client.solrj.request.RequestWriter in project lucene-solr by apache.

the class TestSolrJErrorHandling method testWithXml.

@Test
public void testWithXml() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.setRequestWriter(new RequestWriter());
    // delete everything!
    client.deleteByQuery("*:*");
    doIt(client);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) Test(org.junit.Test)

Example 5 with RequestWriter

use of org.apache.solr.client.solrj.request.RequestWriter in project lucene-solr by apache.

the class TestBatchUpdate method testWithXml.

@Test
public void testWithXml() throws Exception {
    HttpSolrClient client = (HttpSolrClient) getSolrClient();
    client.setRequestWriter(new RequestWriter());
    // delete everything!
    client.deleteByQuery("*:*");
    doIt(client);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) BinaryRequestWriter(org.apache.solr.client.solrj.impl.BinaryRequestWriter) Test(org.junit.Test)

Aggregations

RequestWriter (org.apache.solr.client.solrj.request.RequestWriter)7 Test (org.junit.Test)5 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)3 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)2 BinaryRequestWriter (org.apache.solr.client.solrj.impl.BinaryRequestWriter)2 XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)2 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)2 StringReader (java.io.StringReader)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 ParseException (org.apache.http.ParseException)1 ConcurrentUpdateSolrClient (org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient)1 XMLLoader (org.apache.solr.handler.loader.XMLLoader)1