使用lucene-core-3.1.0.jar
一 創建索引
- public class Indexer {
- public static void main(String[] args) throws IOException {
- // 保存索引文件的地方
- String indexDir = "F://indexDir";
- // 將要搜索TXT文件的地方
- String dateDir = "F://dateDir";
- IndexWriter indexWriter = null;
- // 創建Directory對象
- Directory dir = new SimpleFSDirectory(new File(indexDir));
- // 創建IndexWriter對象,第一個參數是Directory,第二個是分詞器,第三個表示是否是創建,如果爲false爲在此基礎上面修改,第四表示表示分詞的最大值,比如說new
- // MaxFieldLength(2),就表示兩個字一分,一般用IndexWriter.MaxFieldLength.LIMITED
- indexWriter = new IndexWriter(dir, new StandardAnalyzer(
- Version.LUCENE_30), true, IndexWriter.MaxFieldLength.UNLIMITED);
- File[] files = new File(dateDir).listFiles();
- for (int i = 0; i < files.length; i++) {
- Document doc = new Document();
- // 創建Field對象,並放入doc對象中
- doc.add(new Field("contents", new FileReader(files[i])));
- doc.add(new Field("filename", files[i].getName(), Field.Store.YES,
- Field.Index.NOT_ANALYZED));
- doc.add(new Field("indexDate", DateTools.dateToString(new Date(),
- DateTools.Resolution.DAY), Field.Store.YES,
- Field.Index.NOT_ANALYZED));
- // 寫入IndexWriter
- indexWriter.addDocument(doc);
- }
- // 查看IndexWriter裏面有多少個索引
- System.out.println("numDocs/t" + indexWriter.numDocs());
- indexWriter.close();
- }
- }
2 索引文件
- public class Seacher {
- public static void main(String[] args) throws IOException, ParseException {
- // 保存索引文件的地方
- String indexDir = "F://indexDir";
- Directory dir = new SimpleFSDirectory(new File(indexDir));
- // 創建 IndexSearcher對象,相比IndexWriter對象,這個參數就要提供一個索引的目錄就行了
- IndexSearcher indexSearch = new IndexSearcher(dir);
- // 創建QueryParser對象,第一個參數表示Lucene的版本,第二個表示搜索Field的字段,第三個表示搜索使用分詞器
- QueryParser queryParser = new QueryParser(Version.LUCENE_30,
- "contents", new StandardAnalyzer(Version.LUCENE_30));
- // 生成Query對象
- Query query = queryParser.parse("pause");
- // 搜索結果 TopDocs裏面有scoreDocs[]數組,裏面保存着索引值
- TopDocs hits = indexSearch.search(query, 3);
- // hits.totalHits表示一共搜到多少個
- System.out.println("找到了" + hits.totalHits + "個");
- // 循環hits.scoreDocs數據,並使用indexSearch.doc方法把Document還原,再拿出對應的字段的值
- for (int i = 0; i < hits.scoreDocs.length; i++) {
- ScoreDoc sdoc = hits.scoreDocs[i];
- Document doc = indexSearch.doc(sdoc.doc);
- System.out.println(doc.get("filename") + "/t"
- + hits.scoreDocs[i].score);
- }
- indexSearch.close();
- }
- }