use of play.db.jpa.Model in project play-cookbook by spinscale.
the class SolrSearchTest method testPagination.
@Test
public void testPagination() throws Exception {
Car.deleteAll();
clearSolrServerIndex();
Fixtures.loadModels("test-data-many-cars.yml");
List<Model> result = new Query("brand_s:*", Car.class).limit(10).start(10).fetch();
assertEquals(3, result.size());
}
use of play.db.jpa.Model in project play-cookbook by spinscale.
the class SolrSearchTest method testOffset.
@Test
public void testOffset() throws Exception {
Car.deleteAll();
clearSolrServerIndex();
Fixtures.loadModels("test-data-many-cars.yml");
List<Model> result = new Query("brand_s:*", Car.class).limit(5).start(0).fetch();
assertEquals(5, result.size());
result = new Query("brand_s:*", Car.class).limit(5).start(5).fetch();
assertEquals(5, result.size());
result = new Query("brand_s:*", Car.class).limit(5).start(10).fetch();
assertEquals(3, result.size());
}
use of play.db.jpa.Model 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