构造field--》被add到document--》被add到IndexWriter
指定查找路径(即从哪得到数据源)
把数据源的数据读出来构造field对象
指定索引存放路径
构建IndexWriter对象需要索引存放路径
查询
从索引存放路径查找
个人理解过程:就是把数据源做成索引文件,然后在索引文件里找关键词
参考一位网友的百度空间,代码如下:
package com.cstp.lucene;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
public class TestFileIndexer {
public static void main(String[] args) throws Exception {
/* 指明要索引的文件夹的位置 */
File fileDir = new File("c://dataDir");
/* 这里放索引文件的位置 */
File indexDir = new File("c://index");
Analyzer luceneAnalyzer = new StandardAnalyzer();
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,
true, IndexWriter.MaxFieldLength.UNLIMITED);
File[] textFiles = fileDir.listFiles();
long startTime = new Date().getTime();
// 增加document到索引去
for (int i = 0; i < textFiles.length; i++) {
if (textFiles[i].isFile()
&& textFiles[i].getName().endsWith(".txt")) {
System.out.println("File " + textFiles[i].getCanonicalPath()
+ "正在被索引....");
//读取文件
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);
document.add(FieldPath);
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
// optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out.println("这花费了" + (endTime - startTime) + " 毫秒来把文档增加到索引里面去!"
+ fileDir.getPath());
// 进行查找
Analyzer analyzer = new StandardAnalyzer();
IndexSearcher searcher = new IndexSearcher("c://index");
ScoreDoc[] hits = null;
String queryString = "中国";
Query query = null;
try {
//从"body"这个field里找
query = new QueryParser("body", analyzer).parse(queryString);
} catch (ParseException e) {
}
if (searcher != null) {
hits = searcher.search(query, searcher.maxDoc()).scoreDocs;
if (hits.length > 0) {
System.out.println("找到:" + hits.length + " 个结果!");
}
Document doc;
for (int i = 0; i < hits.length; i++) {
doc = searcher.doc(hits[i].doc);
System.out.println(doc.get("body"));
}
}
}
//读取文件,以字符串返回
public static String FileReaderAll(String FileName, String charset)
throws IOException {
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;
}
}