[JAVA] Import JSON with SolrJ

Task

SolrJ is supposed to use the "SolrInputDocument" class to import the document, but I made it because it seems that the function to convert from JSON to SolrInputDocument is not provided. The type of the field can be specified by the property.

Code

import java.io.IOException;
import java.util.Properties;

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 com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class SolrJsonImporter {

	SolrClient client;
	Properties props;

	public SolrJsonImporter(String endPoint) {
		client = new HttpSolrClient.Builder(endPoint).build();
	}

	public UpdateResponse add(String json) throws SolrServerException, IOException {

		SolrInputDocument document = new SolrInputDocument();
		JsonObject jsonObj = (JsonObject) new Gson().fromJson(json, JsonObject.class);
		if (this.props == null) {
			for (String key : jsonObj.keySet()) {
				String value = jsonObj.get(key).getAsString();
				document.addField(key, value);
			}

		} else {
			processFields(jsonObj, document, this.props);

		}

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

	}

	private void processFields(JsonObject jsonObj, SolrInputDocument document, Properties props) {
		for (int n = 0; true; n++) {
			String propKey = String.format("field[%d]", n);
			String fieldName = props.getProperty(propKey + ".name");
			if (fieldName == null) {
				break;
			}

			String fieldType = props.getProperty(propKey + ".type");

			System.err.println(fieldName + "," + fieldType);

			String fieldTarget = props.getProperty(propKey + ".target", fieldName);

			Object oValue = null;

			if (fieldType.equals("String")) {
				oValue = jsonObj.get(fieldName).getAsString();
			} //
			else if (fieldType.equals("String[]")) {
				JsonArray arr = jsonObj.get(fieldName).getAsJsonArray();
				Object[] oo = new Object[arr.size()];
				for (int x = 0; x < arr.size(); x++) {
					oo[x] = arr.get(x).getAsString();
				}
				oValue = oo;
			} //
			else if (fieldType.equals("Long")) {
				oValue = jsonObj.get(fieldName).getAsLong();
			} //
			else if (fieldType.equals("Long[]")) {
				JsonArray arr = jsonObj.get(fieldName).getAsJsonArray();
				Object[] oo = new Object[arr.size()];
				for (int x = 0; x < arr.size(); x++) {
					oo[x] = arr.get(x).getAsLong();
				}
				oValue = oo;
			} //
			else if (fieldType.equals("Double")) {
				oValue = jsonObj.get(fieldName).getAsDouble();
			} //
			else if (fieldType.equals("Double[]")) {
				JsonArray arr = jsonObj.get(fieldName).getAsJsonArray();
				Object[] oo = new Object[arr.size()];
				for (int x = 0; x < arr.size(); x++) {
					oo[x] = arr.get(x).getAsDouble();
				}
				oValue = oo;
			} //
			document.addField(fieldTarget, oValue);
		}

	}

	public UpdateResponse commit() throws IOException, SolrServerException {
		return client.commit();
	}

	private void setProperties(Properties props) {
		this.props = props;

	}

}

How to use

String urlString = "http://localhost:8983/solr/sandbox";
Properties props = new Properties();
props.setProperty("debug", "false");
props.setProperty("field[0].name", "id");
props.setProperty("field[0].type", "String");
props.setProperty("field[1].name", "text");
props.setProperty("field[1].type", "String");
props.setProperty("field[2].name", "field1");
props.setProperty("field[2].type", "String");
props.setProperty("field[3].name", "field2");
props.setProperty("field[3].type", "String[]");
props.setProperty("field[4].name", "field3");
props.setProperty("field[4].type", "Long[]");
props.setProperty("field[5].name", "field4");
props.setProperty("field[5].type", "Double[]");
props.setProperty("field[6].name", "field5");
props.setProperty("field[6].type", "Long");
props.setProperty("field[7].name", "field6");
props.setProperty("field[7].type", "Double");

String json = "{" //
		+ "'id':'001'" //
		+ ",'text':'This is test.'" //
		+ ",'field1':'fieldvalue001'" //
		+ ",'field2':['fieldvalue001']" //
		+ ",'field3':[1,2,3]" //
		+ ",'field4':[0.1,0.2,0.3]" //
		+ ",'field5':123" //
		+ ",'field6':0.123" //
		+ "}";

SolrJsonImporter util = new SolrJsonImporter(urlString);
util.setProperties(props);

System.err.println(util.add(json));

util.commit();

Summary

This class makes it easy to import JSON via SolrJ. I think that it is convenient when you have existing JSON and want to convert it appropriately and import it.

Recommended Posts

Import JSON with SolrJ
Import documents with SolrJ
Diffed with JSON
CSV import with BOM
JSON validation with JSON schema
Handle JSON with minimal-json
Format JSON with org.json
[Java] JSON communication with jackson
Import device images with Android app
Handle JSON effortlessly with Alamofire's respone Json
Apply regular expression matching with JSON Sassert
Create a GUI JSON Viewer with Ruby/GTK3
Working with huge JSON in Java Lambda
Convert JSON to TSV and TSV to JSON with Ruby