Wednesday, February 1, 2023

how to create a solr document with autogenerated unique id ?

how to create a solr document with autogenerated unique id ? You can create a Solr document with an auto-generated unique ID by not specifying the ID field (i.e., id) when adding the document to Solr. If you do not specify an ID, Solr will automatically generate a unique ID for the document.
  
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.common.SolrInputDocument;

import java.io.IOException;

public class SolrJExample {
    public static void main(String[] args) throws SolrServerException, IOException {
        String solrUrl = "http://localhost:8983/solr/your_core_name";
        SolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();

        SolrInputDocument document = new SolrInputDocument();
        document.addField("field1", "value1");
        document.addField("field2", "value2");

        solrClient.add(document);
        solrClient.commit();
    }
}

  

No comments:

Post a Comment