资源简介
lucene对某目录下txt文件做全文检索源码
代码片段和文件信息
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.codecs.TermVectorsReader;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.Fields;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReaderContext;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.SlowCompositeReaderWrapper;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext;
import org.apache.lucene.index.TermState;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
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.TermStatistics;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class TestIndexReadWrite {
/**
* @param args
* @throws IOException
*/
public static void buildIndex(String fileDirString indexDir) throws IOException{
//fileDir is the directory that contains the text files to be indexed
//indexDir is the directory that hosts Lucene‘s index files
File textFile = new File(fileDir);
File indexFile = new File(indexDir);
Directory dir=FSDirectory.open(indexFile);
//Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_42);
Analyzer luceneAnalyzer = new SimpleAnalyzer(Version.LUCENE_42);
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_42luceneAnalyzer);
IndexWriter indexWriter = new IndexWriter(diriwc);
File[] textFiles = textFile.listFiles();
long startTime = new Date().getTime();
//Add documents to the index
for(int i = 0; i < textFiles.length; i++){
if(textFiles[i].isFile()&& textFiles[i].getName().endsWith(“.txt“)){
System.out.println(“File “ + textFiles[i].getCanonicalPath()
+ “ is being indexed“);
Reader textReader = new FileReader(textFiles[i]);
- 上一篇:java课程设计实验报告
- 下一篇:SurfaceControl.jar
评论
共有 条评论