Search in sources :

Example 1 with Field

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;
}
Also used : Field(org.apache.solr.client.solrj.beans.Field)

Example 2 with Field

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());
    }
}
Also used : Field(org.apache.solr.client.solrj.beans.Field) SolrInputDocument(org.apache.solr.common.SolrInputDocument) Model(play.db.jpa.Model) SolrServer(org.apache.solr.client.solrj.SolrServer) CommonsHttpSolrServer(org.apache.solr.client.solrj.impl.CommonsHttpSolrServer) MalformedURLException(java.net.MalformedURLException)

Aggregations

Field (org.apache.solr.client.solrj.beans.Field)2 MalformedURLException (java.net.MalformedURLException)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 CommonsHttpSolrServer (org.apache.solr.client.solrj.impl.CommonsHttpSolrServer)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 Model (play.db.jpa.Model)1