[JAVA] Import documents with SolrJ

Code


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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;

public class HelloDocumentAdd2 {

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

		String urlString = "http://localhost:8983/solr/sandbox";
		SolrClient solr = new HttpSolrClient.Builder(urlString).build();

		SolrInputDocument document = new SolrInputDocument();
		document.addField("id", "5");
		document.addField("field_text_en", "This is test. for Date formatting ");
		document.addField("field_location_string", "ja");

		{
			String dateNow = (new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")).format(new Date());
			System.err.println(dateNow);
			document.addField("field_date", dateNow);
		}

		UpdateResponse response = solr.add(document); // throws SolrServerException,IOException

		System.err.println(response);

		// Remember to commit your changes!

		solr.commit();

	}

}

Recommended Posts

Import documents with SolrJ
Import JSON with SolrJ
CSV import with BOM
Import device images with Android app