<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Dict.CN 在線詞典, 英語學習, 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統計

    留言簿(23)

    積分與排名

    優秀學習網站

    友情連接

    閱讀排行榜

    評論排行榜

    搜索篇:lucene的簡單實例

    說明一下,這一篇文章的用到的lucene,是用2.0版本的,主要在查詢的時候2.0版本的lucene與以前的版本有了一些區別.
    其實這一些代碼都是早幾個月寫的,自己很懶,所以到今天才寫到自己的博客上,高深的文章自己寫不了,只能記錄下一些簡單的記錄與點滴,其中的代碼算是自娛自樂的,希望高手不要把重構之類的砸下來...

    1、在windows系統下的的C盤,建一個名叫s的文件夾,在該文件夾里面隨便建三個txt文件,隨便起名啦,就叫"1.txt","2.txt"和"3.txt"啦
    其中1.txt的內容如下:

    代碼
    1. 中華人民共和國 ??
    2. 全國人民 ??
    3. 2006 年??

    而"2.txt"和"3.txt"的內容也可以隨便寫幾寫,這里懶寫,就復制一個和1.txt文件的內容一樣吧

    2、下載lucene包,放在classpath路徑中
    建立索引:

    代碼
    1. package?lighter.javaeye.com; ??
    2. ??
    3. import?java.io.BufferedReader; ??
    4. import?java.io.File; ??
    5. import?java.io.FileInputStream; ??
    6. import?java.io.IOException; ??
    7. import?java.io.InputStreamReader; ??
    8. import?java.util.Date; ??
    9. ??
    10. import?org.apache.lucene.analysis.Analyzer; ??
    11. import?org.apache.lucene.analysis.standard.StandardAnalyzer; ??
    12. import?org.apache.lucene.document.Document; ??
    13. import?org.apache.lucene.document.Field; ??
    14. import?org.apache.lucene.index.IndexWriter; ??
    15. ??
    16. /** ?
    17. ?*?author?lighter?date?2006-8-7 ?
    18. ?*/??
    19. public?class?TextFileIndexer?{ ??
    20. ????public?static?void?main(String[]?args)?throws?Exception?{ ??
    21. ????????/*?指明要索引文件夾的位置,這里是C盤的S文件夾下?*/??
    22. ????????File?fileDir?=?new?File("c:\\s"); ??
    23. ??
    24. ????????/*?這里放索引文件的位置?*/??
    25. ????????File?indexDir?=?new?File("c:\\index"); ??
    26. ????????Analyzer?luceneAnalyzer?=?new?StandardAnalyzer(); ??
    27. ????????IndexWriter?indexWriter?=?new?IndexWriter(indexDir,?luceneAnalyzer, ??
    28. ????????????????true); ??
    29. ????????File[]?textFiles?=?fileDir.listFiles(); ??
    30. ????????long?startTime?=?new?Date().getTime(); ??
    31. ???????? ??
    32. ????????//增加document到索引去 ??
    33. ????????for?(int?i?=?0;?i?<?textFiles.length;?i++)?{ ??
    34. ????????????if?(textFiles[i].isFile() ??
    35. ????????????????????&&?textFiles[i].getName().endsWith(".txt"))?{ ??
    36. ????????????????System.out.println("File?"?+?textFiles[i].getCanonicalPath() ??
    37. ????????????????????????+?"正在被索引...."); ??
    38. ????????????????String?temp?=?FileReaderAll(textFiles[i].getCanonicalPath(), ??
    39. ????????????????????????"GBK"); ??
    40. ????????????????System.out.println(temp); ??
    41. ????????????????Document?document?=?new?Document(); ??
    42. ????????????????Field?FieldPath?=?new?Field("path",?textFiles[i].getPath(), ??
    43. ????????????????????????Field.Store.YES,?Field.Index.NO); ??
    44. ????????????????Field?FieldBody?=?new?Field("body",?temp,?Field.Store.YES, ??
    45. ????????????????????????Field.Index.TOKENIZED, ??
    46. ????????????????????????Field.TermVector.WITH_POSITIONS_OFFSETS); ??
    47. ????????????????document.add(FieldPath); ??
    48. ????????????????document.add(FieldBody); ??
    49. ????????????????indexWriter.addDocument(document); ??
    50. ????????????} ??
    51. ????????} ??
    52. ????????//optimize()方法是對索引進行優化 ??
    53. ????????indexWriter.optimize(); ??
    54. ????????indexWriter.close(); ??
    55. ???????? ??
    56. ????????//測試一下索引的時間 ??
    57. ????????long?endTime?=?new?Date().getTime(); ??
    58. ????????System.out ??
    59. ????????????????.println("這花費了"??
    60. ????????????????????????+?(endTime?-?startTime) ??
    61. ????????????????????????+?"?毫秒來把文檔增加到索引里面去!"??
    62. ????????????????????????+?fileDir.getPath()); ??
    63. ????} ??
    64. ??
    65. ????public?static?String?FileReaderAll(String?FileName,?String?charset) ??
    66. ????????????throws?IOException?{ ??
    67. ????????BufferedReader?reader?=?new?BufferedReader(new?InputStreamReader( ??
    68. ????????????????new?FileInputStream(FileName),?charset)); ??
    69. ????????String?line?=?new?String(); ??
    70. ????????String?temp?=?new?String(); ??
    71. ???????? ??
    72. ????????while?((line?=?reader.readLine())?!=?null)?{ ??
    73. ????????????temp?+=?line; ??
    74. ????????} ??
    75. ????????reader.close(); ??
    76. ????????return?temp; ??
    77. ????} ??
    78. }??

    索引的結果:

    代碼
    1. File?C:\s\1.txt正在被索引.... ??
    2. 中華人民共和國全國人民2006年 ??
    3. File?C:\s\2.txt正在被索引.... ??
    4. 中華人民共和國全國人民2006年 ??
    5. File?C:\s\3.txt正在被索引.... ??
    6. 中華人民共和國全國人民2006年 ??
    7. 這花費了297?毫秒來把文檔增加到索引里面去!c:\s??

    3、建立了索引之后,查詢啦....

    代碼
    1. package?lighter.javaeye.com; ??
    2. ??
    3. import?java.io.IOException; ??
    4. ??
    5. import?org.apache.lucene.analysis.Analyzer; ??
    6. import?org.apache.lucene.analysis.standard.StandardAnalyzer; ??
    7. import?org.apache.lucene.queryParser.ParseException; ??
    8. import?org.apache.lucene.queryParser.QueryParser; ??
    9. import?org.apache.lucene.search.Hits; ??
    10. import?org.apache.lucene.search.IndexSearcher; ??
    11. import?org.apache.lucene.search.Query; ??
    12. ??
    13. public?class?TestQuery?{ ??
    14. ????public?static?void?main(String[]?args)?throws?IOException,?ParseException?{ ??
    15. ????????Hits?hits?=?null; ??
    16. ????????String?queryString?=?"中華"; ??
    17. ????????Query?query?=?null; ??
    18. ????????IndexSearcher?searcher?=?new?IndexSearcher("c:\\index"); ??
    19. ??
    20. ????????Analyzer?analyzer?=?new?StandardAnalyzer(); ??
    21. ????????try?{ ??
    22. ????????????QueryParser?qp?=?new?QueryParser("body",?analyzer); ??
    23. ????????????query?=?qp.parse(queryString); ??
    24. ????????}?catch?(ParseException?e)?{ ??
    25. ????????} ??
    26. ????????if?(searcher?!=?null)?{ ??
    27. ????????????hits?=?searcher.search(query); ??
    28. ????????????if?(hits.length()?>?0)?{ ??
    29. ????????????????System.out.println("找到:"?+?hits.length()?+?"?個結果!"); ??
    30. ????????????} ??
    31. ????????} ??
    32. ????} ??
    33. ??
    34. }??

    其運行結果:

    引用
    找到:3 個結果!

    具體的API的用法,這里就不說了,具體的做法參考lucene的官方文檔吧...
    下一篇文章:
    搜索篇:lucene的簡單實例<二> http://www.javaeye.com/post/190576

    posted on 2006-12-23 10:16 都市淘沙者 閱讀(206) 評論(0)  編輯  收藏 所屬分類: Java Basic/Lucene/開源資料

    主站蜘蛛池模板: 曰韩亚洲av人人夜夜澡人人爽 | 国产精品网站在线观看免费传媒| 亚洲色精品vr一区二区三区 | 免费av欧美国产在钱| 亚洲1区2区3区精华液| 亚洲国产精品成人久久| 最近免费中文字幕大全| 国产成人无码精品久久久免费| 亚洲综合久久1区2区3区| 免费一级毛片免费播放| 99久9在线|免费| 一区免费在线观看| 亚洲人成网站18禁止久久影院| 亚洲精品国产高清不卡在线| 免费在线观看视频网站| 一区免费在线观看| 亚洲熟妇无码一区二区三区| 亚洲AV无码精品色午夜在线观看| 四色在线精品免费观看| 日韩av无码久久精品免费| 四虎成人精品国产永久免费无码 | 成在线人免费无码高潮喷水| 亚洲熟女精品中文字幕| 亚洲人成网www| 国产亚洲精品精品国产亚洲综合 | 亚洲一卡2卡4卡5卡6卡在线99| 久久激情亚洲精品无码?V| 成人影片麻豆国产影片免费观看 | 九九99热免费最新版| 亚洲色成人网站WWW永久四虎 | 中文字幕在线免费观看视频| 亚洲AV一区二区三区四区| 亚洲精品福利在线观看| 亚洲自偷自偷在线制服| 亚洲成人高清在线| 日本特黄特色aa大片免费| 免费不卡视频一卡二卡| 最近的中文字幕大全免费8| a级毛片视频免费观看| 一区二区三区AV高清免费波多| 亚洲Aⅴ在线无码播放毛片一线天 亚洲avav天堂av在线网毛片 |