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

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

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

    草狼

    在努力、磨練、打擊下不斷地向著理想的財務自由方向前進

    2006年12月29日

    這幾天,我的第一個基于lucene的搜索搞好了,記載一下:
    首先要有一個包包Jar的那個,可以到官方網站去下載,之后現研究一下這個包包由于現在是學習階段,就下了兩個版本1.4.3的和2.0的,lucene-2.0的留著以后開發用,lucene-1.4.3的學習用,畢竟到2.0 時代文件格式有很大的變化,包括生成的index格式都變化了,所以最好是兩個版本都有。在開發的時候導入這兩個包就行了,我開始真的不會,汗!我還以為和C++里面的一樣呢直接include,現在想起來狂暈,那時候剛開始寫Java連聲明個類都叫Cjavaclass,MFC的寫法,汗自己一個!定義變量還保留C的習慣_javaVar_,再汗一個,現在好多了。
    步驟一:
    先寫一個定義常量的*.java文件
    public class Constants {
    ?public final static String INDEX_FILE_PATH = "C:\\Java\\lucene\\DataSource";
    ?public final static String INDEX_STORE_PATH = "C:\\Java\\lucene\\DataIndex";
    }
    用來存儲要建立索引的文件和存儲建好的索引存儲在什么地方
    步驟二:
    寫生成索引的類:
    ?//將要索引的文件構成一個Document對象,并添加一個域"content"
    ?public class LuceneIndex {
    ?//索引器
    ?private IndexWriter writer = null;
    ?// 初始化=====>構造函數
    ?public LuceneIndex() {
    ??try {
    ???writer = new IndexWriter(Constants.INDEX_STORE_PATH,new StandardAnalyzer(), true);
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}
    ?//將要索引的文件構成一個Document對象,并添加一個域"content"
    ?private Document getDocument(File f) throws Exception {
    ??Document doc = new Document();
    ??FileInputStream is = new FileInputStream(f);
    ??Reader reader = new BufferedReader(new InputStreamReader(is));
    ??doc.add(Field.Text("contents", reader));
    ??doc.add(Field.Keyword("path", f.getAbsolutePath()));
    ??return doc;
    ?}
    ?public void writeToIndex() throws Exception {
    ??File folder = new File(Constants.INDEX_FILE_PATH);
    ??if (folder.isDirectory()) {
    ???String[] files = folder.list();
    ???System.out.println("正在建立索引..........請等待");
    ???for (int i = 0; i < files.length; i++) {
    ????File file = new File(folder, files[i]);
    ????Document doc = getDocument(file);
    ????System.out.println("正在建立文件 : " + file + " 的索引");
    ????System.out.println("完畢");
    ????writer.addDocument(doc);
    ???}
    ??}
    ?}
    ?public void close() throws Exception {
    ??writer.close();
    ?}
    ?//測試用的主程序
    ?public static void main(String[] agrs) throws Exception {
    ??// 聲明一個LuceneIndex對象
    ??LuceneIndex indexer = new LuceneIndex();
    ??// 建立索引
    ??Date start = new Date();
    ??indexer.writeToIndex();
    ??Date end = new Date();
    ??System.out.println("建立索引完畢..........Thank you for Lucene");
    ??System.out.println("");
    ??System.out.println("消耗時間 " + (end.getTime() - start.getTime())
    ????+ " 毫秒");
    ??System.out.println("索引建立完畢");
    ??indexer.close();
    ?}
    }
    現在索引生成了,是這些文本的的全文索引用的索引文件
    步驟三:
    現在基礎都有了,要的就是搜索的累了,干嘛?寫個搜索類就是用來查詢??!
    public class LuceneSearch {
    ?// 聲明一個IndexSearcher對象
    ?private IndexSearcher searcher = null;
    ?// 聲明一個Query對象
    ?private Query query = null;
    ?// 初始化構造函數
    ?public LuceneSearch() {
    ??try {
    ???searcher = new IndexSearcher(IndexReader.open(Constants.INDEX_STORE_PATH));
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ??}
    ?}
    ?public final Hits search(String keyword) {
    ??System.out.println("正在檢索關鍵字 : " + keyword);
    ??// System.out.println(keyword);
    ??try {
    ???query = QueryParser.parse(keyword, "contents",
    ?????new StandardAnalyzer());
    ???System.out.println(query);
    ???Date start = new Date();
    ???Hits hits = searcher.search(query);
    ???Date end = new Date();
    ???System.out.println("檢索完成......." + " 用時 "+ (end.getTime() - start.getTime()) + " 毫秒");
    ???System.out.println(" ");
    ???return hits;
    ??} catch (Exception e) {
    ???e.printStackTrace();
    ???return null;
    ??}
    ?}

    ?public void printResult(Hits h) {
    ??if (h.length() == 0) {
    ???System.out.println(h);
    ???System.out.println(h.length());
    ???System.out.println("對不起,沒有找到您需要的結果");
    ??} else {
    ???for (int i = 0; i < h.length(); i++) {
    ????try {
    ?????Document doc = h.doc(i);
    ?????System.out.print("這是第 " + i + "個檢索結果,文件名為: ");
    ?????System.out.println(doc.get("path"));
    ????} catch (Exception e) {
    ?????e.printStackTrace();
    ????}
    ???}
    ??}
    ??System.out.println(" ");
    ??System.out.println("----------------------------------");
    ??System.out.println(" ");
    ?}
    ?
    ?public static void main(String[] args) throws Exception {
    ??LuceneSearch test = new LuceneSearch();
    ??Hits myHits1 = test.search("足球");
    ??Hits myHits2 = test.search("世界杯");
    ??test.printResult(myHits1);
    ??test.printResult(myHits2);
    ?}
    }
    步驟四:
    運行LuceneIndex.java=====> 生成索引
    運行LuceneSearch.java====>查詢關鍵字
    ok,this is my first searcher!
    Although this is very simple,it let me begin with luceneSearcher.Thanks lucene,Tanks Search!
    Keep on studying knowledge of lucene and search,also and artificial intelligence!
    I love this job!

    posted @ 2006-12-29 11:49 在法律保護下合法地搶銀行 閱讀(181) | 評論 (0)編輯 收藏
    僅列出標題  下一頁

    公告

    轉載、引用、收藏,請標明來自:

    http://m.tkk7.com/gupaladino

    導航

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    統計

    常用鏈接

    留言簿(1)

    隨筆分類(3)

    隨筆檔案(5)

    文章分類(1)

    文章檔案(1)

    相冊

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产精一品亚洲二区在线播放 | 午夜毛片不卡高清免费| 亚洲欧美精品午睡沙发| 亚洲精品国产精品国自产观看| 国产婷婷成人久久Av免费高清| 亚洲国产精品日韩在线| vvvv99日韩精品亚洲| 国产成人精品一区二区三区免费| 亚洲激情视频图片| 亚洲一级特黄大片无码毛片| 18禁黄网站禁片免费观看不卡| 春暖花开亚洲性无区一区二区| 国产av无码专区亚洲av桃花庵| 亚洲国产精品免费观看| 一区二区三区免费视频网站| 亚洲精品国产免费| 亚洲无码精品浪潮| 无码中文在线二区免费| 二个人看的www免费视频| 亚洲av乱码一区二区三区| 精品国产亚洲一区二区在线观看 | 亚洲天堂免费在线| 国产AV无码专区亚洲精品| 在线观看国产情趣免费视频| 先锋影音资源片午夜在线观看视频免费播放 | 免费中文字幕在线| 999国内精品永久免费视频| 国产精品免费大片一区二区| 亚洲国产精品美女久久久久| 久久噜噜噜久久亚洲va久| 高清在线亚洲精品国产二区| 免费精品国产自产拍在线观看图片| 中国一级特黄高清免费的大片中国一级黄色片 | 无码精品人妻一区二区三区免费| 亚洲五月综合缴情婷婷| 久久夜色精品国产亚洲AV动态图| 亚洲欧洲日产国码高潮αv| 日本v片免费一区二区三区 | 久久亚洲国产精品一区二区| 免费人成在线观看网站视频| 最近2019中文字幕免费看最新|