Search in sources :

Example 1 with Model

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());
}
Also used : Query(play.modules.solr.Query) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Car(models.Car) Model(play.db.jpa.Model) UnitTest(play.test.UnitTest) Test(org.junit.Test)

Example 2 with Model

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());
}
Also used : Query(play.modules.solr.Query) SolrQuery(org.apache.solr.client.solrj.SolrQuery) Car(models.Car) Model(play.db.jpa.Model) UnitTest(play.test.UnitTest) Test(org.junit.Test)

Example 3 with Model

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());
    }
}
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

Model (play.db.jpa.Model)3 Car (models.Car)2 SolrQuery (org.apache.solr.client.solrj.SolrQuery)2 Test (org.junit.Test)2 Query (play.modules.solr.Query)2 UnitTest (play.test.UnitTest)2 MalformedURLException (java.net.MalformedURLException)1 SolrServer (org.apache.solr.client.solrj.SolrServer)1 Field (org.apache.solr.client.solrj.beans.Field)1 CommonsHttpSolrServer (org.apache.solr.client.solrj.impl.CommonsHttpSolrServer)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1