D:\lucene\example下有a.txt、b.txt和c.txt三个文件,分别写入“hello java”、“hello lucene”和“hello elasticsearch”。
package com.ant.jdk8.lucene7;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.*;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.RAMDirectory;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class LuceneIndex {
public static void main(String[] args) throws IOException, ParseException {
LuceneIndex luceneIndex = new LuceneIndex();
String indexPath = "D:\\lucene\\index";
String filePath = "D:\\lucene\\example";
luceneIndex.index(indexPath,filePath).search(indexPath);
}
public LuceneIndex index(String indexPath,String filePath) throws IOException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
//Directory directory = new RAMDirectory();
IndexWriterConfig iwc = new IndexWriterConfig(new StandardAnalyzer());
//iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
IndexWriter writer = new IndexWriter(directory,iwc);
Document doc = null;
DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(filePath));
for(Path path : paths){
File file = path.toFile();
doc = new Document();
doc.add(new TextField("content",new FileReader(file)));
doc.add(new StringField("filename",file.getName(), Field.Store.YES));
doc.add(new StringField("path",file.getAbsolutePath(),Field.Store.YES));
writer.addDocument(doc);
}
writer.close();
return this;
}
public void search(String indexPath) throws IOException {
Directory directory = FSDirectory.open(Paths.get(indexPath));
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Query query = new TermQuery(new Term("content","java"));
TopDocs tds = searcher.search(query,10);
ScoreDoc[] scoreDocs = tds.scoreDocs;
for(ScoreDoc sd : scoreDocs){
Document doc = searcher.doc(sd.doc);
System.out.println(doc.get("filename")+"--->"+doc.get("path"));
}
}
}