使用Lucene2.4时的一些注意点(新版本的修改点)

Lucene2.4版本已经在很多地方有了修改,可能是为了迎接Lucene3.0的到来所做的调整吧。
以前写的Blog(http://blog.csdn.net/kunshan_shenbin/archive/2008/06/02/2503388.aspx)在2.3.2版本下测试通过,但在2.4下必须修改。

代码如下:

Indexer.java

  1. package com.lucene.index;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  6. import org.apache.lucene.document.Document;
  7. import org.apache.lucene.document.Field;
  8. import org.apache.lucene.index.IndexWriter;
  9. public class Indexer {
  10.     public static void main(String[] args) throws IOException {
  11.         File indexDir = new File("C://test//index");
  12.         File dataDir = new File("C://test//data");
  13.         int numIndexed = index(indexDir, dataDir);
  14.         System.out.println(numIndexed);
  15.     }
  16.     public static int index(File indexDir, File dataDir) throws IOException {
  17.         if (!indexDir.exists() || !dataDir.isDirectory()) {
  18.             throw new IOException();
  19.         }
  20.         IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
  21.         writer.setUseCompoundFile(false);
  22.         indexDirectory(writer, dataDir);
  23.         int numIndexed = writer.maxDoc();
  24.         writer.optimize();
  25.         writer.close();
  26.         return numIndexed;
  27.     }
  28.     private static void indexDirectory(IndexWriter writer, File dir)
  29.             throws IOException {
  30.         File[] files = dir.listFiles();
  31.         for (int i = 0; i < files.length; i++) {
  32.             File f = files[i];
  33.             if (f.isDirectory()) {
  34.                 indexDirectory(writer, f);
  35.             } else if (f.getName().toLowerCase().endsWith(".txt")) {
  36.                 indexFile(writer, f);
  37.             }
  38.         }
  39.     }
  40.     public static void indexFile(IndexWriter writer, File f) throws IOException {
  41.         if (f.isHidden() || !f.exists() || !f.canRead()) {
  42.             return;
  43.         }
  44.         System.out.println("Indexing " + f.getCanonicalPath());
  45.         Document doc = new Document();
  46.         doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES,
  47.                 Field.Index.NOT_ANALYZED));
  48.         doc.add(new Field("contents"new FileReader(f)));
  49.         writer.addDocument(doc);
  50.     }
  51. }

Searcher.java

  1. package com.lucene.search;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.queryParser.QueryParser;
  7. import org.apache.lucene.search.IndexSearcher;
  8. import org.apache.lucene.search.Query;
  9. import org.apache.lucene.search.ScoreDoc;
  10. import org.apache.lucene.search.TopDocs;
  11. import org.apache.lucene.store.Directory;
  12. import org.apache.lucene.store.FSDirectory;
  13. public class Searcher {
  14.     public static void main(String[] args) throws Exception {
  15.         File indexDir = new File("C://test//index");
  16.         String q = "111";
  17.         if (!indexDir.exists() || !indexDir.isDirectory()) {
  18.             throw new IOException();
  19.         }
  20.         search(indexDir, q);
  21.     }
  22.     public static void search(File indexDir, String q) throws Exception {
  23.         Directory fsDir = FSDirectory.getDirectory(indexDir);
  24.         IndexSearcher searcher = new IndexSearcher(fsDir);
  25.         QueryParser parser = new QueryParser("contents"new StandardAnalyzer());
  26.         Query query = parser.parse(q);
  27.         
  28.         TopDocs topDocs = searcher.search(query, 100);//100是显示队列的Size
  29.         ScoreDoc[] hits = topDocs.scoreDocs;
  30.         System.out.println("共有" + searcher.maxDoc() + "条索引,命中" + hits.length + "条");
  31.         for (int i = 0; i < hits.length; i++) {
  32.             int DocId = hits[i].doc;
  33.             Document document = searcher.doc(DocId);
  34.             System.out.println(DocId + ":" + document.get("filename"));
  35.         }
  36.     }
  37. }
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值