建立一个简单索引的例子

/*IndexFiles.java*/
import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;


public class IndexFiles {
	static final File INDEX_DIR= new File("index");
	
	public static void main(String[] args)
	{
		if(INDEX_DIR.exists())
		{
			System.out.println("Can not save to "+INDEX_DIR+" directory, please delete it first!");
			System.exit(1);
		}
		final File docDir=new File("E:\\原桌面");// 要建立索引的目录
		if(!docDir.exists()||!docDir.canRead())
		{
			System.out.println("Document directory "+docDir.getAbsolutePath()+" does not exist or not readable,please check the path");
			System.exit(1);
		}
		
		Date start= new Date();
		
		try {
			IndexWriter writer=new IndexWriter(INDEX_DIR,new StandardAnalyzer(),true);//构造了一个IndexWriter索引器
			writer.setUseCompoundFile(false);//文件不合并
			System.out.println("Indexing to directory "+INDEX_DIR+"……");
			indexDocs(writer,docDir);
			System.out.println("Optimizing……");
			writer.optimize();
			writer.close();
			Date end=new Date();
			System.out.println(end.getTime()-start.getTime()+"tatal milliseconds");
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static void indexDocs(IndexWriter writer, File file) {
		// TODO Auto-generated method stub
		if(file.canRead())
		{
			if(file.isDirectory())// 如果file是一个目录(该目录下面可能有文件、目录文件、空文件三种情况)
			{
				String[] files=file.list();// 获取file目录下的所有文件(包括目录文件)File对象,放到数组files里
				if(files!=null)
				{
					for(int i=0;i<files.length;i++) // 对files数组里面的File对象递归索引,通过广度遍历
					{
						indexDocs(writer,new File(file,files[i]));
					}
				}
			}
			else if(file.getName().endsWith(".txt"))// 到达叶节点时,说明是一个File,而不是目录,则建立索引
			{
				System.out.println("adding"+file);
				try {
					writer.addDocument(FileDocument.Document(file));// 把Document对象加入索引器IndexWriter对象中
				} catch (CorruptIndexException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
		}
	}
	

}

/*FileDocument.java*/

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class FileDocument {
	public static Document Document(File file)// 构建Document对象
	{
		// 实例化一个Document
		Document doc=new Document();
		// 根据传进来的File file,构造多个Field对象,然后把他们都添加到Document中

	    // 通过file的所在路径构造一个Field对象,并设定该Field对象的一些属性:
	    // “path”是构造的Field的名字,通过该名字可以找到该Field
	    // Field.Store.YES表示存储该Field;Field.Index.UN_TOKENIZED表示不对该Field进行分词,但是对其进行索引,以便检索
		doc.add(new Field("path",file.getPath(),Store.YES,Index.UN_TOKENIZED));
		// 构造一个具有最近修改修改时间信息的Field
		doc.add(new Field("modified",DateTools.timeToString(file.lastModified(), DateTools.Resolution.MINUTE),Store.YES,Index.UN_TOKENIZED));
		try {
			// 构造一个Field,这个Field可以从一个文件流中读取,必须保证由f所构造的文件流是打开的
			doc.add(new Field("contents",new FileReader(file)));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return doc;
	}

}


本文参考:http://www.cnblogs.com/lvpei/articles/1731787.html

当然还有老师的pdf啦,O(∩_∩)O哈哈~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值