我也不多说什么了 我就给大家一个 例子 : 然后 去看后面别人总结的吧 (注视写在代码上了)
1、首先 在C盘 创建一个 名字为 s 的 文件夹, 在文件夹下 分别创建 1.txt 2.txt 3.txt 内容 分别是 中华人民共和国
2、然后 创建下面两个类, 然后导入 lucene 2.0 jar 包 路径如下:http://download.csdn.net/detail/u010310183/8060271
3、祝大家好运!!
package snippet;
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.StopAnalyzer;
import org.apache.lucene.analysis.WhitespaceAnalyzer;
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;
/** */ /**
* author lighter date 2006-8-7
*/
public class TextFileIndexer {
public static void main(String[] args) throws Exception {
/**/ /* 指明要索引文件夹的位置,这里是C盘的S文件夹下 */
File fileDir = new File( "c:\\s" );
/**/ /* 这里放索引文件的位置 */
File indexDir = new File( "c:\\index" );
Analyzer luceneAnalyzer = new StandardAnalyzer(); //这里是分词器 , 主要用于分析搜索引擎遇到的各种文本
//System.out.println(luceneAnalyzer);
IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,true ); //它主要是用来将文档加入索引,同时控制索引过程中的一些参数使用。
// System.out.println(indexWriter.getAnalyzer());
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( "body", textFiles[i].getPath(),
Field.Store.YES, Field.Index.NO);
Field FieldBody = new Field( "path", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS); //这里表示 path 被分词索引且存储
document.add(FieldPath); //添加到 document 中
document.add(FieldBody);
indexWriter.addDocument(document);
}
}
// optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out
.println(" 这花费了"
+ (endTime - startTime)
+ " 毫秒来把文档增加到索引里面去! "
+ fileDir.getPath());
}
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;
}
}
//上面这个是 创建索引完毕 你可以在C盘路径下 看到生成三个文件 然后我们在进行查询 索引中内容 通过关键词
package snippet;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
public class TestQuery {
public static void main(String[] args) throws IOException, ParseException {
Hits hits = null ;
String queryString = "中华 ";
Query query = null ;
IndexSearcher searcher = new IndexSearcher( "c:\\index" );
Analyzer analyzer = new StandardAnalyzer(); //创建 分析器
try {
QueryParser qp = new QueryParser( "path", analyzer); //表示 查询 分析器中的 内容(这里是一个对象)
System.out.println(qp);
query = qp.parse(queryString); //这里的 query 表示 开始查询
} catch (ParseException e) {
}
if (searcher != null ) {
hits = searcher.search(query); //这里表示 已经查询到 内容
// System.out.println(hits);
if (hits.length() > 0 ) {
System.out.println("找到:" + hits.length() + "个结果!" );
}
}
}
}
//以上是 查询完毕
http://blog.csdn.net/tianlincao/article/details/6867127