public class HelloLunce {
/**
*建立索引
* @throws IOException
*/
public void buildIndex() throws IOException{
//1:建立索引存放的目录Directory
File path = new File("E:/lucene/index");
//Directory dir = new RAMDirectory();
Directory dir = FSDirectory.open(path);
//2:创建IndexWriter
IndexWriterConfig iwc= new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter iw = new IndexWriter(dir, iwc);
//3:创建Doucument对象
File pathDoc = new File("E:/lucene/document");
Document doc = null;
for(File f : pathDoc.listFiles()){
doc = new Document();
//4:为Doucument添加Filed
doc.add(new Field("content", new FileReader(f)));
doc.add(new Field("name",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",f.getPath(),Field.Store.YES,Index.NOT_ANALYZED));
//5:通过IndexWriter添加文档到索引中
iw.addDocument(doc);
}
iw.close();
}
public void searcher() throws IOException, ParseException{
//1:创建Directory
Directory dir = FSDirectory.open(new File("E:/lucene/index"));
//2:创建IndexReader
IndexReader reader = IndexReader.open(dir);
//3:得到IndexSearcher
IndexSearcher sercher = new IndexSearcher(reader);
//4:得到query
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("lucene");
//5:通过IndexSercher和Query得到TopDoc
TopDocs topDocs = sercher.search(query, 10);
//6:从TopDocs中取出scordoc
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc sd : scoreDocs){
//7:根据scordoc和IndexSercher得到doc
Document doc = sercher.doc(sd.doc);
//8:根据doc的得到具体信息
System.out.println("[name]"+doc.get("name")+"[path]"+doc.get("path"));
}
reader.close();
}
}
/**
*建立索引
* @throws IOException
*/
public void buildIndex() throws IOException{
//1:建立索引存放的目录Directory
File path = new File("E:/lucene/index");
//Directory dir = new RAMDirectory();
Directory dir = FSDirectory.open(path);
//2:创建IndexWriter
IndexWriterConfig iwc= new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
IndexWriter iw = new IndexWriter(dir, iwc);
//3:创建Doucument对象
File pathDoc = new File("E:/lucene/document");
Document doc = null;
for(File f : pathDoc.listFiles()){
doc = new Document();
//4:为Doucument添加Filed
doc.add(new Field("content", new FileReader(f)));
doc.add(new Field("name",f.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
doc.add(new Field("path",f.getPath(),Field.Store.YES,Index.NOT_ANALYZED));
//5:通过IndexWriter添加文档到索引中
iw.addDocument(doc);
}
iw.close();
}
public void searcher() throws IOException, ParseException{
//1:创建Directory
Directory dir = FSDirectory.open(new File("E:/lucene/index"));
//2:创建IndexReader
IndexReader reader = IndexReader.open(dir);
//3:得到IndexSearcher
IndexSearcher sercher = new IndexSearcher(reader);
//4:得到query
QueryParser parser = new QueryParser(Version.LUCENE_35, "content", new StandardAnalyzer(Version.LUCENE_35));
Query query = parser.parse("lucene");
//5:通过IndexSercher和Query得到TopDoc
TopDocs topDocs = sercher.search(query, 10);
//6:从TopDocs中取出scordoc
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc sd : scoreDocs){
//7:根据scordoc和IndexSercher得到doc
Document doc = sercher.doc(sd.doc);
//8:根据doc的得到具体信息
System.out.println("[name]"+doc.get("name")+"[path]"+doc.get("path"));
}
reader.close();
}
}