Java Code Examples for org.hibernate.search.FullTextSession#createFullTextQuery()

The following examples show how to use org.hibernate.search.FullTextSession#createFullTextQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchDAO.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Security is evaluated by Lucene, so query result are already pruned. This means that every node
 * should have its security (user and role) info stored in Lucene. This provides very quick search
 * but security modifications need to be recursively applied to reach every document node in the
 * repository. This may take several hours (or days) is big repositories.
 */
@SuppressWarnings("unchecked")
private NodeResultSet runQueryLucene(FullTextSession ftSession, Query query, int offset, int limit)
		throws IOException, InvalidTokenOffsetsException, HibernateException, DatabaseException {
	log.debug("runQueryLucene({}, {}, {}, {})", new Object[]{ftSession, query, offset, limit});
	List<NodeQueryResult> results = new ArrayList<NodeQueryResult>();
	NodeResultSet result = new NodeResultSet();
	FullTextQuery ftq = ftSession.createFullTextQuery(query, NodeDocument.class, NodeFolder.class, NodeMail.class);
	ftq.setProjection(FullTextQuery.SCORE, FullTextQuery.THIS);
	ftq.enableFullTextFilter("readAccess");
	QueryScorer scorer = new QueryScorer(query, NodeDocument.TEXT_FIELD);

	// Set limits
	ftq.setFirstResult(offset);
	ftq.setMaxResults(limit);

	// Highlight using a CSS style
	SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<span class='highlight'>", "</span>");
	Highlighter highlighter = new Highlighter(formatter, scorer);
	highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer, MAX_FRAGMENT_LEN));

	for (Iterator<Object[]> it = ftq.iterate(); it.hasNext(); ) {
		Object[] qRes = it.next();
		Float score = (Float) qRes[0];
		NodeBase nBase = (NodeBase) qRes[1];

		// Add result
		addResult(ftSession, results, highlighter, score, nBase);
	}

	result.setTotal(ftq.getResultSize());
	result.setResults(results);
	log.debug("runQueryLucene: {}", result);
	return result;
}
 
Example 2
Source File: SearchServiceBean.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Performs a search in Lucene and puts the resulting product object ids in
 * a corresponding map.
 * 
 * @param query
 *            the Lucene query
 * @param fts
 *            the Hibernate Search FullTextSession
 * @param map
 *            the map for the search results
 * @throws HibernateException
 */
private void searchViaLucene(org.apache.lucene.search.Query query,
        FullTextSession fts, LinkedHashMap<Long, VOService> map)
        throws HibernateException {
    FullTextQuery ftQuery = fts.createFullTextQuery(query, Product.class);
    ftQuery.setProjection("key");
    List<?> result = ftQuery.list();
    if (result != null) {
        for (Object item : result) {
            map.put((Long) ((Object[]) item)[0], null);
        }
    }
}
 
Example 3
Source File: HibernateSearch5InstrumentationTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
private org.hibernate.search.FullTextQuery createNonJpaFullTextQuery(Query query) {
    FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(entityManager.unwrap(Session.class));

    return fullTextSession.createFullTextQuery(query, Dog.class);
}
 
Example 4
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 5
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);
	
	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query, matching the user's search keywords against the name 
	// and description fields of App.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	// Perform the search query, and put its results on the HTTP request object
	List<App> apps = 	hibernateQuery.list();
	logger.info("Found " + apps.size() + " search results");
	request.setAttribute("apps", apps);
    
	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 6
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 7
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);
	
	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query, matching the user's search keywords against the name 
	// and description fields of App.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	// Perform the search query, and put its results on the HTTP request object
	List<App> apps = 	hibernateQuery.list();
	logger.info("Found " + apps.size() + " search results");
	request.setAttribute("apps", apps);
    
	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 8
Source File: SearchServlet.java    From maven-framework-project with MIT License 4 votes vote down vote up
/**
 * This method contains the primary search functionality for this servlet, and is automatically invoked once for every HTTP
 * POST to the mapped URL. 
 */
@SuppressWarnings("unchecked")
@Override	
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
	Logger logger = LoggerFactory.getLogger(SearchServlet.class);
	
	// Get the user's search keyword(s) from CGI variables
	String searchString = request.getParameter("searchString");
	logger.info("Received searchString [" + searchString + "]");

	// Start a Hibernate session.
	Session session = StartupDataLoader.openSession();
	
	// Create a Hibernate Search wrapper around the vanilla Hibernate session
	FullTextSession fullTextSession = Search.getFullTextSession(session);

	// Begin a transaction.  This may not be strictly necessary, but is a good practice in general.
	fullTextSession.beginTransaction();

	// Create a Hibernate Search QueryBuilder for the appropriate Lucene index (i.e. the index for "App" in this case)
	QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( App.class ).get();
	
	// Use the QueryBuilder to construct a Lucene keyword query... matching the user's search keywords against the "name" 
	// and "description" fields of App, as well as "name" field of associated Device entities, and the "comments" field of
	// embedded CustomerReview objects.
	org.apache.lucene.search.Query luceneQuery = queryBuilder
		.keyword()
		.onFields("name", "description", "supportedDevices.name", "customerReviews.comments")
		.matching(searchString)
		.createQuery();
	org.hibernate.Query hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, App.class);
	
	List<App> apps = hibernateQuery.list();
	logger.info("Found " + apps.size() + " apps");

	// Detach the results from the Hibernate session (to prevent unwanted interaction between the view layer 
	// and Hibernate when associated devices or embedded customer reviews are referenced)
	fullTextSession.clear();

	// Put the search results on the HTTP reqeust object
	request.setAttribute("apps", apps);

	// Close and clean up the Hibernate session
	fullTextSession.getTransaction().commit();
	session.close();
	
	// Forward the request object (including the search results) to the JSP/JSTL view for rendering
	getServletContext().getRequestDispatcher("/WEB-INF/pages/search.jsp").forward(request, response);
}
 
Example 9
Source File: SearchManager.java    From maven-framework-project with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception{
		ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
		SessionFactory sessionFactory = applicationContext.getBean("hibernate4sessionFactory",SessionFactory.class);
		FullTextSession fullTextSession = Search.getFullTextSession(sessionFactory.openSession());
		
		//使用Hibernate Search api查询 从多个字段匹配 name、description、authors.name
//		QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class ).get();
//		Query luceneQuery = qb.keyword().onFields("name","description","authors.name").matching("移动互联网").createQuery();
		
		//使用lucene api查询 从多个字段匹配 name、description、authors.name
		//使用庖丁分词器
		MultiFieldQueryParser queryParser=new MultiFieldQueryParser(Version.LUCENE_36, new String[]{"name","description","authors.name"}, new PaodingAnalyzer());
		Query luceneQuery=queryParser.parse("实战");
		
		FullTextQuery fullTextQuery =fullTextSession.createFullTextQuery(luceneQuery, Book.class);
		//设置每页显示多少条
		fullTextQuery.setMaxResults(5);
		//设置当前页
		fullTextQuery.setFirstResult(0);
		
		//高亮设置
		SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<b><font color='red'>", "<font/></b>");
		QueryScorer queryScorer=new QueryScorer(luceneQuery);
		Highlighter highlighter=new Highlighter(formatter, queryScorer);

		@SuppressWarnings("unchecked")
		List<Book> resultList = fullTextQuery.list();
		System.out.println("共查找到["+resultList.size()+"]条记录");
		for (Book book : resultList) {
			String highlighterString=null;
			Analyzer analyzer=new PaodingAnalyzer();
			try {
				//高亮name
				highlighterString=highlighter.getBestFragment(analyzer, "name", book.getName());
				if(highlighterString!=null){
					book.setName(highlighterString);
				}
				//高亮authors.name
				Set<Author> authors = book.getAuthors();
				for (Author author : authors) {
					highlighterString=highlighter.getBestFragment(analyzer, "authors.name", author.getName());
					if(highlighterString!=null){
						author.setName(highlighterString);
					}
				}
				//高亮description
				highlighterString=highlighter.getBestFragment(analyzer, "description", book.getDescription());
				if(highlighterString!=null){
					book.setDescription(highlighterString);
				}
			} catch (Exception e) {
			}
			
			System.out.println("书名:"+book.getName()+"\n描述:"+book.getDescription()+"\n出版日期:"+book.getPublicationDate());
			System.out.println("----------------------------------------------------------");
		}
		
		fullTextSession.close();
		sessionFactory.close();
		
	}
 
Example 10
Source File: BookDaoImpl.java    From maven-framework-project with MIT License 4 votes vote down vote up
@Override
public QueryResult<Book> query(String keyword, int start, int pagesize,Analyzer analyzer,String...field) throws Exception{
	
	QueryResult<Book> queryResult=new QueryResult<Book>();
	
	List<Book> books=new ArrayList<Book>();
	
	FullTextSession fullTextSession = Search.getFullTextSession(getSession());
	
	//使用Hibernate Search api查询 从多个字段匹配 name、description、authors.name
	//QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Book.class ).get();
	//Query luceneQuery = qb.keyword().onFields(field).matching(keyword).createQuery();

	//使用lucene api查询 从多个字段匹配 name、description、authors.name
	
	MultiFieldQueryParser queryParser=new MultiFieldQueryParser(Version.LUCENE_36,new String[]{"name","description","authors.name"}, analyzer);
	Query luceneQuery=queryParser.parse(keyword);
	
	FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery);
	int searchresultsize = fullTextQuery.getResultSize();
	queryResult.setSearchresultsize(searchresultsize);
	System.out.println("共查找到["+searchresultsize+"]条记录");
	
	fullTextQuery.setFirstResult(start);
	fullTextQuery.setMaxResults(pagesize);
	
	//设置按id排序
	fullTextQuery.setSort(new Sort(new SortField("id", SortField.INT ,true)));
	
	//高亮设置
	SimpleHTMLFormatter formatter=new SimpleHTMLFormatter("<b><font color='red'>", "</font></b>");
	QueryScorer queryScorer=new QueryScorer(luceneQuery);
	Highlighter highlighter=new Highlighter(formatter, queryScorer);

	@SuppressWarnings("unchecked")
	List<Book> tempresult = fullTextQuery.list();
	for (Book book : tempresult) {
		String highlighterString=null;
		try {
			//高亮name
			highlighterString=highlighter.getBestFragment(analyzer, "name", book.getName());
			if(highlighterString!=null){
				book.setName(highlighterString);
			}
			//高亮authors.name
			Set<Author> authors = book.getAuthors();
			for (Author author : authors) {
				highlighterString=highlighter.getBestFragment(analyzer, "authors.name", author.getName());
				if(highlighterString!=null){
					author.setName(highlighterString);
				}
			}
			//高亮description
			highlighterString=highlighter.getBestFragment(analyzer, "description", book.getDescription());
			if(highlighterString!=null){
				book.setDescription(highlighterString);
			}
		} catch (Exception e) {
		}
		
		books.add(book);
		
		
		System.out.println("书名:"+book.getName()+"\n描述:"+book.getDescription()+"\n出版日期:"+book.getPublicationDate());
		System.out.println("----------------------------------------------------------");
	}
	
	queryResult.setSearchresult(books);
	
	return queryResult;
}
 
Example 11
Source File: BaseDao.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void getHistoryEntries(final Session session, final BaseSearchFilter filter, final Set<Integer> idSet, final Class< ? > clazz,
    final boolean searchStringInHistory)
{
  if (log.isDebugEnabled() == true) {
    log.debug("Searching in " + clazz);
  }
  // First get all history entries matching the filter and the given class.
  final String className = ClassUtils.getShortClassName(clazz);
  if (searchStringInHistory == true) {
    final StringBuffer buf = new StringBuffer();
    buf.append("(+className:").append(className);
    if (filter.getStartTimeOfModification() != null || filter.getStopTimeOfModification() != null) {
      final DateFormat df = new SimpleDateFormat(DateFormats.LUCENE_TIMESTAMP_MINUTE);
      df.setTimeZone(DateHelper.UTC);
      buf.append(" +timestamp:[");
      if (filter.getStartTimeOfModification() != null) {
        buf.append(df.format(filter.getStartTimeOfModification()));
      } else {
        buf.append("000000000000");
      }
      buf.append(" TO ");
      if (filter.getStopTimeOfModification() != null) {
        buf.append(df.format(filter.getStopTimeOfModification()));
      } else {
        buf.append("999999999999");
      }
      buf.append("]");
    }
    if (filter.getModifiedByUserId() != null) {
      buf.append(" +userName:").append(filter.getModifiedByUserId());
    }
    buf.append(") AND (");
    final String searchString = buf.toString() + modifySearchString(filter.getSearchString()) + ")";
    try {
      final FullTextSession fullTextSession = Search.getFullTextSession(getSession());
      final org.apache.lucene.search.Query query = createFullTextQuery(HISTORY_SEARCH_FIELDS, null, searchString);
      if (query == null) {
        // An error occured:
        return;
      }
      final FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, HistoryEntry.class);
      fullTextQuery.setCacheable(true);
      fullTextQuery.setCacheRegion("historyItemCache");
      fullTextQuery.setProjection("entityId");
      final List<Object[]> result = fullTextQuery.list();
      if (result != null && result.size() > 0) {
        for (final Object[] oa : result) {
          idSet.add((Integer) oa[0]);
        }
      }
    } catch (final Exception ex) {
      final String errorMsg = "Lucene error message: "
          + ex.getMessage()
          + " (for "
          + this.getClass().getSimpleName()
          + ": "
          + searchString
          + ").";
      filter.setErrorMessage(errorMsg);
      log.info(errorMsg);
    }
  } else {
    final Criteria criteria = session.createCriteria(HistoryEntry.class);
    setCacheRegion(criteria);
    criteria.add(Restrictions.eq("className", className));
    if (filter.getStartTimeOfModification() != null && filter.getStopTimeOfModification() != null) {
      criteria.add(Restrictions.between("timestamp", filter.getStartTimeOfModification(), filter.getStopTimeOfModification()));
    } else if (filter.getStartTimeOfModification() != null) {
      criteria.add(Restrictions.ge("timestamp", filter.getStartTimeOfModification()));
    } else if (filter.getStopTimeOfModification() != null) {
      criteria.add(Restrictions.le("timestamp", filter.getStopTimeOfModification()));
    }
    if (filter.getModifiedByUserId() != null) {
      criteria.add(Restrictions.eq("userName", filter.getModifiedByUserId().toString()));
    }
    criteria.setCacheable(true);
    criteria.setCacheRegion("historyItemCache");
    criteria.setProjection(Projections.property("entityId"));
    final List<Integer> idList = criteria.list();
    if (idList != null && idList.size() > 0) {
      for (final Integer id : idList) {
        idSet.add(id);
      }
    }
  }
}