Java Code Examples for org.apache.solr.core.CoreContainer#create()

The following examples show how to use org.apache.solr.core.CoreContainer#create() . 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: TestCrossCoreJoin.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@BeforeClass
  public static void beforeTests() throws Exception {
    System.setProperty("enable.update.log", "false"); // schema12 doesn't support _version_
//    initCore("solrconfig.xml","schema12.xml"); 

    // File testHome = createTempDir().toFile();
    // FileUtils.copyDirectory(getFile("solrj/solr"), testHome);
    initCore("solrconfig.xml", "schema12.xml", TEST_HOME(), "collection1");
    final CoreContainer coreContainer = h.getCoreContainer();

    fromCore = coreContainer.create("fromCore", ImmutableMap.of("configSet", "minimal"));

    assertU(add(doc("id", "1", "name", "john", "title", "Director", "dept_s", "Engineering")));
    assertU(add(doc("id", "2", "name", "mark", "title", "VP", "dept_s", "Marketing")));
    assertU(add(doc("id", "3", "name", "nancy", "title", "MTS", "dept_s", "Sales")));
    assertU(add(doc("id", "4", "name", "dave", "title", "MTS", "dept_s", "Support", "dept_s", "Engineering")));
    assertU(add(doc("id", "5", "name", "tina", "title", "VP", "dept_s", "Engineering")));
    assertU(commit());

    update(fromCore, add(doc("id", "10", "dept_id_s", "Engineering", "text", "These guys develop stuff", "cat", "dev")));
    update(fromCore, add(doc("id", "11", "dept_id_s", "Marketing", "text", "These guys make you look good")));
    update(fromCore, add(doc("id", "12", "dept_id_s", "Sales", "text", "These guys sell stuff")));
    update(fromCore, add(doc("id", "13", "dept_id_s", "Support", "text", "These guys help customers")));
    update(fromCore, commit());

  }
 
Example 2
Source File: TestSubQueryTransformerCrossCore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void beforeTests() throws Exception {
  System.setProperty("enable.update.log", "false"); // schema12 doesn't support _version_
  initCore("solrconfig-basic.xml","schema-docValuesJoin.xml");
  final CoreContainer coreContainer = h.getCoreContainer();

  fromCore = coreContainer.create("fromCore", //FileSystems.getDefault().getPath( TEST_HOME()), ImmutableMap.of("config","solrconfig-basic.xml","schema","schema-docValuesJoin.xml"
      ImmutableMap.of("configSet", "minimal")
      );
  assertU(add(doc("id", "1","name_s", "john", "title_s", "Director", "dept_ss_dv","Engineering",
      "text_t","These guys develop stuff")));
  assertU(add(doc("id", "2","name_s", "mark", "title_s", "VP", "dept_ss_dv","Marketing",
      "text_t","These guys make you look good")));
  assertU(add(doc("id", "3","name_s", "nancy", "title_s", "MTS", "dept_ss_dv","Sales",
      "text_t","These guys sell stuff")));
  assertU(add(doc("id", "4","name_s", "dave", "title_s", "MTS", "dept_ss_dv","Support", "dept_ss_dv","Engineering"
      , "text_t","These guys help customers")));
  assertU(add(doc("id", "5","name_s", "tina", "title_s", "VP", "dept_ss_dv","Engineering",
      "text_t","These guys develop stuff")));
  assertU(commit());

  update(fromCore, add(doc("id","10", "dept_id_s", "Engineering", "text_t","These guys develop stuff", "salary_i_dv", "1000")));
  update(fromCore, add(doc("id","11", "dept_id_s", "Marketing", "text_t","These guys make you look good","salary_i_dv", "1500")));
  update(fromCore, add(doc("id","12", "dept_id_s", "Sales", "text_t","These guys sell stuff","salary_i_dv", "1600")));
  update(fromCore, add(doc("id","13", "dept_id_s", "Support", "text_t","These guys help customers","salary_i_dv", "800")));
  update(fromCore, commit());
}
 
Example 3
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void createCoreAndValidateListeners(int numTimesCalled, int numTimesCalledFirstSearcher,
    int numTimesCalledAfterGetSearcher, int numTimesCalledFirstSearcherAfterGetSearcher) throws Exception {
  CoreContainer cores = h.getCoreContainer();
  CoreDescriptor cd = h.getCore().getCoreDescriptor();
  SolrCore newCore = null;
  // reset counters
  MockSearcherListener.numberOfTimesCalled = new AtomicInteger();
  MockSearcherListener.numberOfTimesCalledFirstSearcher = new AtomicInteger();
  
  try {
    // Create a new core, this should call all the firstSearcherListeners
    newCore = cores.create("core1", cd.getInstanceDir(), ImmutableMap.of("config", "solrconfig-searcher-listeners1.xml"), false);
    
    //validate that the new core was created with the correct solrconfig
    assertNotNull(newCore.getSearchComponent("mock"));
    assertEquals(MockSearchComponent.class, newCore.getSearchComponent("mock").getClass());
    assertFalse(newCore.getSolrConfig().useColdSearcher);
    
    doQuery(newCore);
    
    assertEquals(numTimesCalled, MockSearcherListener.numberOfTimesCalled.get());
    assertEquals(numTimesCalledFirstSearcher, MockSearcherListener.numberOfTimesCalledFirstSearcher.get());
    
    addDummyDoc(newCore);
    
    // Open a new searcher, this should call the newSearcherListeners
    @SuppressWarnings({"rawtypes"})
    Future<?>[] future = new Future[1];
    newCore.getSearcher(true, false, future);
    future[0].get();
    
    assertEquals(numTimesCalledAfterGetSearcher, MockSearcherListener.numberOfTimesCalled.get());
    assertEquals(numTimesCalledFirstSearcherAfterGetSearcher, MockSearcherListener.numberOfTimesCalledFirstSearcher.get());
    
  } finally {
    if (newCore != null) {
      cores.unload("core1");
    }
  }
}
 
Example 4
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testDontUseColdSearcher() throws Exception {
  MockSearchComponent.registerFirstSearcherListener = false;
  MockSearchComponent.registerNewSearcherListener = false;
  MockSearchComponent.registerSlowSearcherListener = true;
  final AtomicBoolean querySucceeded = new AtomicBoolean(false);
  SlowSearcherListener.numberOfTimesCalled = new AtomicInteger(0);
  SlowSearcherListener.latch = new CountDownLatch(1);
  
  CoreContainer cores = h.getCoreContainer();
  CoreDescriptor cd = h.getCore().getCoreDescriptor();
  final SolrCore newCore;
  boolean coreCreated = false;
  try {
    // Create a new core, this should call all the firstSearcherListeners
    newCore = cores.create("core1", cd.getInstanceDir(), ImmutableMap.of("config", "solrconfig-searcher-listeners1.xml"), false);
    coreCreated = true;
    
    //validate that the new core was created with the correct solrconfig
    assertNotNull(newCore.getSearchComponent("mock"));
    assertEquals(MockSearchComponent.class, newCore.getSearchComponent("mock").getClass());
    assertFalse(newCore.getSolrConfig().useColdSearcher);
    
    Thread t = new Thread() {
      public void run() {
        try {
          doQuery(newCore);
          querySucceeded.set(true);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      };
    };
    t.start();
    
    if (System.getProperty(SYSPROP_NIGHTLY) != null) {
      // even if we wait here, the SearcherListener should not finish
      Thread.sleep(500);
    }
    // validate that the searcher warmer didn't finish yet. 
    assertEquals(0, SlowSearcherListener.numberOfTimesCalled.get());
    assertFalse("Query should be waiting for warming to finish", querySucceeded.get());
    
    // Let warmer finish 
    SlowSearcherListener.latch.countDown();
    
    // Validate that the query eventually succeeds
    for (int i = 0; i <= 1000; i++) {
      if (querySucceeded.get()) {
        break;
      }
      if (i == 1000) {
        fail("Query didn't succeed after 10 secoonds");
      }
      Thread.sleep(10);
    }
    
  } finally {
    
    if (coreCreated) {
      cores.unload("core1");
    }
  }
}
 
Example 5
Source File: TestIndexSearcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testUseColdSearcher() throws Exception {
  MockSearchComponent.registerFirstSearcherListener = false;
  MockSearchComponent.registerNewSearcherListener = false;
  MockSearchComponent.registerSlowSearcherListener = true;
  final AtomicBoolean querySucceeded = new AtomicBoolean(false);
  SlowSearcherListener.numberOfTimesCalled = new AtomicInteger(0);
  SlowSearcherListener.latch = new CountDownLatch(1);
  
  
  CoreContainer cores = h.getCoreContainer();
  CoreDescriptor cd = h.getCore().getCoreDescriptor();
  final SolrCore newCore;
  boolean coreCreated = false;
  try {
    System.setProperty("tests.solr.useColdSearcher", "true");
    // Create a new core, this should call all the firstSearcherListeners
    newCore = cores.create("core1", cd.getInstanceDir(), ImmutableMap.of("config", "solrconfig-searcher-listeners1.xml"), false);
    coreCreated = true;
    
    //validate that the new core was created with the correct solrconfig
    assertNotNull(newCore.getSearchComponent("mock"));
    assertEquals(MockSearchComponent.class, newCore.getSearchComponent("mock").getClass());
    assertTrue(newCore.getSolrConfig().useColdSearcher);
    
    Thread t = new Thread() {
      public void run() {
        try {
          doQuery(newCore);
          querySucceeded.set(true);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      };
    };
    t.start();
    
    // validate that the query runs before the searcher warmer finishes
    for (int i = 0; i <= 1000; i++) {
      if (querySucceeded.get()) {
        break;
      }
      if (i == 1000) {
        fail("Query didn't succeed after 10 secoonds");
      }
      Thread.sleep(10);
    }
    
    assertEquals(0, SlowSearcherListener.numberOfTimesCalled.get());
    
  } finally {
    System.getProperties().remove("tests.solr.useColdSearcher");
    if (coreCreated) {
      SlowSearcherListener.latch.countDown();
      cores.unload("core1");
    }
    
  }
}
 
Example 6
Source File: SolrRecordWriter.java    From examples with Apache License 2.0 4 votes vote down vote up
public static EmbeddedSolrServer createEmbeddedSolrServer(Path solrHomeDir, FileSystem fs, Path outputShardDir)
    throws IOException {

  if (solrHomeDir == null) {
    throw new IOException("Unable to find solr home setting");
  }
  LOG.info("Creating embedded Solr server with solrHomeDir: " + solrHomeDir + ", fs: " + fs + ", outputShardDir: " + outputShardDir);

  Path solrDataDir = new Path(outputShardDir, "data");

  String dataDirStr = solrDataDir.toUri().toString();

  SolrResourceLoader loader = new SolrResourceLoader(solrHomeDir.toString(), null, null);

  LOG.info(String
      .format(Locale.ENGLISH, 
          "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s",
          solrHomeDir, solrHomeDir.toUri(), loader.getInstanceDir(),
          loader.getConfigDir(), dataDirStr, outputShardDir));

  // TODO: This is fragile and should be well documented
  System.setProperty("solr.directoryFactory", HdfsDirectoryFactory.class.getName()); 
  System.setProperty("solr.lock.type", "hdfs"); 
  System.setProperty("solr.hdfs.nrtcachingdirectory", "false");
  System.setProperty("solr.hdfs.blockcache.enabled", "false");
  System.setProperty("solr.autoCommit.maxTime", "600000");
  System.setProperty("solr.autoSoftCommit.maxTime", "-1");
  
  CoreContainer container = new CoreContainer(loader);
  container.load();
  
  Properties props = new Properties();
  props.setProperty(CoreDescriptor.CORE_DATADIR, dataDirStr);
  
  CoreDescriptor descr = new CoreDescriptor(container, "core1", solrHomeDir.toString(), props);
  
  SolrCore core = container.create(descr);
  
  if (!(core.getDirectoryFactory() instanceof HdfsDirectoryFactory)) {
    throw new UnsupportedOperationException(
        "Invalid configuration. Currently, the only DirectoryFactory supported is "
            + HdfsDirectoryFactory.class.getSimpleName());
  }

  EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "core1");
  return solr;
}
 
Example 7
Source File: SolrRecordWriter.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
public static EmbeddedSolrServer createEmbeddedSolrServer(Path solrHomeDir, FileSystem fs, Path outputShardDir)
      throws IOException {

    LOG.info("Creating embedded Solr server with solrHomeDir: " + solrHomeDir + ", fs: " + fs + ", outputShardDir: " + outputShardDir);
    
    LOG.info("Using custom SolrRecordWriter class for HBaseMapReduceIndexer");
    if (LOG.isDebugEnabled()) {
      LOG.debug("Listing files contained in solrHomeDir {} ...", solrHomeDir);
      int i = 0;
      for (File file : FileUtils.listFiles(new File(solrHomeDir.toString()), null, true)) {
        // strip off common path prefix for better human readability
        String relPath = file.toString();
        relPath = relPath.substring(solrHomeDir.toString().length() + 1, relPath.length());
        LOG.debug("solrHomeDirFile[{}]: {}", i++, relPath);
      }
    }

    Path solrDataDir = new Path(outputShardDir, "data");

    String dataDirStr = solrDataDir.toUri().toString();

    SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrHomeDir.toString()), null, null);

    LOG.info(String
        .format(Locale.ENGLISH, 
            "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s",
            solrHomeDir, solrHomeDir.toUri(), loader.getInstancePath(),
            loader.getConfigDir(), dataDirStr, outputShardDir));

    // TODO: This is fragile and should be well documented
    System.setProperty("solr.directoryFactory", HdfsDirectoryFactory.class.getName()); 
    System.setProperty("solr.lock.type", DirectoryFactory.LOCK_TYPE_HDFS);
    System.setProperty("solr.hdfs.nrtcachingdirectory", "false");
    System.setProperty("solr.hdfs.blockcache.enabled", "false");
    System.setProperty("solr.autoCommit.maxTime", "600000");
    System.setProperty("solr.autoSoftCommit.maxTime", "-1");
    
    CoreContainer container = new CoreContainer(loader);
    container.load();

    SolrCore core = container.create("core1", Paths.get(solrHomeDir.toString()), ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr), false);
//    SolrCore core = container.create("", ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr));
    
    if (!(core.getDirectoryFactory() instanceof HdfsDirectoryFactory)) {
      throw new UnsupportedOperationException(
          "Invalid configuration. Currently, the only DirectoryFactory supported is "
              + HdfsDirectoryFactory.class.getSimpleName());
    }

    EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "core1");
//    EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "");
    return solr;
  }
 
Example 8
Source File: TestUtils.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
private static EmbeddedSolrServer createEmbeddedSolrServer(File solrHomeDir, FileSystem fs, Path outputShardDir)
        throws IOException {

  LOG.info("Creating embedded Solr server with solrHomeDir: " + solrHomeDir + ", fs: " + fs + ", outputShardDir: " + outputShardDir);

  // copy solrHomeDir to ensure it isn't modified across multiple unit tests or multiple EmbeddedSolrServer instances
  File tmpDir = Files.createTempDir();
  tmpDir.deleteOnExit();
  FileUtils.copyDirectory(solrHomeDir, tmpDir);
  solrHomeDir = tmpDir;

  Path solrDataDir = new Path(outputShardDir, "data");

  String dataDirStr = solrDataDir.toUri().toString();

  SolrResourceLoader loader = new SolrResourceLoader(Paths.get(solrHomeDir.toString()), null, null);

  LOG.info(String
          .format(Locale.ENGLISH,
                  "Constructed instance information solr.home %s (%s), instance dir %s, conf dir %s, writing index to solr.data.dir %s, with permdir %s",
                  solrHomeDir, solrHomeDir.toURI(), loader.getInstancePath(),
                  loader.getConfigDir(), dataDirStr, outputShardDir));

  // TODO: This is fragile and should be well documented
  System.setProperty("solr.directoryFactory", HdfsDirectoryFactory.class.getName());
  System.setProperty("solr.lock.type", DirectoryFactory.LOCK_TYPE_HDFS);
  System.setProperty("solr.hdfs.nrtcachingdirectory", "false");
  System.setProperty("solr.hdfs.blockcache.enabled", "false");
  System.setProperty("solr.autoCommit.maxTime", "600000");
  System.setProperty("solr.autoSoftCommit.maxTime", "-1");

  CoreContainer container = new CoreContainer(loader);
  container.load();

  SolrCore core = container.create("core1", Paths.get(solrHomeDir.toString()), ImmutableMap.of(CoreDescriptor.CORE_DATADIR, dataDirStr), false);

  if (!(core.getDirectoryFactory() instanceof HdfsDirectoryFactory)) {
    throw new UnsupportedOperationException(
            "Invalid configuration. Currently, the only DirectoryFactory supported is "
                    + HdfsDirectoryFactory.class.getSimpleName());
  }

  EmbeddedSolrServer solr = new EmbeddedSolrServer(container, "core1");
  return solr;
}