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("-----------------------------------");
}
}
}
lucene4.3 学习1---- lucene搜索文件例子
最新推荐文章于 2019-10-14 17:07:52 发布