现在的位置: 首页 > 综合 > 正文

lucene2.4.0测试

2013年08月07日 ⁄ 综合 ⁄ 共 5069字 ⁄ 字号 评论关闭

本文建立一个基本的Lucene例子,测试一下。

注释我都加在代码里面了。

luceneIndex.java

功能:建立索引

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.Reader;
  6. import java.util.Date;
  7. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  8. import org.apache.lucene.document.Document;
  9. import org.apache.lucene.document.Field;
  10. import org.apache.lucene.index.IndexWriter;
  11. import org.apache.lucene.index.IndexWriter.MaxFieldLength;
  12. public class LuceneIndex {
  13.     public static void main(String[] args) throws Exception {
  14.         // 声明一个对象
  15.         LuceneIndex indexer = new LuceneIndex();
  16.         // 建立索引
  17.         Date start = new Date();
  18.         indexer.writeToIndex();
  19.         Date end = new Date();
  20.         
  21.         System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");
  22.         
  23.         //这一步很关键,往往缺少这一步,而使得存放索引的目录中缺少文件
  24.         indexer.close();
  25.         
  26.     }
  27.     public LuceneIndex() {
  28.         try {
  29.             writer = new IndexWriter(Constants.INDEX_STORE_PATH,
  30.                     new StandardAnalyzer(), true,new MaxFieldLength(999999999));//大小:999999999
  31.         } catch (Exception e) {
  32.             e.printStackTrace();
  33.         }
  34.     }
  35.     // 索引器
  36.     private IndexWriter writer = null;
  37.     
  38.     // 将要建立索引的文件构造成一个Document对象,并添加一个域"content"
  39.     private Document getDocument(File f) throws Exception {
  40.         Document doc = new Document();
  41.         FileInputStream is = new FileInputStream(f);
  42.         
  43.         Reader reader = new BufferedReader(new InputStreamReader(is));
  44.         
  45.         doc.add(new Field("contents", reader));
  46.         doc.add(new Field("path", f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
  47.         
  48.         return doc;
  49.         
  50.     }
  51.     public void writeToIndex() throws Exception {
  52.         File folder = new File(Constants.INDEX_FILE_PATH);
  53.         if (folder.isDirectory()) {
  54.             String[] files = folder.list();
  55.             for (int i = 0; i < files.length; i++) {
  56.                 File file = new File(folder, files[i]);
  57.                 Document doc = getDocument(file);
  58.                 System.out.println("正在建立索引 : " + file + "");
  59.                 writer.addDocument(doc);
  60.             }
  61.         }
  62.     }
  63.     public void close() throws Exception {
  64.         writer.close();
  65.     }
  66. }

 

LuceneSearch.java

//测试

 

  1. import java.util.Date;
  2. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  3. import org.apache.lucene.document.Document;
  4. import org.apache.lucene.queryParser.MultiFieldQueryParser;
  5. import org.apache.lucene.search.BooleanClause;
  6. import org.apache.lucene.search.IndexSearcher;
  7. import org.apache.lucene.search.Query;
  8. import org.apache.lucene.search.ScoreDoc;
  9. import org.apache.lucene.search.TopDocCollector;
  10. public class LuceneSearch {
  11.     
  12.     public static void main(String[] args) throws Exception {
  13.         LuceneSearch test = new LuceneSearch();
  14.         ScoreDoc[] h = null;
  15.         
  16.         h = test.search("关键字");
  17.         
  18.         
  19.         test.printResult(h);
  20.         
  21.         h = null;
  22.     
  23.         h = test.search("adddd");
  24.         test.printResult(h);
  25.     }
  26.     public LuceneSearch() {
  27.         try {
  28.             searcher = new IndexSearcher(Constants.INDEX_STORE_PATH);
  29.         } catch (Exception e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.     // 声明一个IndexSearcher对象
  34.     private IndexSearcher searcher = null;
  35.     public final ScoreDoc[] search(String keyword) {
  36.         System.out.println("正在检索关键字 : " + keyword);
  37.         try {
  38.             BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
  39.             TopDocCollector collector = new TopDocCollector(10); // 启用这个
  40.             Query query = MultiFieldQueryParser.parse(new String[]{keyword}, new String[] { "contents" }, new StandardAnalyzer() );
  41.             //Query query = MultiFieldQueryParser.parse(keyword, new String[] { "contents" }, clauses,new StandardAnalyzer() );
  42.             Date start = new Date();
  43.             searcher.search(query, collector); // 作为参数
  44.             ScoreDoc[] hits = collector.topDocs().scoreDocs; // 拿到结果
  45.     
  46.             Date end = new Date();
  47.             System.out.println("检索完成,用时" + (end.getTime() - start.getTime()) + "毫秒");
  48.             return hits;
  49.         } catch (Exception e) {
  50.             e.printStackTrace();
  51.             return null;
  52.         }
  53.     }
  54.     public void printResult(ScoreDoc[] h) {
  55.         if (h == null || h.length == 0) {
  56.             System.out.println("对不起,没有找到您要的结果。");
  57.         } 
  58.         else 
  59.         {
  60.             for (int i = 0; i < h.length; i++) {
  61.                 try {
  62.                     int num = h[i].doc; // 一个内部编号
  63.                     Document doc = searcher.doc(num); // 通过编号,拿到文档
  64.                     System.out.print("这是第" + i + "个检索到的结果,文档编号为:" + num + "文件名为:");
  65.                     System.out.println("PATH路径:" + doc.get("path"));
  66.                     //doc.setBoost(0.7f);
  67.                     System.out.println("该文档的得分是" + doc.getBoost());
  68.                     System.out.println("CONTENTS内容:" + doc.get("contents"));
  69.                 } catch (Exception e) {
  70.                     e.printStackTrace();
  71.                 }
  72.             }
  73.         }
  74.         System.out.println("--------------------------");
  75.     }
  76. }

抱歉!评论已关闭.