---
title: "初始lucene"
author: "xusulong"
date: "2009-07-31T19:32:11Z"
tags: []
slug: "lucene"
draft: false
lastmod: "2009-07-31T19:55:48Z"
id: 437298
url: "https://xusulong.iteye.com/blog/437298"
---

刚开始接触Lucene，看到好多推荐[Lucene in action](http://swe.web.cs.unibo.it/twiki/pub/SearchingUniBo/ApplicazioniVerticali/luceneinaction.pdf)  
虽然代码用的是lucene 1，但原理讲的很清楚，稍作修改即可。

下面给出lucene最简单的一个应用实例，从书上修改而来  
Lucene in action 中用的为Lucene 1，我用lucene 2来实现了Lucene in action 第一章 初始Lucene

 

# 索引过程:

**核心类：**  
IndexWriter, Directory, Analyzer, Document, Field

**Indexer如下**

```
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

/**
 * @author xusulong
 *
 */
public class Indexer {
	public static void main(String[] args) throws Exception {
		File indexDir = new File("D:\\index");
		File dataDir = new File("D:\\lucene-2.4.1");
		
		long start = new Date().getTime();
		int numIndexed = index(indexDir, dataDir);
		long end = new Date().getTime();
		
		System.out.println("Indexing " + numIndexed + " files took "
				+ (end - start) + " milliseconds");
	}
	
	public static int index(File indexDir, File dataDir) throws IOException{
		if(!dataDir.exists() || !dataDir.isDirectory()) {
			throw new IOException(dataDir + " does not exixt or is not a diretory");
		}
		//创建lucene索引
		IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
		writer.setUseCompoundFile(false);
		
		indexDirectory(writer, dataDir);
		
		int numIndexed = writer.maxDoc();
		writer.optimize();
		writer.close();		//关闭索引
		return numIndexed;
	}
	
	private static void indexDirectory(IndexWriter writer, File dir) throws IOException{
		File[] files = dir.listFiles();
		
		for( int i = 0; i < files.length; i ++) {
			File f = files[i];
			if(f.isDirectory()) {
				indexDirectory(writer, f);//遍历
			} else if(f.getName().toLowerCase().endsWith(".txt")) {
				indexFile(writer, f);
			}
		}
	}
	
	private static void indexFile(IndexWriter writer, File f) throws IOException{
		if(f.isHidden() || !f.exists() || !f.canRead()) {
			return;
		}
		
		System.out.println("Indexing " + f.getCanonicalPath());
		Document doc = new Document();
		//索引文件中的内容
		doc.add(new Field("contents", new FileReader(f)));
		//索引文件名
		doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
		//将Document对象添加到lucene的索引中去
		writer.addDocument(doc);
	}
}
```

 

# 搜索过程：

**核心类：**  
IndexSearcher, Term, Query, QueryParser, TopDocs, ScoreDoc

**Searcher如下**

```
import java.io.File;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * @author xusulong
 * 
 */
public class Searcher {
	public static void main(String[] args) throws Exception {
		File indexDir = new File("D:\\index");
		String q = "lucene";

		if (!indexDir.exists() || !indexDir.isDirectory()) {
			throw new Exception(indexDir
					+ " does not exixt or is not a diretory");
		}

		search(indexDir, q);
	}

	public static void search(File indexDir, String q) throws Exception {
		// 打开索引
		Directory fsDir = FSDirectory.getDirectory(indexDir);
		// 对查询的字符串进行解析
		IndexSearcher is = new IndexSearcher(fsDir);
		QueryParser parser = new QueryParser("contents", new StandardAnalyzer());
		Query query = parser.parse(q);
		long start = new Date().getTime();
		// 搜索索引
		TopDocs topDocs = is.search(query, 100);
		ScoreDoc[] hits = topDocs.scoreDocs;

		long end = new Date().getTime();
		System.out.println("Found " + hits.length + " document(s) (in "
				+ (end - start) + " milliseconds) that mactched query '" + q
				+ "':");
		for (int i = 0; i < hits.length; i++) {
			int docId = hits[i].doc;
			// 检索匹配到的文件，显示文件名
			Document doc = is.doc(docId);
			System.out.println(docId + ": " + doc.get("filename"));
		}
	}
}
```

 

# 结果

两个程序都在我的机器上运行成功(windows xp)，结果如下：

**Indexer:**  
Indexing D:\\lucene-2.4.1\\BUILD.txt  
Indexing D:\\lucene-2.4.1\\CHANGES.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\java\\org\\apache\\lucene\\analysis\\nl\\stems.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\java\\org\\apache\\lucene\\analysis\\nl\\words.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\de\\data.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\ru\\stemsUnicode.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\ru\\test1251.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\ru\\testKOI8.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\ru\\testUnicode.txt  
Indexing D:\\lucene-2.4.1\\contrib\\analyzers\\src\\test\\org\\apache\\lucene\\analysis\\ru\\wordsUnicode.txt  
Indexing D:\\lucene-2.4.1\\contrib\\ant\\src\\test\\org\\apache\\lucene\\ant\\test.txt  
Indexing D:\\lucene-2.4.1\\contrib\\benchmark\\CHANGES.txt  
Indexing D:\\lucene-2.4.1\\contrib\\benchmark\\src\\test\\org\\apache\\lucene\\benchmark\\quality\\trecQRels.txt  
Indexing D:\\lucene-2.4.1\\contrib\\benchmark\\src\\test\\org\\apache\\lucene\\benchmark\\quality\\trecTopics.txt  
Indexing D:\\lucene-2.4.1\\contrib\\CHANGES.txt  
Indexing D:\\lucene-2.4.1\\contrib\\memory\\src\\test\\org\\apache\\lucene\\index\\memory\\testqueries.txt  
Indexing D:\\lucene-2.4.1\\contrib\\memory\\src\\test\\org\\apache\\lucene\\index\\memory\\testqueries2.txt  
Indexing D:\\lucene-2.4.1\\contrib\\miscellaneous\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\queries\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\similarity\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\snowball\\LICENSE.txt  
Indexing D:\\lucene-2.4.1\\contrib\\snowball\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\snowball\\SNOWBALL-LICENSE.txt  
Indexing D:\\lucene-2.4.1\\contrib\\surround\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\wordnet\\README.txt  
Indexing D:\\lucene-2.4.1\\contrib\\xml-query-parser\\src\\test\\org\\apache\\lucene\\xmlparser\\reuters21578.txt  
Indexing D:\\lucene-2.4.1\\docs\\skin\\images\\README.txt  
Indexing D:\\lucene-2.4.1\\docs\\skin\\note.txt  
Indexing D:\\lucene-2.4.1\\LICENSE.txt  
Indexing D:\\lucene-2.4.1\\NOTICE.txt  
Indexing D:\\lucene-2.4.1\\README.txt  
Indexing D:\\lucene-2.4.1\\src\\jsp\\README.txt  
Indexing 32 files took 657 milliseconds

**Searcher:**

Found 14 document(s) (in 15 milliseconds) that mactched query 'lucene':  
30: D:\\lucene-2.4.1\\README.txt  
24: D:\\lucene-2.4.1\\contrib\\wordnet\\README.txt  
17: D:\\lucene-2.4.1\\contrib\\miscellaneous\\README.txt  
19: D:\\lucene-2.4.1\\contrib\\similarity\\README.txt  
21: D:\\lucene-2.4.1\\contrib\\snowball\\README.txt  
31: D:\\lucene-2.4.1\\src\\jsp\\README.txt  
0: D:\\lucene-2.4.1\\BUILD.txt  
29: D:\\lucene-2.4.1\\NOTICE.txt  
23: D:\\lucene-2.4.1\\contrib\\surround\\README.txt  
11: D:\\lucene-2.4.1\\contrib\\benchmark\\CHANGES.txt  
14: D:\\lucene-2.4.1\\contrib\\CHANGES.txt  
18: D:\\lucene-2.4.1\\contrib\\queries\\README.txt  
1: D:\\lucene-2.4.1\\CHANGES.txt  
28: D:\\lucene-2.4.1\\LICENSE.txt