lucene4.3 学习1---- lucene搜索文件例子

package com.lsh;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.queryparser.classic.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;
import org.apache.lucene.util.Version;

public class Test1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
//		createIndex();
		
//		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
//		QueryParser parser = new QueryParser(Version.LUCENE_43, "fileName", analyzer);
//		search(parser.parse("CHANGES_1.6"));
	}

	/**创建索引
	 * @throws Exception
	 */
	private static void createIndex() throws Exception {
		System.out.println("正在创建索引");
		File file = new File("E:\\开源项目");
		File docFile = new File("file/indexs");//索引目录
		Directory dir = FSDirectory.open(docFile);
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
		IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43, analyzer);
		iwc.setOpenMode(OpenMode.CREATE);
		IndexWriter indexWriter = new IndexWriter(dir, iwc);
		indexDocs(indexWriter,file);
		indexWriter.commit();
		indexWriter.close();
		System.out.println("创建索引完成");
	}
	private static void indexDocs(IndexWriter indexWriter ,File file) throws Exception {
		if(file.isDirectory()){
			File[] files = file.listFiles();
			for (File file2 : files) {
				Document doc  = getDocument(file2);
				indexWriter.addDocument(doc);
				indexDocs(indexWriter,file2);
			}
		}
		Document doc  = getDocument(file);
		indexWriter.addDocument(doc);
	}
	private static Document getDocument(File file) throws Exception {
		String path = file.getAbsolutePath();
		String fileName = path.substring(path.lastIndexOf("\\")+1);
		Document document = new Document();
		StringField pathField = new StringField("path", path, Store.YES); 
		TextField nameField = new TextField("fileName", fileName, Store.YES); 
		document.add(pathField);
		document.add(nameField);
		return document;
	}
		
	/**查找关键字
	 * @param query
	 * @throws Exception
	 */
	private static void search(Query query) throws Exception {
		IndexReader reader = DirectoryReader.open(FSDirectory.open(new File("file/indexs")));
	    IndexSearcher searcher = new IndexSearcher(reader);
        TopDocs topDocs = searcher.search(query, 1000);  
        ScoreDoc[] scoreDocs = topDocs.scoreDocs;  
        System.out.println("结果数据有"+scoreDocs.length+"条记录");
        for (int i = 0; i < scoreDocs.length; i++) {  
            int index = scoreDocs[i].doc;  
            Document document = searcher.doc(index);  
            System.out.println("path:"+document.get("path")); 
            System.out.println("fileName:"+document.get("fileName"));
            System.out.println("-----------------------------------");
        }  
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值