Search in sources :

Example 1 with XMLResponseParser

use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.

the class SolrEntityProcessor method firstInit.

@Override
protected void firstInit(Context context) {
    super.firstInit(context);
    try {
        String serverPath = context.getResolvedEntityAttribute(SOLR_SERVER);
        if (serverPath == null) {
            throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "SolrEntityProcessor: parameter 'url' is required");
        }
        HttpClient client = getHttpClient();
        URL url = new URL(serverPath);
        // (wt="javabin|xml") default is javabin
        if ("xml".equals(context.getResolvedEntityAttribute(CommonParams.WT))) {
            // TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
            solrClient = new Builder(url.toExternalForm()).withHttpClient(client).withResponseParser(new XMLResponseParser()).build();
            LOG.info("using XMLResponseParser");
        } else {
            // TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
            solrClient = new Builder(url.toExternalForm()).withHttpClient(client).build();
            LOG.info("using BinaryResponseParser");
        }
    } catch (MalformedURLException e) {
        throw new DataImportHandlerException(DataImportHandlerException.SEVERE, e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpClient(org.apache.http.client.HttpClient) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) URL(java.net.URL)

Example 2 with XMLResponseParser

use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.

the class TestDocumentObjectBinder method testSimple.

public void testSimple() throws Exception {
    DocumentObjectBinder binder = new DocumentObjectBinder();
    XMLResponseParser parser = new XMLResponseParser();
    NamedList<Object> nl = parser.processResponse(new StringReader(xml));
    QueryResponse res = new QueryResponse(nl, null);
    SolrDocumentList solDocList = res.getResults();
    List<Item> l = binder.getBeans(Item.class, res.getResults());
    assertEquals(solDocList.size(), l.size());
    assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
    Item item = new Item();
    item.id = "aaa";
    item.categories = new String[] { "aaa", "bbb", "ccc" };
    SolrInputDocument out = binder.toSolrInputDocument(item);
    assertEquals(item.id, out.getFieldValue("id"));
    SolrInputField catfield = out.getField("cat");
    assertEquals(3, catfield.getValueCount());
    List<String> catValues = (List<String>) catfield.getValue();
    assertEquals("aaa", catValues.get(0));
    assertEquals("bbb", catValues.get(1));
    assertEquals("ccc", catValues.get(2));
}
Also used : SolrInputField(org.apache.solr.common.SolrInputField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) StringReader(java.io.StringReader) SolrDocumentList(org.apache.solr.common.SolrDocumentList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser)

Example 3 with XMLResponseParser

use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.

the class PropertiesRequestHandlerTest method readProperties.

private NamedList<NamedList<NamedList<Object>>> readProperties() throws Exception {
    String xml = h.query(req(CommonParams.QT, "/admin/properties", CommonParams.WT, "xml"));
    XMLResponseParser parser = new XMLResponseParser();
    return (NamedList<NamedList<NamedList<Object>>>) parser.processResponse(new StringReader(xml)).get("system.properties");
}
Also used : NamedList(org.apache.solr.common.util.NamedList) StringReader(java.io.StringReader) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser)

Example 4 with XMLResponseParser

use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.

the class SolrExampleTests method testUnicode.

public void testUnicode() throws Exception {
    Random random = random();
    int numIterations = atLeast(3);
    SolrClient client = getSolrClient();
    // save the old parser, so we can set it back.
    ResponseParser oldParser = null;
    if (client instanceof HttpSolrClient) {
        HttpSolrClient httpSolrClient = (HttpSolrClient) client;
        oldParser = httpSolrClient.getParser();
    }
    try {
        for (int iteration = 0; iteration < numIterations; iteration++) {
            // choose format
            if (client instanceof HttpSolrClient) {
                if (random.nextBoolean()) {
                    ((HttpSolrClient) client).setParser(new BinaryResponseParser());
                } else {
                    ((HttpSolrClient) client).setParser(new XMLResponseParser());
                }
            }
            int numDocs = TestUtil.nextInt(random(), 1, 10 * RANDOM_MULTIPLIER);
            // Empty the database...
            // delete everything!
            client.deleteByQuery("*:*");
            List<SolrInputDocument> docs = new ArrayList<>();
            for (int i = 0; i < numDocs; i++) {
                // Now add something...
                SolrInputDocument doc = new SolrInputDocument();
                doc.addField("id", "" + i);
                doc.addField("unicode_s", randomTestString(30));
                docs.add(doc);
            }
            client.add(docs);
            client.commit();
            SolrQuery query = new SolrQuery();
            query.setQuery("*:*");
            query.setRows(numDocs);
            QueryResponse rsp = client.query(query);
            for (int i = 0; i < numDocs; i++) {
                String expected = (String) docs.get(i).getFieldValue("unicode_s");
                String actual = (String) rsp.getResults().get(i).getFieldValue("unicode_s");
                assertEquals(expected, actual);
            }
        }
    } finally {
        if (oldParser != null) {
            // set the old parser back
            ((HttpSolrClient) client).setParser(oldParser);
        }
    }
}
Also used : NoOpResponseParser(org.apache.solr.client.solrj.impl.NoOpResponseParser) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) ArrayList(java.util.ArrayList) StringContains.containsString(org.junit.internal.matchers.StringContains.containsString) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Random(java.util.Random) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) ErrorTrackingConcurrentUpdateSolrClient(org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser)

Example 5 with XMLResponseParser

use of org.apache.solr.client.solrj.impl.XMLResponseParser in project lucene-solr by apache.

the class QueryResponseTest method testSimpleGroupResponse.

@Test
public void testSimpleGroupResponse() throws Exception {
    XMLResponseParser parser = new XMLResponseParser();
    InputStream is = new SolrResourceLoader().openResource("solrj/sampleSimpleGroupResponse.xml");
    assertNotNull(is);
    Reader in = new InputStreamReader(is, StandardCharsets.UTF_8);
    NamedList<Object> response = parser.processResponse(in);
    in.close();
    QueryResponse qr = new QueryResponse(response, null);
    assertNotNull(qr);
    GroupResponse groupResponse = qr.getGroupResponse();
    assertNotNull(groupResponse);
    List<GroupCommand> commands = groupResponse.getValues();
    assertNotNull(commands);
    assertEquals(1, commands.size());
    GroupCommand fieldCommand = commands.get(0);
    assertEquals("acco_id", fieldCommand.getName());
    assertEquals(30000000, fieldCommand.getMatches());
    assertEquals(5687, fieldCommand.getNGroups().intValue());
    List<Group> fieldCommandGroups = fieldCommand.getValues();
    assertEquals(1, fieldCommandGroups.size());
    assertEquals("acco_id", fieldCommandGroups.get(0).getGroupValue());
    SolrDocumentList documents = fieldCommandGroups.get(0).getResult();
    assertNotNull(documents);
    assertEquals(10, documents.size());
    assertEquals("116_AR", documents.get(0).getFieldValue("acco_id"));
    assertEquals("116_HI", documents.get(1).getFieldValue("acco_id"));
    assertEquals("953_AR", documents.get(2).getFieldValue("acco_id"));
    assertEquals("953_HI", documents.get(3).getFieldValue("acco_id"));
    assertEquals("954_AR", documents.get(4).getFieldValue("acco_id"));
    assertEquals("954_HI", documents.get(5).getFieldValue("acco_id"));
    assertEquals("546_AR", documents.get(6).getFieldValue("acco_id"));
    assertEquals("546_HI", documents.get(7).getFieldValue("acco_id"));
    assertEquals("708_AR", documents.get(8).getFieldValue("acco_id"));
    assertEquals("708_HI", documents.get(9).getFieldValue("acco_id"));
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) Test(org.junit.Test)

Aggregations

XMLResponseParser (org.apache.solr.client.solrj.impl.XMLResponseParser)18 Test (org.junit.Test)6 InputStream (java.io.InputStream)5 InputStreamReader (java.io.InputStreamReader)5 Reader (java.io.Reader)5 SolrResourceLoader (org.apache.solr.core.SolrResourceLoader)5 StringReader (java.io.StringReader)4 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)4 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)4 BinaryResponseParser (org.apache.solr.client.solrj.impl.BinaryResponseParser)3 SolrInputDocument (org.apache.solr.common.SolrInputDocument)3 NamedList (org.apache.solr.common.util.NamedList)3 List (java.util.List)2 ResponseParser (org.apache.solr.client.solrj.ResponseParser)2 ErrorTrackingConcurrentUpdateSolrClient (org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient)2 NoOpResponseParser (org.apache.solr.client.solrj.impl.NoOpResponseParser)2 RequestWriter (org.apache.solr.client.solrj.request.RequestWriter)2 SolrDocument (org.apache.solr.common.SolrDocument)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1