/** * 创建索引 * @param path */ public void createIndex(String path){ try { IndexWriter writer = new IndexWriter(path,new StandardAnalyzer(),true); Document docA = new Document(); //相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置 //Field.Store 是否覆盖原来的索引文件,而不是重新建一个 Field fieldA = new Field("content","搜索引擎",Field.Store.YES,Field.Index.TOKENIZED); //我们把列(fieldA)加到某一行(docA)中 docA.add(fieldA); docA.add(new Field("title","你好中国",Field.Store.YES,Field.Index.TOKENIZED)); docA.add(new Field("content","欢迎你llying",Field.Store.YES,Field.Index.TOKENIZED)); docA.add(new Field("lastModifyTime","2008-9-17",Field.Store.YES,Field.Index.TOKENIZED)); docA.add(new Field("testCapital","HelloWangzi",Field.Store.YES,Field.Index.TOKENIZED)); docA.add(new Field("testAndOr","test and",Field.Store.YES,Field.Index.TOKENIZED)); Document docB = new Document(); //相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置 Field fieldB = new Field("content","创建索引",Field.Store.YES,Field.Index.TOKENIZED); //我们把列(fieldA)加到某一行(docA)中 docB.add(fieldB); docB.add(new Field("title","你好世界",Field.Store.YES,Field.Index.TOKENIZED)); docB.add(new Field("content","欢迎加入jee高级开发群46176507",Field.Store.YES,Field.Index.TOKENIZED)); docB.add(new Field("lastModifyTime","2008-9-6",Field.Store.YES,Field.Index.TOKENIZED)); docB.add(new Field("testCapital","hellowangZi",Field.Store.YES,Field.Index.TOKENIZED)); docB.add(new Field("testAndOr","test or",Field.Store.YES,Field.Index.TOKENIZED)); writer.addDocument(docA); writer.addDocument(docB); //如果对海量数据进行创建索引的时候,需要对索引进行优化,以便提高速度 writer.optimize(); //跟数据库类似,打开一个连接,使用完后,要关闭它 writer.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public Wrapper searchDigitalBook(final PageExtNative page){ return (Wrapper) this.baseDao.execute(new HibernateCallback(){ public Object doInHibernate(Session session) throws HibernateException, SQLException{ // 将SESSION转换成lucence session,执行全文检索 FullTextSession fullSession = Search.getFullTextSession(session); //Map<String, String> search = page.getMSearch(); StringBuffer searchQuery = new StringBuffer(); Wrapper wrap = null; Query luceneQuery = null; QueryParser parser = new QueryParser("dboSourceName", new StandardAnalyzer()); try { searchQuery.append("( dboSourceName:").append(page.getMSearch().get("contantion"));//书名 searchQuery.append(" OR dboAuthor:").append(page.getMSearch().get("contantion"));//作者 searchQuery.append(" OR dboSummary:").append(page.getMSearch().get("contantion"));//摘要 searchQuery.append(" OR dboKeyword:").append(page.getMSearch().get("contantion"));//关键词 searchQuery.append(") AND ddbId:").append(page.getMSearch().get("ddbId"));//资源库ID luceneQuery = parser.parse(searchQuery.toString()); FullTextQuery query = fullSession.createFullTextQuery(luceneQuery, DigitalBookDetail.class); //总记录数 int size = query.getResultSize(); query.setFirstResult(page.getStart()); query.setMaxResults(page.getLimit()); List<DigitalBookDetail> books = query.list(); wrap = new Wrapper(books,String.valueOf(size)); } catch (ParseException e) { e.printStackTrace(); } return wrap; } }); }