import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.ParseException;
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.TermQuery;
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 IndexFile {
String indexFile = null;
String sourceDir = null;
public IndexFile(String sourceDir, String indexFile){
this.sourceDir = sourceDir;
this.indexFile = indexFile;
}
public void index() throws Exception {
// RAMDirectory ramdirectory = new RAMDirectory(); //创建内存索引对象
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile)); //创建磁盘目录对象
Analyzer analyzer = new StandardAnalyzer(); //创建分词器
IndexWriterConfig writerConfig = new IndexWriterConfig(analyzer); //创建索引写入对象控制
IndexWriter writer = new IndexWriter(directory, writerConfig); //创建写入对象
this.writeDocuments(writer); //将文档写入对象
// directory.close();
}
public void writeDocuments(IndexWriter writer) throws Exception{
File directory = new File(sourceDir);
File[] fileList = directory.listFiles();
for(int i = 0 ; i < fileList.length; i++){
String title = fileList[i].getName();
String content= this.readFile(fileList[i]);
System.out.println(content);
System.out.println(title);
Document doc = new Document();
doc.add(new Field("name", title, TextField.TYPE_STORED));
doc.add(new Field("address", this.indexFile, TextField.TYPE_STORED));
doc.add(new Field("thing", content, TextField.TYPE_STORED));
writer.addDocument(doc);
System.out.println("add one document");
}
writer.close();
}
public String readFile(File file) throws Exception{
BufferedReader br = null;
String content = null;
try{
br = new BufferedReader(new FileReader(file));
String line ;
while ( (line = br.readLine()) != null){
content += line;
}
}
finally{
if(br != null ){
br.close();
}
}
return content;
}
public void query(String queryStr) throws IOException, ParseException{
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath(this.indexFile));
DirectoryReader directoryReader = DirectoryReader.open(directory);
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
Analyzer analyzer = new StandardAnalyzer();
QueryParser qp = new QueryParser("thing", analyzer);
qp.setDefaultOperator(QueryParser.AND_OPERATOR);
Query query = qp.parse(queryStr);
// Term term = new Term("name", queryStr);
// Query query = new TermQuery(term);
TopDocs topDocs = indexSearcher.search(query, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println( "检索到" + topDocs.totalHits + "条记录。");
for( ScoreDoc scoreDoc : scoreDocs){
Document document = indexSearcher.doc(scoreDoc.doc);
System.out.println("title:" + document.getField("name").stringValue());
System.out.println("content:" + document.getField("thing").stringValue());
}
directory.close();
}
public static void main(String[] agrs) throws Exception{
String indexFileName = "/home/quincy1994/QA/LuceneIndex/data";
String sourceDir = "./MovieList";
IndexFile indexFile = new IndexFile(sourceDir, indexFileName);
// indexFile.index();
System.out.println("nice!");
String queryStr = "大城小事讲的是什么";
indexFile.query(queryStr);
}
}
lucene的代码示范
最新推荐文章于 2023-04-19 17:18:00 发布