手动创建索引及全文检索

  /**  
     * 创建索引  
     * @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;
			}
		});
	}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值