Search in sources :

Example 1 with SearchFactory

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();
    });
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ColumnMetadata(de.micromata.genome.jpa.metainf.ColumnMetadata) Serializable(java.io.Serializable) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) Query(org.apache.lucene.search.Query) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) FullTextEntityManager(org.hibernate.search.jpa.FullTextEntityManager) SearchFactory(org.hibernate.search.SearchFactory) RuntimeIOException(de.micromata.genome.util.runtime.RuntimeIOException) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) RuntimeIOException(de.micromata.genome.util.runtime.RuntimeIOException) IOException(java.io.IOException) IndexableField(org.apache.lucene.index.IndexableField) ClassicAnalyzer(org.apache.lucene.analysis.standard.ClassicAnalyzer) IndexReader(org.apache.lucene.index.IndexReader) IndexReaderAccessor(org.hibernate.search.indexes.IndexReaderAccessor)

Example 2 with SearchFactory

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();
}
Also used : SearchFactory(org.hibernate.search.SearchFactory) Query(org.apache.lucene.search.Query) RuntimeIOException(de.micromata.genome.util.runtime.RuntimeIOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) PfEmgrFactory(org.projectforge.framework.persistence.jpa.PfEmgrFactory) ClassicAnalyzer(org.apache.lucene.analysis.standard.ClassicAnalyzer) IndexableField(org.apache.lucene.index.IndexableField) IndexDescriptor(org.hibernate.search.metadata.IndexDescriptor) ToStringStyle(org.apache.commons.lang3.builder.ToStringStyle) FieldDescriptor(org.hibernate.search.metadata.FieldDescriptor) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ColumnMetadata(de.micromata.genome.jpa.metainf.ColumnMetadata) Document(org.apache.lucene.document.Document) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) PropertyDescriptor(org.hibernate.search.metadata.PropertyDescriptor) SearchFactory(org.hibernate.search.SearchFactory) Service(org.springframework.stereotype.Service) FullTextEntityManager(org.hibernate.search.jpa.FullTextEntityManager) TopDocs(org.apache.lucene.search.TopDocs) Set(java.util.Set) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) List(java.util.List) IndexReaderAccessor(org.hibernate.search.indexes.IndexReaderAccessor) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) IndexReader(org.apache.lucene.index.IndexReader) IndexedTypeDescriptor(org.hibernate.search.metadata.IndexedTypeDescriptor) IndexSearcher(org.apache.lucene.search.IndexSearcher) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) PropertyDescriptor(org.hibernate.search.metadata.PropertyDescriptor) IndexDescriptor(org.hibernate.search.metadata.IndexDescriptor) IndexedTypeDescriptor(org.hibernate.search.metadata.IndexedTypeDescriptor) FullTextEntityManager(org.hibernate.search.jpa.FullTextEntityManager) FieldDescriptor(org.hibernate.search.metadata.FieldDescriptor)

Example 3 with SearchFactory

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;
}
Also used : SearchFactory(org.hibernate.search.SearchFactory) FullTextSession(org.hibernate.search.FullTextSession) FullTextQuery(org.hibernate.search.FullTextQuery) ReaderProvider(org.hibernate.search.reader.ReaderProvider) ArrayList(java.util.ArrayList) QueryBuilder(org.hibernate.search.query.dsl.QueryBuilder) DirectoryProvider(org.hibernate.search.store.DirectoryProvider) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 4 with SearchFactory

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);
    }
}
Also used : SearchFactory(org.hibernate.search.SearchFactory) FullTextSession(org.hibernate.search.FullTextSession) TermDocs(org.apache.lucene.index.TermDocs) ReaderProvider(org.hibernate.search.reader.ReaderProvider) Term(org.apache.lucene.index.Term) NodeDocument(com.openkm.dao.bean.NodeDocument) Document(org.apache.lucene.document.Document) TermEnum(org.apache.lucene.index.TermEnum) Fieldable(org.apache.lucene.document.Fieldable) DirectoryProvider(org.hibernate.search.store.DirectoryProvider) IndexReader(org.apache.lucene.index.IndexReader) ServletContext(javax.servlet.ServletContext) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Example 5 with SearchFactory

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 ***");
    }
}
Also used : SearchFactory(org.hibernate.search.SearchFactory) FullTextSession(org.hibernate.search.FullTextSession) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) FullTextSession(org.hibernate.search.FullTextSession)

Aggregations

SearchFactory (org.hibernate.search.SearchFactory)20 IOException (java.io.IOException)10 IndexReader (org.apache.lucene.index.IndexReader)8 FullTextSession (org.hibernate.search.FullTextSession)7 FullTextEntityManager (org.hibernate.search.jpa.FullTextEntityManager)6 ReaderProvider (org.hibernate.search.reader.ReaderProvider)5 Document (org.apache.lucene.document.Document)4 LuceneDictionary (org.apache.lucene.search.spell.LuceneDictionary)4 SpellChecker (org.apache.lucene.search.spell.SpellChecker)4 Directory (org.apache.lucene.store.Directory)4 IndexReaderAccessor (org.hibernate.search.indexes.IndexReaderAccessor)4 DirectoryProvider (org.hibernate.search.store.DirectoryProvider)4 ColumnMetadata (de.micromata.genome.jpa.metainf.ColumnMetadata)3 RuntimeIOException (de.micromata.genome.util.runtime.RuntimeIOException)3 Serializable (java.io.Serializable)3 ToStringBuilder (org.apache.commons.lang3.builder.ToStringBuilder)3 ClassicAnalyzer (org.apache.lucene.analysis.standard.ClassicAnalyzer)3 IndexableField (org.apache.lucene.index.IndexableField)3 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3