Search code examples
javasolrlucenesolrj

How to add document in Apache Solr (SolrJ ) using Java API on specific core


How we can add data to a specific core running on Apcache Solr. Currently i have this piece of code, and it adds data on a single core, what if we have multiple cores running with having same field names, how solar decides to write in which core, because this piece of code is Nebulous !

We are nowhere defining , which core/collection/index !!!

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;

import java.io.IOException;

public class SolrjPopulator {
  public static void main(String[] args) throws IOException, SolrServerException {
    HttpSolrServer server = new HttpSolrServer("http://localhost:8983/solr");
    for(int i=0;i<1000;++i) {
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("cat", "book");
      doc.addField("id", "book-" + i);
      doc.addField("name", "The Legend of the Hobbit part " + i);
      server.add(doc);
      if(i%100==0) server.commit();  // periodically flush
    }
    server.commit();
  }
}

Solution

  • Cores will have different URLs and you can use those URLs in code:

    HttpSolrServer server0 = new HttpSolrServer("http://localhost:8983/solr/core0");
    HttpSolrServer server1 = new HttpSolrServer("http://localhost:8983/solr/core1");