Commit dc65b0ca authored by Matija Obreza's avatar Matija Obreza
Browse files

Reindex one entity, FaoInstitute lucene bridge

parent 42d0186b
package org.crophub.rest.common.lucene.genesys;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.crophub.rest.common.model.impl.FaoInstitute;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
public class FaoInstituteBridge implements FieldBridge {
public static final Log LOG = LogFactory.getLog(FaoInstituteBridge.class);
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
// In this particular class the name of the new field was passed
// from the name field of the ClassBridge Annotation. This is not
// a requirement. It just works that way in this instance. The
// actual name could be supplied by hard coding it below.
FaoInstitute institute = (FaoInstitute) value;
StringDocument sd = new StringDocument();
sd.append(institute.getAcronym());
sd.append(institute.getCode());
sd.append(institute.getFullName());
if (institute.getCountry() != null)
sd.append(institute.getCountry().getName());
LOG.debug("IDX." + name + " = " + sd.toString());
Field field = new Field(name, sd.toString(), luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
field.setBoost(luceneOptions.getBoost());
document.add(field);
}
}
......@@ -28,7 +28,9 @@ import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.crophub.rest.common.lucene.genesys.FaoInstituteBridge;
import org.hibernate.annotations.Index;
import org.hibernate.search.annotations.ClassBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
......@@ -38,6 +40,7 @@ import org.hibernate.search.annotations.Store;
@Table(name = "faoinstitute", uniqueConstraints = @UniqueConstraint(columnNames = { "code" }))
@org.hibernate.annotations.Table(appliesTo = "faoinstitute", indexes = { @Index(columnNames = { "code" }, name = "code_FAOINSTITUTE") })
@Indexed
@ClassBridge(name = "body", impl = FaoInstituteBridge.class)
public class FaoInstitute extends GeoEntity implements java.io.Serializable {
/**
......
......@@ -4,4 +4,6 @@ public interface LuceneIndexer {
void reindexEverything();
void reindexEntity(String entityName);
}
......@@ -22,6 +22,30 @@ public class LuceneIndexerImpl implements LuceneIndexer {
@Autowired
private EntityManagerFactory entityManagerFactory;
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public void reindexEntity(String entityName) {
LOG.info("Indexing " + entityName + " in the DB...");
StopWatch timer = new StopWatch();
Session session = (Session) this.entityManagerFactory.createEntityManager().getDelegate();
FullTextSession fullTextSession = Search.getFullTextSession(session);
try {
timer.start();
LOG.info("Start and Wait...");
fullTextSession.createIndexer(Class.forName(entityName)).startAndWait();
timer.stop();
} catch (InterruptedException e) {
LOG.error(e);
} catch (ClassNotFoundException e) {
LOG.error(e);
} finally {
fullTextSession.close();
}
LOG.info("Finished indexing after: " + timer.getTime() + "ms");
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public void reindexEverything() {
......
......@@ -27,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/admin")
......@@ -72,5 +73,11 @@ public class AdminController {
luceneIndexer.reindexEverything();
return "redirect:/admin/";
}
@RequestMapping(method = RequestMethod.POST, value = "/reindexEntity")
public String reindexEntities(@RequestParam("entity") String entityName) {
luceneIndexer.reindexEntity(entityName);
return "redirect:/admin/";
}
}
......@@ -22,6 +22,18 @@
<form method="post" action="<c:url value="/admin/reindexEverything" />">
<input type="submit" value="Reindex search indexes" />
</form>
<form method="post" action="<c:url value="/admin/reindexEntity" />">
<select name="entity">
<option value="org.crophub.rest.common.model.impl.Country">Countries</option>
<option value="org.crophub.rest.common.model.impl.FaoInstitute">WIEWS Institutes</option>
<option value="org.crophub.rest.common.model.impl.ActivityPost">Posts</option>
<option value="org.crophub.rest.common.model.impl.Article">Articles</option>
<option value="org.crophub.rest.common.model.genesys.Accession">Accessions</option>
</select>
<input type="submit" value="Reindex search indexes" />
</form>
</sec:authorize>
</body>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment