锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
棣栧厛瑕佹湁涓涓寘鍖匤ar鐨勯偅涓紝鍙互鍒板畼鏂圭綉绔欏幓涓嬭澆錛屼箣鍚庣幇鐮旂┒涓涓嬭繖涓寘鍖呯敱浜庣幇鍦ㄦ槸瀛︿範闃舵錛屽氨涓嬩簡涓や釜鐗堟湰1.4.3鐨勫拰2.0鐨勶紝lucene-2.0鐨勭暀鐫浠ュ悗寮鍙戠敤錛宭ucene-1.4.3鐨勫涔犵敤錛屾瘯绔熷埌2.0 鏃朵唬鏂囦歡鏍煎紡鏈夊緢澶х殑鍙樺寲錛屽寘鎷敓鎴愮殑index鏍煎紡閮藉彉鍖栦簡錛屾墍浠ユ渶濂芥槸涓や釜鐗堟湰閮芥湁銆傚湪寮鍙戠殑鏃跺欏鍏ヨ繖涓や釜鍖呭氨琛屼簡錛屾垜寮濮嬬湡鐨勪笉浼氾紝姹楋紒鎴戣繕浠ヤ負鍜孋++閲岄潰鐨勪竴鏍峰憿鐩存帴include錛岀幇鍦ㄦ兂璧鋒潵鐙傛檿錛岄偅鏃跺欏垰寮濮嬪啓Java榪炲0鏄庝釜綾婚兘鍙獵javaclass錛孧FC鐨勫啓娉曪紝姹楄嚜宸變竴涓紒瀹氫箟鍙橀噺榪樹繚鐣機鐨勪範鎯痏javaVar_錛屽啀姹椾竴涓紝鐜板湪濂藉浜嗐?br />姝ラ涓錛?br />鍏堝啓涓涓畾涔夊父閲忕殑*.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";
}
鐢ㄦ潵瀛樺偍瑕佸緩绔嬬儲寮曠殑鏂囦歡鍜屽瓨鍌ㄥ緩濂界殑绱㈠紩瀛樺偍鍦ㄤ粈涔堝湴鏂?br />姝ラ浜岋細
鍐欑敓鎴愮儲寮曠殑綾伙細
聽//灝嗚绱㈠紩鐨勬枃浠舵瀯鎴愪竴涓狣ocument瀵硅薄,騫舵坊鍔犱竴涓煙"content"
聽public class LuceneIndex {
聽//绱㈠紩鍣?br />聽private IndexWriter writer = null;
聽// 鍒濆鍖?====>鏋勯犲嚱鏁?br />聽public LuceneIndex() {
聽聽try {
聽聽聽writer = new IndexWriter(Constants.INDEX_STORE_PATH,new StandardAnalyzer(), true);
聽聽} catch (Exception e) {
聽聽聽e.printStackTrace();
聽聽}
聽}
聽//灝嗚绱㈠紩鐨勬枃浠舵瀯鎴愪竴涓狣ocument瀵硅薄,騫舵坊鍔犱竴涓煙"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("姝e湪寤虹珛绱㈠紩..........璇風瓑寰?);
聽聽聽for (int i = 0; i < files.length; i++) {
聽聽聽聽File file = new File(folder, files[i]);
聽聽聽聽Document doc = getDocument(file);
聽聽聽聽System.out.println("姝e湪寤虹珛鏂囦歡 : " + file + " 鐨勭儲寮?);
聽聽聽聽System.out.println("瀹屾瘯");
聽聽聽聽writer.addDocument(doc);
聽聽聽}
聽聽}
聽}
聽public void close() throws Exception {
聽聽writer.close();
聽}
聽//嫻嬭瘯鐢ㄧ殑涓葷▼搴?br />聽public static void main(String[] agrs) throws Exception {
聽聽// 澹版槑涓涓狶uceneIndex瀵硅薄
聽聽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();
聽}
}
鐜板湪绱㈠紩鐢熸垚浜嗭紝鏄繖浜涙枃鏈殑鐨勫叏鏂囩儲寮曠敤鐨勭儲寮曟枃浠?br />姝ラ涓夛細
鐜板湪鍩虹閮芥湁浜嗭紝瑕佺殑灝辨槸鎼滅儲鐨勭瘡浜嗭紝騫插槢錛熷啓涓悳绱㈢被灝辨槸鐢ㄦ潵鏌ヨ鍟婏紒
public class LuceneSearch {
聽// 澹版槑涓涓狪ndexSearcher瀵硅薄
聽private IndexSearcher searcher = null;
聽// 澹版槑涓涓猀uery瀵硅薄
聽private Query query = null;
聽// 鍒濆鍖栨瀯閫犲嚱鏁?br />聽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("姝e湪媯绱㈠叧閿瓧 : " + 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====>鏌ヨ鍏抽敭瀛?br />ok錛宼his 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!