台灣最大程式設計社群網站
線上人數
2281
 
會員總數:244119
討論主題:188482
歡迎您免費加入會員
討論區列表 >> Java >> 自己寫的文件檢索程式發生一些!?懇請指點新手~
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
自己寫的文件檢索程式發生一些!?懇請指點新手~
價值 : 50 QP  點閱數:155 回應數:1

樓主

JohnX
門外漢
0 2
6 0
發送站內信

我是直接用javac進行編譯,下面為錯誤訊息!


這是我的程式碼:
[code]

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringReader;

import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.highlight.Formatter;
import org.apache.lucene.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.search.highlight.TokenSources;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class Searcher {
public static void main(String[] args) throws IOException, ParseException, InvalidTokenOffsetsException {
String Idx = "D:\\Jwork\\Lucene\\search\\Idx";
String dataDir = "D:\\Jwork\\Lucene\\search\\Data";
Directory dir = new SimpleFSDirectory(new File(Idx));
IndexSearcher indexSearch = new IndexSearcher(dir);

QueryParser queryParser = new QueryParser(Version.LUCENE_30,
"contents", new StandardAnalyzer(Version.LUCENE_30));
Query query = queryParser.parse("鼻涕"); // key Query term
TopDocs hits = indexSearch.search(query, 500);
System.out.println("找到" + hits.totalHits + "個");
for (int i = 0; i < hits.scoreDocs.length; i++) {
ScoreDoc sdoc = hits.scoreDocs[i];
Document doc = indexSearch.doc(sdoc.doc);
System.out.println(doc.get("filename"));
Scorer scorer = new QueryScorer(query);
SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color=\"red\">", "</font></b>");
Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer);
String content = readFileAsString(dataDir+File.separator+doc.get("filename"));
TokenStream tokenStream = queryParser.getAnalyzer().tokenStream("contents", new StringReader(content));
String fragment = highlighter.getBestFragment(tokenStream, content);
System.out.println(fragment != null ? fragment : content);

}
indexSearch.close();
}
private static String readFileAsString(String filePath) throws java.io.IOException{
byte[] buffer = new byte[(int) new File(filePath).length()];
BufferedInputStream f = null;
try {
f = new BufferedInputStream(new FileInputStream(filePath));
f.read(buffer);
} finally {
if (f != null) try { f.close(); } catch (IOException ignored) { }
}
return new String(buffer);
}
}

[/code]

搜尋相關Tags的文章: [ java ] , [ 文件檢索 ] , [ 編程錯誤 ] ,
本篇文章發表於2019-01-06 10:18
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
回應

JavaHand
檢舉此回應
Searcher.java:16: error: package org.apache.lucene.search.highlight does not exist

第一行有提到 package 不存在
也就是你在 compile 時,javac 找不到 Lucene 的 jar 檔
本篇文章回覆於2019-01-11 10:50
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.