use of org.hibernate.search.SearchFactory in project projectforge by micromata.
the class LuceneServiceImpl method searchSimple.
/**
* Returns html
*
* @param entityClass
* @param search
* @return
*/
public String searchSimple(Class<?> entityClass, String search, String fieldList) {
StringBuilder sb = new StringBuilder();
String rsearch = StringUtils.isBlank(search) ? "*" : search;
return emf.runInTrans((emgr) -> {
FullTextEntityManager femg = emgr.getFullTextEntityManager();
SearchFactory sf = femg.getSearchFactory();
String[] searchFields;
if (StringUtils.isNotBlank(fieldList)) {
searchFields = splitFieldList(fieldList);
} else {
searchFields = getSearchFieldsForEntity(entityClass);
}
final MultiFieldQueryParser parser = new MultiFieldQueryParser(searchFields, new ClassicAnalyzer());
parser.setAllowLeadingWildcard(true);
try {
Query query = parser.parse(rsearch);
IndexReaderAccessor ia = sf.getIndexReaderAccessor();
try (IndexReader ir = ia.open(entityClass)) {
IndexSearcher is = new IndexSearcher(ir);
TopDocs ret = is.search(query, 1000);
sb.append("found: " + ret.totalHits).append("<br/>\n");
for (ScoreDoc sdoc : ret.scoreDocs) {
sb.append("===================================================================").append("<br/>\n");
sb.append(StringEscapeUtils.escapeHtml4(sdoc.toString())).append(" ");
Document document = is.doc(sdoc.doc);
sb.append("LuceneDocument: ");
renderDocument(document, sb);
IndexableField id = document.getField("id");
ColumnMetadata idCol = emf.getMetadataRepository().getEntityMetadata(entityClass).getIdColumn();
if (id == null) {
id = document.getField("pk");
}
String snval = id.stringValue();
Class<?> idclazz = idCol.getJavaType();
Serializable entityPk;
if (idclazz.isAssignableFrom(Long.class)) {
entityPk = Long.parseLong(snval);
} else {
entityPk = Integer.parseInt(snval);
}
Object entity = emgr.findByPkDetached(entityClass, entityPk);
String osdesc = ToStringBuilder.reflectionToString(entity, ToStringStyle.MULTI_LINE_STYLE, false);
osdesc = StringEscapeUtils.escapeHtml4(osdesc);
osdesc = StringUtils.replace(osdesc, "\n", "<br/>\n<nbsp/><nbsp/>");
sb.append(osdesc).append("<br/>\n");
sb.append("<br/>\n");
}
} catch (IOException ex) {
throw new RuntimeIOException(ex);
}
} catch (org.apache.lucene.queryparser.classic.ParseException ex) {
throw new RuntimeException(ex);
}
return sb.toString();
});
}
use of org.hibernate.search.SearchFactory in project projectforge by micromata.
the class LuceneServiceImpl method getIndexDescription.
public String getIndexDescription(Class<?> entityClass) {
StringBuilder sb = new StringBuilder();
emf.runInTrans((emgr) -> {
sb.append("class: ").append(entityClass.getName()).append("\n");
FullTextEntityManager femg = emgr.getFullTextEntityManager();
SearchFactory sf = femg.getSearchFactory();
IndexedTypeDescriptor itd = sf.getIndexedTypeDescriptor(entityClass);
List<String> fields = itd.getIndexedProperties().stream().map((desc) -> desc.getName()).collect(Collectors.toList());
sb.append("\nFields: ").append(StringUtils.join(fields, ", ")).append("\n");
IndexedTypeDescriptor descr = sf.getIndexedTypeDescriptor(entityClass);
sb.append("\nIndexedTypeDescriptor: indexed: ").append(descr.isIndexed()).append("\nFields:\n");
for (FieldDescriptor field : descr.getIndexedFields()) {
sb.append(" ").append(field).append("<br?\n");
}
sb.append("\nProperties: \n");
for (PropertyDescriptor ip : descr.getIndexedProperties()) {
sb.append(" ").append(ip).append("\n");
}
sb.append("\nIndexe: \n");
for (IndexDescriptor ides : descr.getIndexDescriptors()) {
sb.append(" ").append(ides).append("\n");
}
String[] sfields = getSearchFieldsForEntity(entityClass);
sb.append("\nSearchFields: ").append(StringUtils.join(sfields, ",")).append("\n");
return null;
});
return sb.toString();
}
use of org.hibernate.search.SearchFactory in project document-management-system by openkm.
the class SearchDAO method getTerms.
/**
* Get Lucent document terms.
*/
@SuppressWarnings("unchecked")
public List<String> getTerms(Class<?> entityType, String nodeUuid) throws CorruptIndexException, IOException {
List<String> terms = new ArrayList<String>();
FullTextSession ftSession = null;
IndexSearcher searcher = null;
ReaderProvider provider = null;
Session session = null;
IndexReader reader = null;
try {
session = HibernateUtil.getSessionFactory().openSession();
ftSession = Search.getFullTextSession(session);
SearchFactory sFactory = ftSession.getSearchFactory();
provider = sFactory.getReaderProvider();
QueryBuilder builder = sFactory.buildQueryBuilder().forEntity(entityType).get();
Query query = builder.keyword().onField("uuid").matching(nodeUuid).createQuery();
DirectoryProvider<Directory>[] dirProv = sFactory.getDirectoryProviders(NodeDocument.class);
reader = provider.openReader(dirProv[0]);
searcher = new IndexSearcher(reader);
TopDocs topDocs = searcher.search(query, 1);
for (ScoreDoc sDoc : topDocs.scoreDocs) {
if (!reader.isDeleted(sDoc.doc)) {
for (TermEnum te = reader.terms(); te.next(); ) {
Term t = te.term();
if ("text".equals(t.field())) {
for (TermDocs tds = reader.termDocs(t); tds.next(); ) {
if (sDoc.doc == tds.doc()) {
terms.add(t.text());
// log.info("Field: {} - {}", t.field(), t.text());
}
}
}
}
}
}
} finally {
if (provider != null && reader != null) {
provider.closeReader(reader);
}
if (searcher != null) {
searcher.close();
}
HibernateUtil.close(ftSession);
HibernateUtil.close(session);
}
return terms;
}
use of org.hibernate.search.SearchFactory in project document-management-system by openkm.
the class ListIndexesServlet method showLuceneDocument.
/**
* List Lucene indexes
*/
@SuppressWarnings("unchecked")
private void showLuceneDocument(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean showTerms = WebUtils.getBoolean(request, "showTerms");
int id = WebUtils.getInt(request, "id", 0);
FullTextSession ftSession = null;
ReaderProvider rProv = null;
Session session = null;
IndexReader idx = null;
List<Map<String, String>> fields = new ArrayList<Map<String, String>>();
try {
session = HibernateUtil.getSessionFactory().openSession();
ftSession = Search.getFullTextSession(session);
SearchFactory sFactory = ftSession.getSearchFactory();
rProv = sFactory.getReaderProvider();
DirectoryProvider<Directory>[] dirProv = sFactory.getDirectoryProviders(NodeDocument.class);
idx = rProv.openReader(dirProv[0]);
// Print Lucene documents
if (!idx.isDeleted(id)) {
Document doc = idx.document(id);
String hibClass = null;
for (Fieldable fld : doc.getFields()) {
Map<String, String> field = new HashMap<String, String>();
field.put("name", fld.name());
field.put("value", fld.stringValue());
fields.add(field);
if (fld.name().equals("_hibernate_class")) {
hibClass = fld.stringValue();
}
}
/**
* 1) Get all the terms using indexReader.terms()
* 2) Process the term only if it belongs to the target field.
* 3) Get all the docs using indexReader.termDocs(term);
* 4) So, we have the term-doc pairs at this point.
*/
if (showTerms && NodeDocument.class.getCanonicalName().equals(hibClass)) {
List<String> terms = new ArrayList<String>();
for (TermEnum te = idx.terms(); te.next(); ) {
Term t = te.term();
if ("text".equals(t.field())) {
for (TermDocs tds = idx.termDocs(t); tds.next(); ) {
if (id == tds.doc()) {
terms.add(t.text());
}
}
}
}
Map<String, String> field = new HashMap<String, String>();
field.put("name", "terms");
field.put("value", terms.toString());
fields.add(field);
}
}
ServletContext sc = getServletContext();
sc.setAttribute("fields", fields);
sc.setAttribute("id", id);
sc.setAttribute("max", idx.maxDoc() - 1);
sc.setAttribute("prev", id > 0);
sc.setAttribute("next", id < idx.maxDoc() - 1);
sc.setAttribute("showTerms", showTerms);
sc.getRequestDispatcher("/admin/list_indexes.jsp").forward(request, response);
} finally {
if (rProv != null && idx != null) {
rProv.closeReader(idx);
}
HibernateUtil.close(ftSession);
HibernateUtil.close(session);
}
}
use of org.hibernate.search.SearchFactory in project document-management-system by openkm.
the class RebuildIndexesServlet method optimizeIndexes.
/**
* Do real indexes optimization.
*/
public static void optimizeIndexes() throws Exception {
FullTextSession ftSession = null;
Session session = null;
if (optimizeIndexesRunning) {
log.warn("*** Optimize indexes already running ***");
} else {
optimizeIndexesRunning = true;
log.debug("*** Begin optimize indexes ***");
try {
session = HibernateUtil.getSessionFactory().openSession();
ftSession = Search.getFullTextSession(session);
// Optimize indexes
SearchFactory searchFactory = ftSession.getSearchFactory();
searchFactory.optimize();
} catch (Exception e) {
throw e;
} finally {
optimizeIndexesRunning = false;
HibernateUtil.close(ftSession);
HibernateUtil.close(session);
}
log.debug("*** End optimize indexes ***");
}
}
Aggregations