1、新建资源文件
2、给资源文件建立索引
package com.sf.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class StandardAnalyzerTest {
public static void main(String[] args) throws Exception {
File fileDir = new File("D:\\source");
File indexDir = new File("D:\\index");
Directory dir = FSDirectory.open(indexDir);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter iw = new IndexWriter(dir, iwc);
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
for (int i = 0; i < textFiles.length; i++) {
if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt"))
{
System.out.println("File"+textFiles[i].getParentFile()+"正在被搜索...");
String temp = fileReaderAll(textFiles[i].getCanonicalPath(),"GBK");//
System.out.println(temp);
Document document = new Document();
Field fieldPath = new Field("path",textFiles[i].getPath(),Field.Store.YES,Field.Index.NO);
Field fieldBody = new Field("body", temp, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(fieldPath);
document.add(fieldBody);
iw.addDocument(document);
}
}
iw.close();
long endTime = new Date().getTime();
System.out.println("这个花费了"+(endTime - startTime) +"毫秒来把文档增加到索引里面"+fileDir.getPath());
}
public static String fileReaderAll(String fileName,String charset) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName),charset));
String line = new String();
String temp = new String();
while((line =reader.readLine()) != null)
{
temp = line;
}
reader.close();
return temp;
}
}
2、检索文件
package com.sf.test;
import java.io.File;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class SearchTest {
public static void main(String[] args) throws Exception {
String index = "D:\\index"; //搜索的索引路径
IndexReader iReader = IndexReader.open(FSDirectory.open(new File(index)));
IndexSearcher is = new IndexSearcher(iReader);
ScoreDoc[] hits = null;
String quertString = "绝对秋香";
Query query = null;
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
QueryParser qp = new QueryParser(Version.LUCENE_36, "body", analyzer);
query = qp.parse(quertString);
if(is != null)
{
TopDocs result = is.search(query, 10);
hits = result.scoreDocs;
if(hits.length > 0)
{
System.out.println("找到"+hits.length+"个结果!");
}
is.close();
}
}
}