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");
}
}
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);
}
}
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();
}
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";
}
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");
}
Aggregations