lucene檢索數(shù)據(jù)庫(kù)_第1頁(yè)
lucene檢索數(shù)據(jù)庫(kù)_第2頁(yè)
lucene檢索數(shù)據(jù)庫(kù)_第3頁(yè)
lucene檢索數(shù)據(jù)庫(kù)_第4頁(yè)
lucene檢索數(shù)據(jù)庫(kù)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、寫(xiě)一段傳統(tǒng)的JDBC程序,講每條的用戶信息從數(shù)據(jù)庫(kù)讀取出來(lái)針對(duì)每條用戶記錄,建立一個(gè)lucene documentDocument doc = new Document();并根據(jù)你的需要,將用戶信息的各個(gè)字段對(duì)應(yīng)luncene document中的field進(jìn)行添加,如:doc.add(new Field(NAME”,USERNAME”,Field.Store.YES,Field.Index.UN_TOKENIZED);然后將該條doc加入到索引中,如:luceneWriter.addDocument(doc);這樣就建立了 lucene的索引庫(kù)編寫(xiě)對(duì)索引庫(kù)的搜索程序(看lucene文檔),

2、通過(guò)對(duì)lucene的索引庫(kù)的查找,你可以快速找到對(duì)應(yīng)記 錄的ID通過(guò)ID到數(shù)據(jù)庫(kù)中查找相關(guān)記錄用Lucene索引數(shù)據(jù)庫(kù)Lucene,作為一種全文搜索的輔助工具,為我們進(jìn)行條件搜索,無(wú)論是像Google,Baidu之類的搜 索引擎,還是論壇中的搜索功能,還是其它C/S架構(gòu)的搜索,都帶來(lái)了極大的便利和比較高的效率。本 文主要是利用Lucene對(duì)MS Sql Server 2000進(jìn)行建立索引,然后進(jìn)行全文索引。至于數(shù)據(jù)庫(kù)的內(nèi)容, 可以是網(wǎng)頁(yè)的內(nèi)容,還是其它的。本文中數(shù)據(jù)庫(kù)的內(nèi)容是圖書(shū)館管理系統(tǒng)中的某個(gè)作者表一Authors表。因?yàn)榭紤]到篇幅的問(wèn)題,所以該文不會(huì)講的很詳細(xì),也不可能講的很深。本文以這

3、樣的結(jié)構(gòu)進(jìn)行:介紹數(shù)據(jù)庫(kù)中Authors表的結(jié)構(gòu)為數(shù)據(jù)庫(kù)建立索引為數(shù)據(jù)庫(kù)建立查詢功能在web界面下進(jìn)行查詢并顯示結(jié)果1.介紹數(shù)據(jù)庫(kù)中Authors表的結(jié)構(gòu)字段名稱字段類型字段含義Au_idVarchar(11)作者號(hào)Au_nameVarchar(60)作者名PhoneChar(12)電話號(hào)碼AddressVarchar(40)地址CityVarchar(20)城市StateChar(2)省份ZipChar(5)郵編contractBit(1)外鍵(關(guān)系不大)表中的部分內(nèi)容:為數(shù)據(jù)庫(kù)建立索引首先建立一個(gè)類TestLucene.java。這個(gè)類就是對(duì)數(shù)據(jù)庫(kù)進(jìn)行建立索引,編寫(xiě)查詢條件等。當(dāng)然,最開(kāi)始

4、就是建立數(shù)據(jù)庫(kù)連接。連接代碼這里就省略了。八_八接著,新建一個(gè)方法getResutl(String),它返回的是數(shù)據(jù)庫(kù)表Authors的內(nèi)容。具體代碼如下:public ResultSet getResult(String sql)tryStatement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery(sql);return rs;catch(SQLException e)System.out.println(e);return null;然后,為數(shù)據(jù)庫(kù)建立索引。首先要定義一個(gè)IndexWriter(),它是將索引

5、寫(xiě)進(jìn)Lucene自己的數(shù)據(jù)庫(kù)中,它存放的位置是有 你自己定義的。在定義IndexWriter是需要指定它的分析器。Lucene自己自帶有幾個(gè)分析器,例如: StandarAnalyzer(),SimpleAnalyzer(), StopAnalyzer()等。它作用是對(duì)文本進(jìn)行分析,判斷如何進(jìn)行 切詞。接著,要定義一個(gè)Document。Document相當(dāng)于二維表中一行數(shù)據(jù)一樣。Document里包含的是Field 字段,F(xiàn)ield相當(dāng)于數(shù)據(jù)庫(kù)中一列,也就是一個(gè)屬性,一個(gè)字段。最后應(yīng)該對(duì)IndexWriter進(jìn)行優(yōu)化,方法很簡(jiǎn)單,就是writer.optimize().具體代碼如下:publi

6、c void Index(ResultSet rs)tryIndexWriter writer = new IndexWriter(d:/index/, getAnalyzer(), true);while(rs.next()Document doc=new Document();doc.add(Field.Keyword(id,rs.getString(au_id);doc.add(Field.Text(name,rs.getString(au_name);doc.add(Field.UnIndexed(address,rs.getString(address);doc.add(Field.

7、UnIndexed(phone,rs.getString(phone);doc.add(Field.Text(City,rs.getString(city);writer.addDocument(doc);writer.optimize();writer.close();catch(IOException e)System.out.println(e);catch(SQLException e)System.out.println(e);public Analyzer getAnalyzer()return new StandardAnalyzer();為數(shù)據(jù)庫(kù)建立查詢功能在類TestLuce

8、ne中建立一個(gè)新的方法searcher(String),它返回的是一個(gè)搜索的結(jié)構(gòu)集,相當(dāng)于 數(shù)據(jù)庫(kù)中的ResultSet 一樣。它代的參數(shù)是你要查詢的內(nèi)容。這里,我把要查詢的字段寫(xiě)死了。你可以在 添加一個(gè)參數(shù)表示要查詢的字段。這里主要有兩個(gè)對(duì)象IndexSearcher和Query。IndexSearcher是找到索引數(shù)據(jù)庫(kù),Query是處理搜 索,它包含了三個(gè)參數(shù):查詢內(nèi)容,查詢字段,分析器。具體代碼如下:public Hits seacher(String queryString)Hits hits=null;tryIndexSearcher is = new IndexSearcher(

9、D:/index/);Query query=QueryParser.parse(queryString,City,getAnalyzer();hits=is.search(query);catch(Exception e)System.out.print(e);return hits;在web界面下進(jìn)行查詢并顯示結(jié)果這里建立一個(gè)Jsp頁(yè)面TestLucene.jsp進(jìn)行搜索。在TestLucene.jsp頁(yè)面中首先引入類然后定義一個(gè)LuceneTest對(duì)象,獲取查詢結(jié)果集:LucentTest lucent=new LucentTest();Hits hits=lucent.seacher(

10、request.getParameter(queryString);定義一個(gè)Form,建立一個(gè)查詢環(huán)境:顯示查詢結(jié)果:作者號(hào)作者名 地址 電話號(hào)碼% for(int i = 0;i用Lucene-1.3-final為網(wǎng)站數(shù)據(jù)庫(kù)建立索引下是看了 lnboy寫(xiě)的用lucene建立大富翁論壇的全文檢索后寫(xiě)的測(cè)試代碼。為數(shù)據(jù)庫(kù)cwb.mdb建立全文索引的indexdb.jsp用于顯示查詢結(jié)果的aftsearch.jsp%String keyword = request.getParameter(keyword);keyword = new String(keyword.getBytes(ISO8859

11、_1);out.println(keyword);try String aa=getServletContext().getRealPath(/)+index;Searcher searcher = new IndexSearcher(aa);Query query = QueryParser.parse(keyword, Article_name, new StandardAnalyzer();out.println(正在查找:+ query.toString(Article_name)+);Hits hits = searcher.search(query);System.out.prin

12、tln(hits.length() + total matching documents);java.text.NumberFormat format = java.text.NumberFormat.getNumberInstance();for (int i = 0; i hits.length(); i +) 開(kāi)始輸出查詢結(jié)果Document doc = hits.doc(i);out.println(doc.get(Article_id);out.println(準(zhǔn)確度為:+ format.format(hits.score(i) * 100.0) + %);out.println(d

13、oc.get(Article_name)+);/ out.println(doc.get(Article_intro);catch (Exception e) out.println(出錯(cuò)了 + e.getClass() +n 錯(cuò)誤信息為:+ e.getMessage();%輔助類:package lucene;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.DateField;public class myd

14、ocument public static Document Document(String Article_id,String Article_name,String Article_intro )Document doc = new Document();doc.add(Field.Keyword(Article_id, Article_id);doc.add(Field.Text(Article_name, Article_name);doc.add(Field.Text(Article_intro, Article_intro);return doc;public mydocument

15、() 用lucene為數(shù)據(jù)庫(kù)搜索建立增量索引用lucene建立索引不可能每次都重新開(kāi)始建立,而是按照新增加的記錄,一次次的遞增建立索引的IndexWriter類,有三個(gè)參數(shù)IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),isEmpty);其中第三個(gè)參數(shù)是bool型的,指定它可以確定是增量索引,還是重建索引.對(duì)于從數(shù)據(jù)庫(kù)中讀取的記錄,譬如要為文章建立索引,我們可以記錄文章的id號(hào),然后下次再次建 立索引的時(shí)候讀取存下的id號(hào),從此id后往下繼續(xù)增加索引,邏輯如下.建立增量索引,主要代碼如下public void

16、createIndex(String path)Statement myStatement = null;String articleId = 0;讀取文件,獲得文章id號(hào)碼,這里只存最后一篇索引的文章idtry FileReader fr = new FileReader(*.txt);BufferedReader br = new BufferedReader(fr);articleId = br.readLine();if(articleId = = null |articleId = = )articleId=0;br.close();fr.close(); catch (IOExce

17、ption e) System.out.println(error343!”);e.printStackTrace();try /sql語(yǔ)句,根據(jù)id讀取下面的內(nèi)容String sqlText = *+articleId;myStatement = conn.createStatement();ResultSet rs = myStatement.executeQuery(sqlText);寫(xiě)索引while (rs.next() Document doc = new Document();doc.add(Field.Keyword(*, DateAdded);doc.add(Field.Key

18、word(*, articleid);doc.add(Field.Text(*, URL);doc.add(Field.Text(*, Content);doc.add(Field.Text(*, Title);trywriter.addDocument(doc);catch(IOException e)e.printStackTrace();將我索引的最后一篇文章的id寫(xiě)入文件try FileWriter fw = new FileWriter(*.txt);PrintWriter out = new PrintWriter(fw);out.close();fw.close(); catch

19、 (IOException e) e.printStackTrace();ind.Close();System.out.println(ok.end);catch (SQLException e)e.printStackTrace();finally 數(shù)據(jù)庫(kù)關(guān)閉操作然后控制是都建立增量索引的時(shí)候根據(jù)能否都到id值來(lái)設(shè)置IndexWriter的第三個(gè)參數(shù)為true或者是 falseboolean isEmpty = true;try FileReader fr = new FileReader(*.txt);BufferedReader br = new BufferedReader(fr);i

20、f(br.readLine()!= null) isEmpty = false;br.close();fr.close(); catch (IOException e) e.printStackTrace();writer = new IndexWriter(Directory, new StandardAnalyzer(),isEmpty); BOOKMARK 冷. I 寸相關(guān)文檔(Relevant Entries)Lucene基礎(chǔ)指南Beef up Web search applications with LuceneCompass-在Lucene之上作了什么增強(qiáng)?Lucene基本使用介紹為lucene加入簡(jiǎn)單中文分詞在應(yīng)用中加入全文檢索功能一一基于Java的全文索引引擎Lucene簡(jiǎn)介L(zhǎng)u

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論