Skip to content

Instantly share code, notes, and snippets.

@mdread
Created November 27, 2013 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdread/7680293 to your computer and use it in GitHub Desktop.
Save mdread/7680293 to your computer and use it in GitHub Desktop.
Embed JSON values in Solr fields and auto unmarshall it when converting from a document to a bean
import org.apache.http.client.HttpClient;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
public class ExtendedHttpSolrServer extends HttpSolrServer {
private static final long serialVersionUID = -5355794252201598225L;
private DocumentObjectBinder binder;
public ExtendedHttpSolrServer(String baseURL) {
super(baseURL);
}
public ExtendedHttpSolrServer(String baseURL, HttpClient client, ResponseParser parser) {
super(baseURL, client, parser);
}
public ExtendedHttpSolrServer(String baseURL, HttpClient client) {
super(baseURL, client);
}
@Override
public DocumentObjectBinder getBinder() {
if (binder == null) {
binder = new JsonDocumentObjectBinder();
}
return binder;
}
}
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;
import org.apache.solr.client.solrj.beans.DocumentObjectBinder;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import com.google.gson.Gson;
public class JsonDocumentObjectBinder extends DocumentObjectBinder {
public <T> List<T> getBeans(Class<T> clazz, SolrDocumentList solrDocList) {
List<T> beans = super.getBeans(clazz, solrDocList);
for (int i = 0; i < beans.size(); i++) {
try {
mapJsonFields(beans.get(i), solrDocList.get(i));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return beans;
}
public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
T bean = super.getBean(clazz, solrDoc);
try {
return mapJsonFields(bean, solrDoc);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private <T> T mapJsonFields(T bean, SolrDocument document) throws IllegalArgumentException, IllegalAccessException {
Class<?> clazz = bean.getClass();
for (Field field : clazz.getDeclaredFields()) {
if(field.getAnnotation(JSONField.class) != null){
JSONField annotation = field.getAnnotation(JSONField.class);
String solrField = annotation.value();
if(solrField.equals(JSONField.DEFAULT))
solrField = field.getName();
String json = (String)document.getFieldValue(solrField);
if(json == null || json.trim().length() == 0)
continue;
Type type = field.getType();
field.setAccessible(true);
if(Collection.class.isAssignableFrom((Class<?>)type))
type = field.getGenericType();
Object value = new Gson().fromJson(json, type);
field.set(bean, value);
}
}
return bean;
}
}
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Target({FIELD})
@Retention(RUNTIME)
public @interface JSONField {
public static final String DEFAULT ="#default";
String value() default DEFAULT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment