Search code examples
javasolrsolrj

Indexing document with SolrJ


I'm testing my SolrJ code for Indexing a Document. When I execute my code, it looks like working fine. but after execution, When I check the expected result on Solr UI, it displays nothing. but after restarting Solr, it displays the expected result on Solr UI correctly.

How can I get indexed documents without restarting Solr?

My Solr Version : 5.2.1 please refer my Java codes below

import java.io.IOException;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.*;

public class addDocument {

    public static void main(String[] args) throws IOException,
            SolrServerException {
        try {

            String urlString = "http://(SolrServer)/solr/test_core/";
            SolrClient solr = new HttpSolrClient(urlString);
            SolrInputDocument doc = new SolrInputDocument();
            // indexing a document

            doc.addField("id", "12311122113");
            doc.addField("title", "Insertion Completed");

            UpdateResponse response = solr.add(doc);
            System.out.println("Done");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Solution

  • You need to commit after document indexing process is done. so if you restart solr, it will do auto commit. if you dont want to restart Solr. you can try either by

    1. reloading core (refer below URL, change URL names according to you)

      http://127.0.0.1:8080/solr/admin/cores?action=RELOAD&core=core1

      or

    2. use solr.commit(); in your code