/** * When a @FullTextFilterDef annotation associates this factory class with a given name, and a "FullTextQuery.enableFullTextFilter()" is * called with that name as its input parameter, then this method is used to return a Filter with the actual filtering logic. It is * the @Factory annotation that designates this method as having that responsibility for this factory class. */ @Factory public Filter getFilter() { StringTokenizer tokenzier = new StringTokenizer(deviceName.toLowerCase()); PhraseQuery query = new PhraseQuery(); while(tokenzier.hasMoreTokens()) { // By default, field values were converted to lower-case when indexed by Lucene. So be sure to // convert search terms to lower-case in order to make them match. Term term = new Term("supportedDevices.name", tokenzier.nextToken().toLowerCase()); query.add(term); } Filter filter = new QueryWrapperFilter(query); return new CachingWrapperFilter(filter); }
/** * 全文检索 * @param page 分页对象 * @param query 关键字查询对象 * @param queryFilter 查询过滤对象 * @param sort 排序对象 * @return 分页对象 */ @SuppressWarnings("unchecked") public Page<T> search(Page<T> page, BooleanQuery query, BooleanQuery queryFilter, Sort sort){ // 按关键字查询 FullTextQuery fullTextQuery = getFullTextSession().createFullTextQuery(query, entityClass); // 过滤无效的内容 if(queryFilter != null){ fullTextQuery.setFilter(new CachingWrapperFilter(new QueryWrapperFilter(queryFilter))); } // 按时间排序 if(sort != null){ fullTextQuery.setSort(sort); } // 定义分页 page.setCount(fullTextQuery.getResultSize()); fullTextQuery.setFirstResult(page.getFirstResult()); fullTextQuery.setMaxResults(page.getMaxResults()); // 先从持久化上下文中查找对象,如果没有再从二级缓存中查找 fullTextQuery.initializeObjectsWith(ObjectLookupMethod.SECOND_LEVEL_CACHE, DatabaseRetrievalMethod.QUERY); // 返回结果 page.setList(fullTextQuery.list()); return page; }
@Factory public Filter getFilter() { Query query = new TermQuery( new Term("level", level.toString() ) ); return new CachingWrapperFilter( new QueryWrapperFilter(query) ); }