为什么80%的码农都做不了架构师?>>>
import java.io.File;
import java.io.FileReader;
import java.util.Date;
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.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.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
/**
* 简单的搜索
*
* 给指定的文件夹下的文件建立索引 ,为指定的的文件创建索引 基本搜索功能
*
* @author dengyang
*
*/
public class TestIndex2 {
// static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);//内置分词器
static Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
public static void createIndexByPath(File indexDir, File dataDir) {
try {
Directory dir = new SimpleFSDirectory(indexDir);
IndexWriterConfig iwConf = new IndexWriterConfig(Version.LUCENE_35,analyzer);
iwConf.setOpenMode(OpenMode.CREATE);
IndexWriter indexWriter = new IndexWriter(dir, iwConf);
long startTime = new Date().getTime();
File[] files = dataDir.listFiles();
for (int i = 0; i
// System.out.println(files[i].getCanonicalPath());
// System.out.println(files[i].getName());
Document document = new Document();
document.add(new Field("path", files[i].getCanonicalPath(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("filename", files[i].getName(),Field.Store.YES, Field.Index.ANALYZED));
document.add(new Field("contents", new FileReader(files[i])));
indexWriter.addDocument(document);
}
}
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("创建索引功耗时:" + (endTime - startTime) / 1000 + "s");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void searchIndexByString(String searchStr, String fileName,File indexDir) {
try {
// fileName:根据某个域搜索 new MultiFieldQueryParser 可以用多个域
QueryParser queryParser = new QueryParser(Version.LUCENE_35,fileName, analyzer);
Query query = queryParser.parse(searchStr);
System.out.println("query = "+query);
IndexReader reader = IndexReader.open(new SimpleFSDirectory(indexDir));
IndexSearcher searcher = new IndexSearcher(reader);
long startTime = new Date().getTime();
TopDocs topDocs = searcher.search(query, 10);//默认的搜索方法
// searcher.setDefaultFieldSortScoring(true, false);//评分
// TopDocs topDocs = searcher.search(query, null, 20, Sort.RELEVANCE);//搜索排序 , 不需要过滤
// Filter filter = new QueryWrapperFilter(new TermQuery(new Term("filename", "stylesheet")));
// TopDocs topDocs = searcher.search(query, filter, 20, Sort.RELEVANCE);//搜索排序 , 过滤
System.out.println("共有文件:" + topDocs.totalHits);
for (int i = 0; i
System.out.println("=" + i);
System.out.println("path=" + document.get("path"));
System.out.println("filename=" + document.get("filename"));
}
reader.close();
searcher.close();
long endTime = new Date().getTime();
System.out.println("搜索功耗时:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
File indexDir = new File("D:\\luceneIndex");
File dataDir = new File("F:\\面试与就业");
createIndexByPath(indexDir, dataDir);
// searchIndexByString("面试与就业", "path", indexDir);
searchIndexByString("面试","filename",indexDir);
}
}