传智播客--itcastbbs(六)


(2009-04-29 20:40:15)
标签:

it

 

Lucene:

1. 举例: Eclipse的帮助;

2. 建立索引:只能为文本类型的数据建立索引
 
 html(去掉标签), pdf等可以使用相应的工具转换为文本;
 
3. 原理:字典
 key ---> recordNum
 ab ---> 5,7,10
 Document;  
  5
  7
  10

4. 使用:

 4.1 建立java Project;
 4.2 添加*.jar;[没有配置文件]目前为2.4版;
  核心;分词器;高亮器;
 4.3 写代码测试;
  FirstTest
   StringindexPath = "c:/luceneDemoIndex/";
   Analyzeranalyzer = new StandardAnalyer();
  
   @Test
   testCreateIndex(){
    Stringtitle   = "xxx";
    Stringcontent  = "yyy";
    
    //1.建立索引    
    MaxFieldLengthmaxFieldLength = MaxFieldLength.LIMITED;
    
    //如果索引库不存在,则创建;
    IndexWriterindexWriter = new IndexWriter(indexPath, analyzer,maxFieldLength); 
    
    
    //文档,字段
    Documentdoc = new Document();
    doc.add(newField("title", title, Store.YES, Index.ANALYZED));
    doc.add(newField("content", content, Store.YES, Index.ANALYZED));
    
    indexWriter.addDocument(doc);
    
    
    //使用完一定要关闭
    indexWriter.close();  
   }
   
   
   voidtestSearch() {
   
    StringqueryString ="document";    
    IndexSearcherindexSearcher = new IndexSearcher(indexPath);
    
    StringdefaultFieldName = "content";
    QueryParserqueryParser = new QueryParser(defaultFieldName, analyzer);
    
    Query query  =queryParser.parse(queryString);   //查询条件
    Filter filter  =null;   //过滤条件
    int nDocs  =100;   //返回匹配的数目
    
    //返回结果
    TopDocstopDocs = indexSearcher.search(query, filter, nDocs);
    System.out.println("共有【"+topDocs.totalHits+"条匹配记录");
    
    List<Document>docs = newArrayList<Document>();
    for( ScoreDoc scoreDoc : topDocs.scoreDocs ) {
     intdocNum = scoreDoc.doc;   //文档在索引库中的编号
    
     Documentdoc = indexSearcher.doc(docNum); // 通过编号取出相应的文档
     
     doc.add(doc);
    }    
    indexSearcher.close();
   

转载于:https://www.cnblogs.com/firecode/archive/2011/11/26/2460933.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值