Search in sources :

Example 6 with FullTextSession

use of org.hibernate.search.FullTextSession in project rubia-forums by flashboss.

the class ForumsSearchModuleImpl method findTopics.

@SuppressWarnings("unchecked")
public ResultPage<Topic> findTopics(SearchCriteria criteria) throws ModuleException {
    if (criteria != null) {
        try {
            EntityManager session = getSession();
            FullTextSession fullTextSession = getFullTextSession((Session) session.getDelegate());
            Builder builder = new Builder();
            String keywords = criteria.getKeywords();
            if (keywords != null && keywords.length() != 0) {
                String[] fields = null;
                Searching searching = Searching.valueOf(criteria.getSearching());
                switch(searching) {
                    case TITLE_MSG:
                        fields = new String[] { "message.text", "topic.subject" };
                        break;
                    case MSG:
                        fields = new String[] { "message.text" };
                        break;
                }
                MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, new StandardAnalyzer());
                builder.add(parser.parse(keywords), MUST);
            }
            String forumId = criteria.getForum();
            if (forumId != null && forumId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.id", forumId)), MUST);
            }
            String categoryId = criteria.getCategory();
            if (categoryId != null && categoryId.length() != 0) {
                builder.add(new TermQuery(new Term("topic.forum.category.id", categoryId)), MUST);
            }
            String userName = criteria.getAuthor();
            if (userName != null && userName.length() != 0) {
                builder.add(new WildcardQuery(new Term("poster.userId", userName)), MUST);
            }
            String timePeriod = criteria.getTimePeriod();
            if (timePeriod != null && timePeriod.length() != 0) {
                addPostTimeQuery(builder, TimePeriod.valueOf(timePeriod));
            }
            FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(builder.build(), Post.class);
            SortOrder sortOrder = SortOrder.valueOf(criteria.getSortOrder());
            SortBy sortBy = valueOf(criteria.getSortBy());
            fullTextQuery.setSort(getSort(sortBy, sortOrder));
            fullTextQuery.setProjection("topic.id");
            LinkedHashSet<Integer> topicIds = new LinkedHashSet<Integer>();
            LinkedHashSet<Integer> topicToDispIds = new LinkedHashSet<Integer>();
            int start = criteria.getPageSize() * criteria.getPageNumber();
            int end = start + criteria.getPageSize();
            int index = 0;
            for (Object o : fullTextQuery.list()) {
                Integer id = (Integer) ((Object[]) o)[0];
                if (topicIds.add(id)) {
                    if (index >= start && index < end) {
                        topicToDispIds.add(id);
                    }
                    index++;
                }
            }
            List<Topic> topics = null;
            if (topicToDispIds.size() > 0) {
                Query q = session.createQuery("from Topic as t join fetch t.poster where t.id IN ( :topicIds )");
                q.setParameter("topicIds", topicToDispIds);
                List<Topic> results = q.getResultList();
                topics = new LinkedList<Topic>();
                for (Integer id : topicToDispIds) {
                    for (Topic topic : results) {
                        if (id.equals(topic.getId())) {
                            topics.add(topic);
                            break;
                        }
                    }
                }
            }
            ResultPage<Topic> resultPage = new ResultPage<Topic>();
            resultPage.setPage(topics);
            resultPage.setResultSize(topicIds.size());
            return resultPage;
        } catch (ParseException e) {
            return null;
        } catch (Exception e) {
            throw new ModuleException(e.getMessage(), e);
        }
    } else {
        throw new IllegalArgumentException("criteria cannot be null");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WildcardQuery(org.apache.lucene.search.WildcardQuery) FullTextSession(org.hibernate.search.FullTextSession) Search.getFullTextSession(org.hibernate.search.Search.getFullTextSession) FullTextQuery(org.hibernate.search.FullTextQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(javax.persistence.Query) TermQuery(org.apache.lucene.search.TermQuery) SortBy(it.vige.rubia.search.SortBy) Builder(org.apache.lucene.search.BooleanQuery.Builder) ResultPage(it.vige.rubia.search.ResultPage) Topic(it.vige.rubia.model.Topic) ModuleException(it.vige.rubia.ModuleException) TermQuery(org.apache.lucene.search.TermQuery) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) SortOrder(it.vige.rubia.search.SortOrder) Term(org.apache.lucene.index.Term) ParseException(org.apache.lucene.queryparser.classic.ParseException) ModuleException(it.vige.rubia.ModuleException) EntityManager(javax.persistence.EntityManager) Searching(it.vige.rubia.search.Searching) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) ParseException(org.apache.lucene.queryparser.classic.ParseException) FullTextQuery(org.hibernate.search.FullTextQuery)

Example 7 with FullTextSession

use of org.hibernate.search.FullTextSession in project openmrs-core by openmrs.

the class HibernateContextDAO method updateSearchIndexForType.

@Override
@Transactional
public void updateSearchIndexForType(Class<?> type) {
    // From http://docs.jboss.org/hibernate/search/3.3/reference/en-US/html/manual-index-changes.html#search-batchindex-flushtoindexes
    FullTextSession session = Search.getFullTextSession(sessionFactory.getCurrentSession());
    session.purgeAll(type);
    // Prepare session for batch work
    session.flush();
    session.clear();
    FlushMode flushMode = session.getFlushMode();
    CacheMode cacheMode = session.getCacheMode();
    try {
        session.setFlushMode(FlushMode.MANUAL);
        session.setCacheMode(CacheMode.IGNORE);
        // Scrollable results will avoid loading too many objects in memory
        ScrollableResults results = session.createCriteria(type).setFetchSize(1000).scroll(ScrollMode.FORWARD_ONLY);
        int index = 0;
        while (results.next()) {
            index++;
            // index each element
            session.index(results.get(0));
            if (index % 1000 == 0) {
                // apply changes to indexes
                session.flushToIndexes();
                // free memory since the queue is processed
                session.clear();
            }
        }
        session.flushToIndexes();
        session.clear();
    } finally {
        session.setFlushMode(flushMode);
        session.setCacheMode(cacheMode);
    }
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) CacheMode(org.hibernate.CacheMode) ScrollableResults(org.hibernate.ScrollableResults) FlushMode(org.hibernate.FlushMode) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with FullTextSession

use of org.hibernate.search.FullTextSession in project openmrs-core by openmrs.

the class HibernateContextDAO method updateSearchIndexForObject.

/**
 * @see org.openmrs.api.db.ContextDAO#updateSearchIndexForObject(java.lang.Object)
 */
@Override
@Transactional
public void updateSearchIndexForObject(Object object) {
    FullTextSession session = Search.getFullTextSession(sessionFactory.getCurrentSession());
    session.index(object);
    session.flushToIndexes();
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with FullTextSession

use of org.hibernate.search.FullTextSession in project trainning by fernandotomasio.

the class SettingsController method reindexLucene.

@RequestMapping("reindex_lucene")
public String reindexLucene(Model model) {
    // systemService.sendMail("lincolny2001@yahoo.com.br", "SGC - INDICAÇÃO PARA CURSO");
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    try {
        fullTextSession.createIndexer().startAndWait();
    } catch (InterruptedException ex) {
        Logger.getLogger(SettingsController.class.getName()).log(Level.SEVERE, null, ex);
    }
    return "home";
}
Also used : SessionFactory(org.hibernate.SessionFactory) FullTextSession(org.hibernate.search.FullTextSession) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with FullTextSession

use of org.hibernate.search.FullTextSession in project ice by JBEI.

the class RebuildLuceneIndexTask method execute.

@Override
public void execute() {
    Logger.info("Rebuilding lucene index in background");
    Session session = HibernateUtil.newSession();
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    MassIndexer indexer = fullTextSession.createIndexer();
    indexer.idFetchSize(20);
    indexer.progressMonitor(IndexerProgressMonitor.getInstance());
    try {
        indexer.startAndWait();
    } catch (InterruptedException e) {
        Thread.interrupted();
    }
    fullTextSession.close();
    Logger.info("Lucene rebuild complete");
}
Also used : FullTextSession(org.hibernate.search.FullTextSession) MassIndexer(org.hibernate.search.MassIndexer) FullTextSession(org.hibernate.search.FullTextSession) Session(org.hibernate.Session)

Aggregations

FullTextSession (org.hibernate.search.FullTextSession)19 FullTextQuery (org.hibernate.search.FullTextQuery)13 ModuleException (it.vige.rubia.ModuleException)8 ResultPage (it.vige.rubia.search.ResultPage)8 Searching (it.vige.rubia.search.Searching)8 SortBy (it.vige.rubia.search.SortBy)8 SortOrder (it.vige.rubia.search.SortOrder)8 EntityManager (javax.persistence.EntityManager)8 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)8 Term (org.apache.lucene.index.Term)8 MultiFieldQueryParser (org.apache.lucene.queryparser.classic.MultiFieldQueryParser)8 ParseException (org.apache.lucene.queryparser.classic.ParseException)8 Builder (org.apache.lucene.search.BooleanQuery.Builder)8 TermQuery (org.apache.lucene.search.TermQuery)8 WildcardQuery (org.apache.lucene.search.WildcardQuery)8 Search.getFullTextSession (org.hibernate.search.Search.getFullTextSession)8 Session (org.hibernate.Session)7 QueryBuilder (org.hibernate.search.query.dsl.QueryBuilder)6 Post (it.vige.rubia.model.Post)4 Topic (it.vige.rubia.model.Topic)4