Java Code Examples for org.apache.solr.client.solrj.request.UpdateRequest#setAction()

The following examples show how to use org.apache.solr.client.solrj.request.UpdateRequest#setAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SolrExampleTests.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testChineseDefaults() throws Exception {
  SolrClient client = getSolrClient();
  // Empty the database...
  client.deleteByQuery("*:*");// delete everything!
  client.commit();
  assertNumFound( "*:*", 0 ); // make sure it got in

  // Beijing medical University
  UpdateRequest req = new UpdateRequest();
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "42");
  doc.addField("text", "北京医科大学");
  req.add(doc);

  req.setAction(ACTION.COMMIT, true, true );
  req.process( client );

  // Beijing university should match:
  SolrQuery query = new SolrQuery("北京大学");
  QueryResponse rsp = client.query( query );
  assertEquals(1, rsp.getResults().getNumFound());
}
 
Example 2
Source File: CdcrTestsUtil.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public static void indexRandomDocs(Integer start, Integer count, CloudSolrClient solrClient) throws Exception {
  // ADD operation on cluster 1
  int docs = 0;
  if (count == 0) {
    docs = (TEST_NIGHTLY ? 100 : 10);
  } else {
    docs = count;
  }
  for (int k = start; k < docs; k++) {
    UpdateRequest req = new UpdateRequest();
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", k);
    req.add(doc);

    req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    req.process(solrClient);
  }
}
 
Example 3
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private int indexDocs(CloudSolrClient sourceSolrClient, String collection, int batches) throws IOException, SolrServerException {
  sourceSolrClient.setDefaultCollection(collection);
  int numDocs = 0;
  for (int k = 0; k < batches; k++) {
    UpdateRequest req = new UpdateRequest();
    for (; numDocs < (k + 1) * 100; numDocs++) {
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("id", "source_" + numDocs);
      doc.addField("xyz", numDocs);
      req.add(doc);
    }
    req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    req.process(sourceSolrClient);
  }
  log.info("Adding numDocs={}", numDocs);
  return numDocs;
}
 
Example 4
Source File: SolrExampleStreamingTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWaitOptions() throws Exception {
  // SOLR-3903
  final List<Throwable> failures = new ArrayList<>();
  final String serverUrl = jetty.getBaseUrl().toString() + "/collection1";
  try (ConcurrentUpdateSolrClient concurrentClient = new FailureRecordingConcurrentUpdateSolrClient.Builder(serverUrl)
      .withQueueSize(2)
      .withThreadCount(2)
      .build()) {
    int docId = 42;
    for (UpdateRequest.ACTION action : EnumSet.allOf(UpdateRequest.ACTION.class)) {
      for (boolean waitSearch : Arrays.asList(true, false)) {
        for (boolean waitFlush : Arrays.asList(true, false)) {
          UpdateRequest updateRequest = new UpdateRequest();
          SolrInputDocument document = new SolrInputDocument();
          document.addField("id", docId++);
          updateRequest.add(document);
          updateRequest.setAction(action, waitSearch, waitFlush);
          concurrentClient.request(updateRequest);
        }
      }
    }
    concurrentClient.commit();
    concurrentClient.blockUntilFinished();
  }

  if (0 != failures.size()) {
    assertEquals(failures.size() + " Unexpected Exception, starting with...", 
                 null, failures.get(0));
  }
}
 
Example 5
Source File: Solr5Index.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
private UpdateRequest newUpdateRequest() {
    UpdateRequest req = new UpdateRequest();
    req.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    if (waitSearcher) {
        req.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    }
    return req;
}
 
Example 6
Source File: SolrIndex.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
private UpdateRequest newUpdateRequest() {
    UpdateRequest req = new UpdateRequest();
    if(waitSearcher) {
        req.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    }
    return req;
}
 
Example 7
Source File: SolrIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Forces a commit of the given collection to make recently inserted documents available for
 * search.
 *
 * @return The number of docs in the index
 */
static long commitAndGetCurrentNumDocs(String collection, AuthorizedSolrClient client)
    throws IOException {
  SolrQuery solrQuery = new SolrQuery("*:*");
  solrQuery.setRows(0);
  try {
    UpdateRequest update = new UpdateRequest();
    update.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    client.process(collection, update);

    return client.query(collection, new SolrQuery("*:*")).getResults().getNumFound();
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 8
Source File: SolrIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Clear given collection. */
static void clearCollection(String collection, AuthorizedSolrClient client) throws IOException {
  try {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    updateRequest.deleteByQuery("*:*");
    client.process(collection, updateRequest);
  } catch (SolrServerException e) {
    throw new IOException(e);
  }
}
 
Example 9
Source File: SolrIOTestUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Inserts the given number of test documents into Solr. */
static void insertTestDocuments(String collection, long numDocs, AuthorizedSolrClient client)
    throws IOException {
  List<SolrInputDocument> data = createDocuments(numDocs);
  try {
    UpdateRequest updateRequest = new UpdateRequest();
    updateRequest.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    updateRequest.add(data);
    client.process(collection, updateRequest);
  } catch (SolrServerException e) {
    throw new IOException("Failed to insert test documents to collection", e);
  }
}
 
Example 10
Source File: TestSolrCloudWithDelegationTokens.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private SolrRequest getUpdateRequest(boolean commit) {
  UpdateRequest request = new UpdateRequest();
  if (commit) {
    request.setAction(ACTION.COMMIT, false, false);
  }
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", "dummy_id");
  request.add(doc);
  return request;
}
 
Example 11
Source File: OverseerCollectionMessageHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
static UpdateResponse softCommit(String url) throws SolrServerException, IOException {

    try (HttpSolrClient client = new HttpSolrClient.Builder(url)
        .withConnectionTimeout(30000)
        .withSocketTimeout(120000)
        .build()) {
      UpdateRequest ureq = new UpdateRequest();
      ureq.setParams(new ModifiableSolrParams());
      ureq.setAction(AbstractUpdateRequest.ACTION.COMMIT, false, true, true);
      return ureq.process(client);
    }
  }
 
Example 12
Source File: SolrExampleStreamingHttp2Test.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testWaitOptions() throws Exception {
  // SOLR-3903
  final List<Throwable> failures = new ArrayList<>();
  final String serverUrl = jetty.getBaseUrl().toString() + "/collection1";
  try (Http2SolrClient http2Client = new Http2SolrClient.Builder().build();
       ConcurrentUpdateHttp2SolrClient concurrentClient = new FailureRecordingConcurrentUpdateSolrClient.Builder(serverUrl, http2Client)
           .withQueueSize(2)
           .withThreadCount(2)
           .build()) {
    int docId = 42;
    for (UpdateRequest.ACTION action : EnumSet.allOf(UpdateRequest.ACTION.class)) {
      for (boolean waitSearch : Arrays.asList(true, false)) {
        for (boolean waitFlush : Arrays.asList(true, false)) {
          UpdateRequest updateRequest = new UpdateRequest();
          SolrInputDocument document = new SolrInputDocument();
          document.addField("id", docId++);
          updateRequest.add(document);
          updateRequest.setAction(action, waitSearch, waitFlush);
          concurrentClient.request(updateRequest);
        }
      }
    }
    concurrentClient.commit();
    concurrentClient.blockUntilFinished();
  }

  if (0 != failures.size()) {
    assertEquals(failures.size() + " Unexpected Exception, starting with...",
        null, failures.get(0));
  }
}
 
Example 13
Source File: Solr6Index.java    From atlas with Apache License 2.0 5 votes vote down vote up
private UpdateRequest newUpdateRequest() {
    final UpdateRequest req = new UpdateRequest();
    if(waitSearcher) {
        req.setAction(UpdateRequest.ACTION.COMMIT, true, true);
    }
    return req;
}
 
Example 14
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * This test start cdcr source, adds data,starts target cluster, verifies replication,
 * stops cdcr replication and buffering, adds more data, re-enables cdcr and verify replication
 */
public void testBootstrapWithSourceCluster() throws Exception {
  // start the target first so that we know its zkhost
  MiniSolrCloudCluster target = new MiniSolrCloudCluster(1, createTempDir("cdcr-target"), buildJettyConfig("/solr"));
  try {
    System.out.println("Target zkHost = " + target.getZkServer().getZkAddress());
    System.setProperty("cdcr.target.zkHost", target.getZkServer().getZkAddress());

    MiniSolrCloudCluster source = new MiniSolrCloudCluster(1, createTempDir("cdcr-source"), buildJettyConfig("/solr"));
    try {
      source.uploadConfigSet(configset("cdcr-source"), "cdcr-source");

      CollectionAdminRequest.createCollection("cdcr-source", "cdcr-source", 1, 1)
          .withProperty("solr.directoryFactory", "solr.StandardDirectoryFactory")
          .process(source.getSolrClient());
      source.waitForActiveCollection("cdcr-source", 1, 1);

      CloudSolrClient sourceSolrClient = source.getSolrClient();
      int docs = (TEST_NIGHTLY ? 100 : 10);
      int numDocs = indexDocs(sourceSolrClient, "cdcr-source", docs);

      QueryResponse response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      // setup the target cluster
      target.uploadConfigSet(configset("cdcr-target"), "cdcr-target");
      CollectionAdminRequest.createCollection("cdcr-target", "cdcr-target", 1, 1)
          .process(target.getSolrClient());
      target.waitForActiveCollection("cdcr-target", 1, 1);
      CloudSolrClient targetSolrClient = target.getSolrClient();
      targetSolrClient.setDefaultCollection("cdcr-target");

      CdcrTestsUtil.cdcrStart(targetSolrClient);
      CdcrTestsUtil.cdcrStart(sourceSolrClient);

      response = CdcrTestsUtil.getCdcrQueue(sourceSolrClient);
      if (log.isInfoEnabled()) {
        log.info("Cdcr queue response: {}", response.getResponse());
      }
      long foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);

      int total_tlogs_in_index = FSDirectory.open(target.getBaseDir().resolve("node1").
          resolve("cdcr-target_shard1_replica_n1").resolve("data").
          resolve("tlog")).listAll().length;

      assertEquals("tlogs count should be ZERO",0, total_tlogs_in_index);

      CdcrTestsUtil.cdcrStop(sourceSolrClient);
      CdcrTestsUtil.cdcrDisableBuffer(sourceSolrClient);

      int c = 0;
      for (int k = 0; k < 10; k++) {
        UpdateRequest req = new UpdateRequest();
        for (; c < (k + 1) * 100; c++, numDocs++) {
          SolrInputDocument doc = new SolrInputDocument();
          doc.addField("id", "source_" + numDocs);
          doc.addField("xyz", numDocs);
          req.add(doc);
        }
        req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
        log.info("Adding 100 docs with commit=true, numDocs={}", numDocs);
        req.process(sourceSolrClient);
      }

      response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      CdcrTestsUtil.cdcrStart(sourceSolrClient);
      CdcrTestsUtil.cdcrEnableBuffer(sourceSolrClient);

      foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);

    } finally {
      source.shutdown();
    }
  } finally {
    target.shutdown();
  }
}
 
Example 15
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 6-Sep-2018
@Test
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-12028")
public void testBootstrapWithContinousIndexingOnSourceCluster() throws Exception {
  // start the target first so that we know its zkhost
  MiniSolrCloudCluster target = new MiniSolrCloudCluster(1, createTempDir("cdcr-target"), buildJettyConfig("/solr"));
  try {
    if (log.isInfoEnabled()) {
      log.info("Target zkHost = {}", target.getZkServer().getZkAddress());
    }
    System.setProperty("cdcr.target.zkHost", target.getZkServer().getZkAddress());

    MiniSolrCloudCluster source = new MiniSolrCloudCluster(1, createTempDir("cdcr-source"), buildJettyConfig("/solr"));
    try {
      source.uploadConfigSet(configset("cdcr-source"), "cdcr-source");

      CollectionAdminRequest.createCollection("cdcr-source", "cdcr-source", 1, 1)
          .withProperty("solr.directoryFactory", "solr.StandardDirectoryFactory")
          .process(source.getSolrClient());
      source.waitForActiveCollection("cdcr-source", 1, 1);
      CloudSolrClient sourceSolrClient = source.getSolrClient();
      int docs = (TEST_NIGHTLY ? 100 : 10);
      int numDocs = indexDocs(sourceSolrClient, "cdcr-source", docs);

      QueryResponse response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      // setup the target cluster
      target.uploadConfigSet(configset("cdcr-target"), "cdcr-target");
      CollectionAdminRequest.createCollection("cdcr-target", "cdcr-target", 1, 1)
          .process(target.getSolrClient());
      target.waitForActiveCollection("cdcr-target", 1, 1);
      CloudSolrClient targetSolrClient = target.getSolrClient();
      targetSolrClient.setDefaultCollection("cdcr-target");
      Thread.sleep(1000);

      CdcrTestsUtil.cdcrStart(targetSolrClient);
      CdcrTestsUtil.cdcrStart(sourceSolrClient);
      int c = 0;
      for (int k = 0; k < docs; k++) {
        UpdateRequest req = new UpdateRequest();
        for (; c < (k + 1) * 100; c++, numDocs++) {
          SolrInputDocument doc = new SolrInputDocument();
          doc.addField("id", "source_" + numDocs);
          doc.addField("xyz", numDocs);
          req.add(doc);
        }
        req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
        log.info("Adding {} docs with commit=true, numDocs={}", docs, numDocs);
        req.process(sourceSolrClient);
      }

      response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      response = CdcrTestsUtil.getCdcrQueue(sourceSolrClient);
      if (log.isInfoEnabled()) {
        log.info("Cdcr queue response: {}", response.getResponse());
      }
      long foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);

    } finally {
      source.shutdown();
    }
  } finally {
    target.shutdown();
  }
}
 
Example 16
Source File: TestExportTool.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testBasic() throws Exception {
  String COLLECTION_NAME = "globalLoaderColl";
  configureCluster(4)
      .addConfig("conf", configset("cloud-dynamic"))
      .configure();

  try {
    CollectionAdminRequest
        .createCollection(COLLECTION_NAME, "conf", 2, 1)
        .setMaxShardsPerNode(100)
        .process(cluster.getSolrClient());
    cluster.waitForActiveCollection(COLLECTION_NAME, 2, 2);

    String tmpFileLoc = new File(cluster.getBaseDir().toFile().getAbsolutePath() +
        File.separator).getPath();

    UpdateRequest ur = new UpdateRequest();
    ur.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
    int docCount = 1000;

    for (int i = 0; i < docCount; i++) {
      ur.add("id", String.valueOf(i),
          "desc_s", TestUtil.randomSimpleString(random(), 10, 50) ,
          "a_dt", "2019-09-30T05:58:03Z");
    }
    cluster.getSolrClient().request(ur, COLLECTION_NAME);

    QueryResponse qr = cluster.getSolrClient().query(COLLECTION_NAME, new SolrQuery("*:*").setRows(0));
    assertEquals(docCount, qr.getResults().getNumFound());

    String url = cluster.getRandomJetty(random()).getBaseUrl() + "/" + COLLECTION_NAME;


    ExportTool.Info info = new ExportTool.MultiThreadedRunner(url);
    String absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".json";
    info.setOutFormat(absolutePath, "jsonl");
    info.setLimit("200");
    info.fields = "id,desc_s,a_dt";
    info.exportDocs();

    assertJsonDocsCount(info, 200, record -> "2019-09-30T05:58:03Z".equals(record.get("a_dt")));

    info = new ExportTool.MultiThreadedRunner(url);
    absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".json";
    info.setOutFormat(absolutePath, "jsonl");
    info.setLimit("-1");
    info.fields = "id,desc_s";
    info.exportDocs();

    assertJsonDocsCount(info, 1000,null);

    info = new ExportTool.MultiThreadedRunner(url);
    absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".javabin";
    info.setOutFormat(absolutePath, "javabin");
    info.setLimit("200");
    info.fields = "id,desc_s";
    info.exportDocs();

    assertJavabinDocsCount(info, 200);

    info = new ExportTool.MultiThreadedRunner(url);
    absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".javabin";
    info.setOutFormat(absolutePath, "javabin");
    info.setLimit("-1");
    info.fields = "id,desc_s";
    info.exportDocs();
    assertJavabinDocsCount(info, 1000);

  } finally {
    cluster.shutdown();

  }
}
 
Example 17
Source File: TestExportTool.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Nightly
public void testVeryLargeCluster() throws Exception {
  String COLLECTION_NAME = "veryLargeColl";
  configureCluster(4)
      .addConfig("conf", configset("cloud-minimal"))
      .configure();

  try {
    CollectionAdminRequest
        .createCollection(COLLECTION_NAME, "conf", 8, 1)
        .setMaxShardsPerNode(10)
        .process(cluster.getSolrClient());
    cluster.waitForActiveCollection(COLLECTION_NAME, 8, 8);

    String tmpFileLoc = new File(cluster.getBaseDir().toFile().getAbsolutePath() +
        File.separator).getPath();
    String url = cluster.getRandomJetty(random()).getBaseUrl() + "/" + COLLECTION_NAME;


    int docCount = 0;

    for (int j = 0; j < 4; j++) {
      int bsz = 10000;
      UpdateRequest ur = new UpdateRequest();
      ur.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
      for (int i = 0; i < bsz; i++) {
        ur.add("id", String.valueOf((j * bsz) + i), "desc_s", TestUtil.randomSimpleString(random(), 10, 50));
      }
      cluster.getSolrClient().request(ur, COLLECTION_NAME);
      docCount += bsz;
    }

    QueryResponse qr = cluster.getSolrClient().query(COLLECTION_NAME, new SolrQuery("*:*").setRows(0));
    assertEquals(docCount, qr.getResults().getNumFound());

    DocCollection coll = cluster.getSolrClient().getClusterStateProvider().getCollection(COLLECTION_NAME);
    HashMap<String, Long> docCounts = new HashMap<>();
    long totalDocsFromCores = 0;
    for (Slice slice : coll.getSlices()) {
      Replica replica = slice.getLeader();
      try (HttpSolrClient client = new HttpSolrClient.Builder(replica.getBaseUrl()).build()) {
        long count = ExportTool.getDocCount(replica.getCoreName(), client);
        docCounts.put(replica.getCoreName(), count);
        totalDocsFromCores += count;
      }
    }
    assertEquals(docCount, totalDocsFromCores);

    ExportTool.MultiThreadedRunner info = null;
    String absolutePath = null;

    info = new ExportTool.MultiThreadedRunner(url);
    info.output = System.out;
    absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".javabin";
    info.setOutFormat(absolutePath, "javabin");
    info.setLimit("-1");
    info.exportDocs();
    assertJavabinDocsCount(info, docCount);
    for (Map.Entry<String, Long> e : docCounts.entrySet()) {
      assertEquals(e.getValue().longValue(), info.corehandlers.get(e.getKey()).receivedDocs.get());
    }
    info = new ExportTool.MultiThreadedRunner(url);
    info.output = System.out;
    absolutePath = tmpFileLoc + COLLECTION_NAME + random().nextInt(100000) + ".json";
    info.setOutFormat(absolutePath, "jsonl");
    info.fields = "id,desc_s";
    info.setLimit("-1");
    info.exportDocs();
    long actual = ((ExportTool.JsonSink) info.sink).info.docsWritten.get();
    assertTrue("docs written :" + actual + "docs produced : " + info.docsWritten.get(), actual >= docCount);
    assertJsonDocsCount(info, docCount,null);
  } finally {
    cluster.shutdown();

  }
}
 
Example 18
Source File: SolrCmdDistributor.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void addCommit(UpdateRequest ureq, CommitUpdateCommand cmd) {
  if (cmd == null) return;
  ureq.setAction(cmd.optimize ? AbstractUpdateRequest.ACTION.OPTIMIZE
      : AbstractUpdateRequest.ACTION.COMMIT, false, cmd.waitSearcher, cmd.maxOptimizeSegments, cmd.softCommit, cmd.expungeDeletes, cmd.openSearcher);
}
 
Example 19
Source File: MergeIndexesExampleTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private UpdateRequest setupCores() throws SolrServerException, IOException {
  UpdateRequest up = new UpdateRequest();
  up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
  up.deleteByQuery("*:*");
  up.process(getSolrCore0());
  up.process(getSolrCore1());
  up.clear();

  // Add something to each core
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "AAA");
  doc.setField("name", "core0");

  // Add to core0
  up.add(doc);
  up.process(getSolrCore0());

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("name", "core1");
  up.add(doc);
  up.process(getSolrCore1());

  // Now Make sure AAA is in 0 and BBB in 1
  SolrQuery q = new SolrQuery();
  QueryRequest r = new QueryRequest(q);
  q.setQuery("id:AAA");
  assertEquals(1, r.process(getSolrCore0()).getResults().size());
  assertEquals(0, r.process(getSolrCore1()).getResults().size());

  assertEquals(1,
      getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(0,
      getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

  assertEquals(0,
      getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1,
      getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

  return up;
}
 
Example 20
Source File: TestSolrProperties.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() throws Exception {

  UpdateRequest up = new UpdateRequest();
  up.setAction(ACTION.COMMIT, true, true);
  up.deleteByQuery("*:*");
  up.process(getSolrCore0());
  up.process(getSolrCore1());
  up.clear();

  // Add something to each core
  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "AAA");
  doc.setField("core0", "yup stopfra stopfrb stopena stopenb");

  // Add to core0
  up.add(doc);
  up.process(getSolrCore0());

  SolrTestCaseJ4.ignoreException("unknown field");

  // You can't add it to core1
  expectThrows(Exception.class, () -> up.process(getSolrCore1()));

  // Add to core1
  doc.setField("id", "BBB");
  doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
  doc.removeField("core0");
  up.add(doc);
  up.process(getSolrCore1());

  // You can't add it to core1
  SolrTestCaseJ4.ignoreException("core0");
  expectThrows(Exception.class, () -> up.process(getSolrCore0()));
  SolrTestCaseJ4.resetExceptionIgnores();

  // now Make sure AAA is in 0 and BBB in 1
  SolrQuery q = new SolrQuery();
  QueryRequest r = new QueryRequest(q);
  q.setQuery("id:AAA");
  assertEquals(1, r.process(getSolrCore0()).getResults().size());
  assertEquals(0, r.process(getSolrCore1()).getResults().size());

  // Now test Changing the default core
  assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

  assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
  assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

  // Now test reloading it should have a newer open time
  String name = "core0";
  SolrClient coreadmin = getSolrAdmin();
  CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long before = mcr.getStartTime(name).getTime();
  CoreAdminRequest.reloadCore(name, coreadmin);

  mcr = CoreAdminRequest.getStatus(name, coreadmin);
  long after = mcr.getStartTime(name).getTime();
  assertTrue("should have more recent time: " + after + "," + before, after > before);

}