首先下載lucene相關jar包,這里就不多說,自己網上找
在eclipse下建立web工程luceneTest
將jar包加載到你的web工程里面
新建類Index.java,代碼如下:
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.RAMDirectory;
/*
* Create Date:2007-10-26 下午02:52:53
*
* Author:dingkm
*
* Version: V1.0
*
* Description:對進行修改的功能進行描述
*
*
*/
public class Index {
/**
* @Description 方法實現功能描述
* @param args
* void
* @throws 拋出異常說明
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
new Index().index();
System.out.println("create index success!!!");
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void index() throws CorruptIndexException, LockObtainFailedException, IOException{
long start = System.currentTimeMillis();
// 建立索引的路徑
String path = "c:\\index2";
Document doc1 = new Document();
doc1.add( new Field("name", "中華人民共和國",Field.Store.YES,Field.Index.TOKENIZED));
doc1.add( new Field("content", "標題或正文包括",Field.Store.YES,Field.Index.TOKENIZED));
doc1.add( new Field("time", "20080715",Field.Store.YES,Field.Index.TOKENIZED));
Document doc2 = new Document();
doc2.add(new Field("name", "大中國中國",Field.Store.YES,Field.Index.TOKENIZED));
IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);
writer.setMaxMergeDocs(10);
writer.setMaxFieldLength(3);
writer.addDocument(doc1);
writer.setMaxFieldLength(3);
writer.addDocument(doc2);
writer.close();
System.out.println("=========================");
System.out.print(System.currentTimeMillis() - start);
System.out.println("total milliseconds");
System.out.println("=========================");
}
}
執行這個類,可以看到結果:
=========================
375total milliseconds
=========================
create index success!!!
可以看到索引創建成功。
下面我們來創建搜索類,Search.java
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
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;
/*
* Create Date:2007-10-26 下午02:56:12
*
* Author:dingkm
*
* Version: V1.0
*
* Description:對進行修改的功能進行描述
*
*
*/
public class Search {
/**
* @Description 方法實現功能描述
* @param args
* void
* @throws 拋出異常說明
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = "c:\\index2";
try {
new Search().search(path);
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void search(String path) throws CorruptIndexException, IOException, ParseException{
IndexSearcher searcher = new IndexSearcher(path);
Hits hits = null;
Query query = null;
QueryParser qp = new QueryParser("name",new StandardAnalyzer());
query = qp.parse("中");
hits = searcher.search(query);
java.text.NumberFormat format = java.text.NumberFormat.getNumberInstance();
System.out.println("查找到共" + hits.length() + "個結果");
for (int i = 0; i < hits.length(); i++) {
//開始輸出查詢結果
Document doc = hits.doc(i);
System.out.println(doc.get("name"));
System.out.println("content="+doc.get("content"));
System.out.println("time="+doc.get("time"));
System.out.println("準確度為:" + format.format(hits.score(i) * 100.0) + "%");
// System.out.println(doc.get("CONTENT"));
}
}
}
執行它,會得到以下結果:
查找到共2個結果
中華人民共和國
content=標題或正文包括
time=20080715
準確度為:29.727%
大中國中國
content=null
time=null
準確度為:29.727%
這樣就完成了我們的程序
這是我第一次發表文章
說的比較簡單,可能很多地方說的不清楚
希望大家多多支持
有什么不明白的歡迎留言。
posted on 2008-09-04 13:06
老丁 閱讀(538)
評論(0) 編輯 收藏 所屬分類:
搜索引擎 lucene