Search in sources :

Example 26 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TestDocumentObjectBinder method testSingleVal4Array.

public void testSingleVal4Array() {
    DocumentObjectBinder binder = new DocumentObjectBinder();
    SolrDocumentList solDocList = new SolrDocumentList();
    SolrDocument d = new SolrDocument();
    solDocList.add(d);
    d.setField("cat", "hello");
    List<Item> l = binder.getBeans(Item.class, solDocList);
    assertEquals("hello", l.get(0).categories[0]);
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) SolrDocumentList(org.apache.solr.common.SolrDocumentList)

Example 27 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList 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 28 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TestCoreAdmin method testCoreSwap.

@Test
public void testCoreSwap() throws Exception {
    // index marker docs to core0
    SolrClient cli0 = getSolrCore0();
    SolrInputDocument d = new SolrInputDocument("id", "core0-0");
    cli0.add(d);
    d = new SolrInputDocument("id", "core0-1");
    cli0.add(d);
    cli0.commit();
    // index a marker doc to core1
    SolrClient cli1 = getSolrCore1();
    d = new SolrInputDocument("id", "core1-0");
    cli1.add(d);
    cli1.commit();
    // initial state assertions
    SolrQuery q = new SolrQuery("*:*");
    QueryResponse rsp = cli0.query(q);
    SolrDocumentList docs = rsp.getResults();
    assertEquals(2, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core0-"));
    });
    rsp = cli1.query(q);
    docs = rsp.getResults();
    assertEquals(1, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core1-"));
    });
    // assert initial metrics
    SolrMetricManager metricManager = cores.getMetricManager();
    String core0RegistryName = SolrCoreMetricManager.createRegistryName(false, null, null, null, "core0");
    String core1RegistryName = SolrCoreMetricManager.createRegistryName(false, null, null, null, "core1");
    MetricRegistry core0Registry = metricManager.registry(core0RegistryName);
    MetricRegistry core1Registry = metricManager.registry(core1RegistryName);
    // 2 docs + 1 commit
    assertEquals(3, core0Registry.counter("UPDATE./update.requests").getCount());
    // 1 doc + 1 commit
    assertEquals(2, core1Registry.counter("UPDATE./update.requests").getCount());
    // swap
    CoreAdminRequest.swapCore("core0", "core1", getSolrAdmin());
    // assert state after swap
    cli0 = getSolrCore0();
    cli1 = getSolrCore1();
    rsp = cli0.query(q);
    docs = rsp.getResults();
    assertEquals(1, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core1-"));
    });
    rsp = cli1.query(q);
    docs = rsp.getResults();
    assertEquals(2, docs.size());
    docs.forEach(doc -> {
        assertTrue(doc.toString(), doc.getFieldValue("id").toString().startsWith("core0-"));
    });
    core0Registry = metricManager.registry(core0RegistryName);
    core1Registry = metricManager.registry(core1RegistryName);
    assertEquals(2, core0Registry.counter("UPDATE./update.requests").getCount());
    assertEquals(3, core1Registry.counter("UPDATE./update.requests").getCount());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrClient(org.apache.solr.client.solrj.SolrClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) MetricRegistry(com.codahale.metrics.MetricRegistry) SolrMetricManager(org.apache.solr.metrics.SolrMetricManager) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Test(org.junit.Test)

Example 29 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TestReplicationHandler method doTestIndexFetchWithMasterUrl.

@Test
public void doTestIndexFetchWithMasterUrl() throws Exception {
    //change solrconfig on slave
    //this has no entry for pollinginterval
    slave.copyConfigFile(CONF_DIR + "solrconfig-slave1.xml", "solrconfig.xml");
    slaveJetty.stop();
    slaveJetty = createJetty(slave);
    slaveClient.close();
    slaveClient = createNewSolrClient(slaveJetty.getLocalPort());
    masterClient.deleteByQuery("*:*");
    slaveClient.deleteByQuery("*:*");
    slaveClient.commit();
    nDocs--;
    for (int i = 0; i < nDocs; i++) index(masterClient, "id", i, "name", "name = " + i);
    // make sure prepareCommit doesn't mess up commit  (SOLR-3938)
    // todo: make SolrJ easier to pass arbitrary params to
    // TODO: precommit WILL screw with the rest of this test
    masterClient.commit();
    NamedList masterQueryRsp = rQuery(nDocs, "*:*", masterClient);
    SolrDocumentList masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    assertEquals(nDocs, masterQueryResult.getNumFound());
    // index fetch
    String masterUrl = buildUrl(slaveJetty.getLocalPort()) + "/" + DEFAULT_TEST_CORENAME + ReplicationHandler.PATH + "?command=fetchindex&masterUrl=";
    masterUrl += buildUrl(masterJetty.getLocalPort()) + "/" + DEFAULT_TEST_CORENAME + ReplicationHandler.PATH;
    URL url = new URL(masterUrl);
    InputStream stream = url.openStream();
    stream.close();
    //get docs from slave and check if number is equal to master
    NamedList slaveQueryRsp = rQuery(nDocs, "*:*", slaveClient);
    SolrDocumentList slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs, slaveQueryResult.getNumFound());
    //compare results
    String cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    for (int i = nDocs; i < nDocs + 3; i++) index(slaveClient, "id", i, "name", "name = " + i);
    slaveClient.commit();
    pullFromSlaveToMaster();
    rQuery(nDocs + 3, "*:*", masterClient);
    //get docs from slave and check if number is equal to master
    slaveQueryRsp = rQuery(nDocs + 3, "*:*", slaveClient);
    slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs + 3, slaveQueryResult.getNumFound());
    //compare results
    masterQueryRsp = rQuery(nDocs + 3, "*:*", masterClient);
    masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    assertVersions(masterClient, slaveClient);
    pullFromSlaveToMaster();
    //get docs from slave and check if number is equal to master
    slaveQueryRsp = rQuery(nDocs + 3, "*:*", slaveClient);
    slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs + 3, slaveQueryResult.getNumFound());
    //compare results
    masterQueryRsp = rQuery(nDocs + 3, "*:*", masterClient);
    masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    assertVersions(masterClient, slaveClient);
    // now force a new index directory
    for (int i = nDocs + 3; i < nDocs + 7; i++) index(masterClient, "id", i, "name", "name = " + i);
    masterClient.commit();
    pullFromSlaveToMaster();
    rQuery((int) slaveQueryResult.getNumFound(), "*:*", masterClient);
    //get docs from slave and check if number is equal to master
    slaveQueryRsp = rQuery(nDocs + 3, "*:*", slaveClient);
    slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs + 3, slaveQueryResult.getNumFound());
    //compare results
    masterQueryRsp = rQuery(nDocs + 3, "*:*", masterClient);
    masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    assertVersions(masterClient, slaveClient);
    pullFromSlaveToMaster();
    //get docs from slave and check if number is equal to master
    slaveQueryRsp = rQuery(nDocs + 3, "*:*", slaveClient);
    slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs + 3, slaveQueryResult.getNumFound());
    //compare results
    masterQueryRsp = rQuery(nDocs + 3, "*:*", masterClient);
    masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    assertVersions(masterClient, slaveClient);
    NamedList<Object> details = getDetails(masterClient);
    details = getDetails(slaveClient);
    checkForSingleIndex(masterJetty);
    checkForSingleIndex(slaveJetty);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) URL(java.net.URL) Test(org.junit.Test)

Example 30 with SolrDocumentList

use of org.apache.solr.common.SolrDocumentList in project lucene-solr by apache.

the class TestReplicationHandler method doTestIndexAndConfigReplication.

@Test
public void doTestIndexAndConfigReplication() throws Exception {
    clearIndexWithReplication();
    nDocs--;
    for (int i = 0; i < nDocs; i++) index(masterClient, "id", i, "name", "name = " + i);
    masterClient.commit();
    NamedList masterQueryRsp = rQuery(nDocs, "*:*", masterClient);
    SolrDocumentList masterQueryResult = (SolrDocumentList) masterQueryRsp.get("response");
    assertEquals(nDocs, numFound(masterQueryRsp));
    //get docs from slave and check if number is equal to master
    NamedList slaveQueryRsp = rQuery(nDocs, "*:*", slaveClient);
    SolrDocumentList slaveQueryResult = (SolrDocumentList) slaveQueryRsp.get("response");
    assertEquals(nDocs, numFound(slaveQueryRsp));
    //compare results
    String cmp = BaseDistributedSearchTestCase.compare(masterQueryResult, slaveQueryResult, 0, null);
    assertEquals(null, cmp);
    assertVersions(masterClient, slaveClient);
    //start config files replication test
    masterClient.deleteByQuery("*:*");
    masterClient.commit();
    //change the schema on master
    master.copyConfigFile(CONF_DIR + "schema-replication2.xml", "schema.xml");
    masterJetty.stop();
    masterJetty = createJetty(master);
    masterClient.close();
    masterClient = createNewSolrClient(masterJetty.getLocalPort());
    slave.setTestPort(masterJetty.getLocalPort());
    slave.copyConfigFile(slave.getSolrConfigFile(), "solrconfig.xml");
    slaveJetty.stop();
    // setup an xslt dir to force subdir file replication
    File masterXsltDir = new File(master.getConfDir() + File.separator + "xslt");
    File masterXsl = new File(masterXsltDir, "dummy.xsl");
    assertTrue("could not make dir " + masterXsltDir, masterXsltDir.mkdirs());
    assertTrue(masterXsl.createNewFile());
    File slaveXsltDir = new File(slave.getConfDir() + File.separator + "xslt");
    File slaveXsl = new File(slaveXsltDir, "dummy.xsl");
    assertFalse(slaveXsltDir.exists());
    slaveJetty = createJetty(slave);
    slaveClient.close();
    slaveClient = createNewSolrClient(slaveJetty.getLocalPort());
    //add a doc with new field and commit on master to trigger index fetch from slave.
    index(masterClient, "id", "2000", "name", "name = " + 2000, "newname", "newname = " + 2000);
    masterClient.commit();
    assertEquals(1, numFound(rQuery(1, "*:*", masterClient)));
    slaveQueryRsp = rQuery(1, "*:*", slaveClient);
    assertVersions(masterClient, slaveClient);
    SolrDocument d = ((SolrDocumentList) slaveQueryRsp.get("response")).get(0);
    assertEquals("newname = 2000", (String) d.getFieldValue("newname"));
    assertTrue(slaveXsltDir.isDirectory());
    assertTrue(slaveXsl.exists());
    checkForSingleIndex(masterJetty);
    checkForSingleIndex(slaveJetty, true);
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) NamedList(org.apache.solr.common.util.NamedList) SolrDocumentList(org.apache.solr.common.SolrDocumentList) File(java.io.File) Test(org.junit.Test)

Aggregations

SolrDocumentList (org.apache.solr.common.SolrDocumentList)263 SolrDocument (org.apache.solr.common.SolrDocument)153 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)118 SolrQuery (org.apache.solr.client.solrj.SolrQuery)83 Test (org.junit.Test)73 ArrayList (java.util.ArrayList)61 SolrServerException (org.apache.solr.client.solrj.SolrServerException)50 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)44 NamedList (org.apache.solr.common.util.NamedList)44 IOException (java.io.IOException)43 SolrParams (org.apache.solr.common.params.SolrParams)32 SolrInputDocument (org.apache.solr.common.SolrInputDocument)26 HashMap (java.util.HashMap)22 Map (java.util.Map)21 SolrClient (org.apache.solr.client.solrj.SolrClient)17 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)17 SolrException (org.apache.solr.common.SolrException)16 List (java.util.List)15 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)14 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)14