use of org.apache.solr.client.solrj.beans.Field in project play-cookbook by spinscale.
the class SearchModel method getSolrFieldName.
private static String getSolrFieldName(String fieldName, Class clazz) {
try {
java.lang.reflect.Field field = clazz.getField(fieldName);
Field annot = field.getAnnotation(Field.class);
if (annot != null && !annot.value().equals("#default")) {
return annot.value();
}
} catch (Exception e) {
e.printStackTrace();
}
return fieldName;
}
use of org.apache.solr.client.solrj.beans.Field in project play-cookbook by spinscale.
the class SolrPlugin method onEvent.
@Override
public void onEvent(String message, Object context) {
if (!StringUtils.startsWith(message, "JPASupport.")) {
return;
}
try {
Model model = (Model) context;
String entityId = model.getClass().getName() + ":" + model.getId().toString();
SolrServer server = getSearchServer();
server.deleteById(entityId);
// if ("JPASupport.objectPersisted".equals(message) || "JPASupport.objectUpdated".equals(message)) {
if ("JPASupport.objectUpdated".equals(message)) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", entityId);
doc.addField("searchClass", model.getClass().getName());
for (java.lang.reflect.Field field : context.getClass().getFields()) {
String fieldName = field.getName();
Field annot = field.getAnnotation(Field.class);
if (annot == null) {
continue;
}
String annotationValue = annot.value();
if (annotationValue != null && !"#default".equals(annotationValue)) {
fieldName = annotationValue;
}
doc.addField(fieldName, field.get(context));
}
server.add(doc);
Logger.info("Added entity %s with fields %s on event %s", model.getClass().getSimpleName(), doc.getFieldNames(), message);
}
server.commit();
} catch (Exception e) {
Logger.error(e, "Problem updating entity %s on event %s with error %s", context, message, e.getMessage());
}
}
Aggregations