lucene入门实例一(写索引)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6


copy一个 lucene in action 的入门实例代码:

 

 

 

import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Index {
	public static void main(String[] args) throws Exception {
		String indexDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene\\index";//存储lucene索引的文件夹
		String dataDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene";//lucene源文件
		
		long start = System.currentTimeMillis();
		Index indexer = new Index(indexDir);//初始化indexWriter
		int numIndexed;
		try{
			numIndexed = indexer.index(dataDir, new TextFilesFilter());//创建索引
		}finally{
			indexer.close();
		}
		
		long end = System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	private IndexWriter writer;
	
	public Index(String indexDir) throws IOException{
		Directory dir = FSDirectory.open(new File(indexDir));//创建索引文件夹
		//创建索引writer 也可以根据IndexWriterConfig创建
		writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
	}
	
	public void close() throws IOException{
		writer.close();
	}
	
	public int index(String dataDir,FileFilter filter) throws Exception{
		File[] files = new File(dataDir).listFiles();//列出源文件
		
		for (File f : files) {
			if(!f.isDirectory() && filter.accept(f)){
				indexFile(f);//遍历源文件,创建索引
			}
		}
		return writer.numDocs();
	}
	
	public static class TextFilesFilter implements FileFilter{
		public boolean accept(File path){
			return path.getName().toLowerCase().endsWith(".txt");
		}
	}
	
	protected Document getDocument(File f) throws Exception{
		Document doc = new Document();
		doc.add(new Field("contents",new FileReader(f)));//内容
		doc.add(new Field("filename",f.getName(),
				Field.Store.YES,Field.Index.NOT_ANALYZED));//文件名
		doc.add(new Field("fullpath",f.getCanonicalPath(),
				Field.Store.YES,Field.Index.NOT_ANALYZED)); //文件全路径
		return doc;
	}
	
	private void indexFile(File f) throws Exception{
		Document doc = getDocument(f);//创建document
		writer.addDocument(doc);//添加document到索引
	}
	
	
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6