利用lucene對檔案內容進行關鍵字檢索

2021-09-02 11:33:07 字數 1737 閱讀 5675

在具體實現之前,請根據自己的要求,建立對應的路徑及檔案。例如,我這邊建立的路徑及檔案是:     d:/tools/learningbymyself/lucene/source/demo1.txt

d:/tools/learningbymyself/lucene/source/demo2.txt

d:/tools/learningbymyself/lucene/index

第一步,建立索引,**如下:

/**

* @param sourcefile 需要新增到索引中的路徑

* @param indexfile 存放索引的路徑

* @throws exception

*/public static void textfileindexer(string sourcefile,string indexfile) throws exception

}indexwriter.close();

long endtime = new date().gettime();

system.out.println("一共花費了" +

(endtime - starttime) + "毫秒將" + sourcedir.getpath() + "中的檔案增加到索引裡面去.....");

}

private static string filereaderall(string filename,string charset) throws ioexception

buffer_read.close();

return temp ;

}

第二步,在索引中檢索關鍵字

/**

* @param indexfile 索引所在的路徑

* @param keywords 需要檢索的關鍵字

* @throws ioexception

* @throws parseexception

*/public static void querykeywords(string indexfile,string keywords) throws ioexception,parseexception

index_search.close();

}}

第三部,自己編寫乙個測試類,測試一下上面的兩個方法,例如,我寫的測試類如下:

public class lucenetest 

}

最後,在控制台上檢視結果。例如,我這邊的測試結果如下:

file--->d:\tools\learningbymyself\lucene\source\demo1.txt 正在被索引.....

檔案內容:為了保證機房的網路安全,idc內所有伺服器不被允許從辦公網直接ssh登入,必須通過跳板機 進行間接登入。使用者通過跳板機執行的所有命令(包括通過跳板機登入的其他機器後的命令)都會被儲存並審計。

file--->d:\tools\learningbymyself\lucene\source\demo2.txt 正在被索引.....

檔案內容:relay是我們登入idc伺服器的跳板機,在relay上使用者只能執行ssh、passwd等簡單命令,relay只做ssh跳板機兒不做日常工具機使用。

一共花費了235毫秒將d:\tools\learningbymyself\lucene\source中的檔案增加到索引裡面去.....

關鍵字:伺服器,在  d:/tools/learningbymyself/lucene/index中,一共檢索到2個...

利用lucene對檔案內容進行關鍵字檢索

一 概述 二 例項講解 在具體實現之前,請根據自己的要求,建立對應的路徑及檔案。例如,我這邊建立的路徑及檔案是 d tools learningbymyself lucene source demo1.txt d tools learningbymyself lucene source demo2....

linux less對檔案內容進行搜尋

可以先用 less 檔名 來開啟檔案,然後可以按回車,開啟底部命令輸入行 即出現乙個冒號的位置 然後可以使用 鍵盤上的 home 鍵跳到檔案開始,end鍵跳到最後,pgup向前翻頁,pgdn向下翻頁,還可以在冒號後面直接輸入 要搜尋的關鍵字 進行 高亮顯示,可以 用 n 向前查詢或者 n 向後查詢。...

利用FILE結構對檔案進行操作

利用file結構對檔案進行操作 輸入若干個學生的資料 包括學號 姓名和成績 將資料存放在student.txt檔案中 從student.txt檔案中讀取所有資料並顯示出來 include include include struct student void main file write,read...