当前位置:   article > 正文

手动创建索引及全文检索

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


本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/903373
推荐阅读
相关标签
  

闽ICP备14008679号