Search code examples
javasolrsolrj

How do I add one document to solr index using solrj?


I can reindex an entire solr core using the following code:

 public void indexSolr() throws SolrServerException, IOException {
    HttpSolrServer solr = new HttpSolrServer(solrIndexPath);
    logger.info("Indexing fcv solr at " + solrIndexPath);

    // reindex to pickup new articles
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("qt", "/" + solrDataImportPath);
    params.set("command", "full-import");
    params.set("clean", "true");
    params.set("commit", "true");
    solr.query(params);
}

How can I insert just one single document into the index without having to index the whole thing?


Solution

  • Are you looking for something like this?

    public void insertOneDoc() throws SolrServerException, IOException {
      HttpSolrServer solr = new HttpSolrServer(solrIndexPath);
      SolrInputDocument doc = new SolrInputDocument();
      doc.addField("fieldName", "fieldValue");
      solr.add(doc);
      solr.commit();
    }
    

    Because that adds a single document to your index that the HttpSolrServer refereces.