if ("".equals(indexName)) {
request.setAttribute("error", "Index name should be not empty, Operation " + action + " not executed.");
getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
return;
}
try (GaeDirectory directory = new GaeDirectory(indexName); Analyzer analyzer = new PorterAnalyzer(LUCENE_VERSION)){
final String text = request.getParameter("text");
final String query = request.getParameter("query");
final String page = request.getParameter("page");
request.setAttribute("index", indexName);
if("index".equalsIgnoreCase(action)) {
long start = System.currentTimeMillis();
try (IndexWriter w = new IndexWriter(directory, getIndexWriterConfig(LUCENE_VERSION, analyzer))) {
request.setAttribute("info", "Indexed in index '" + indexName + "' string: " + text.substring(0, Math.min(70, text.length())) + (text.length() > 70 ? "..." : ""));
addDoc(w, text);
} catch(Exception e) {
request.setAttribute("message", e.getMessage());
log.error("Error indexing text {}.", text, e);
}
long end = System.currentTimeMillis();
log.info("Search: {} millis.", end - start);
} else if("delete".equalsIgnoreCase(action)) {
try {
directory.delete();
request.setAttribute("info", "Successfully deleted index:'" + indexName + "'.");
} catch (RuntimeException e) {
request.setAttribute("error", "Error during delete index:'" + indexName + "' cause:" + e.getMessage());
log.error("Error during delete index '{}'.", indexName, e);
}