JV_*_*_MI 1 java mysql pdf lucene
我必须在我的项目中使用lucene创建全文搜索,所以我必须索引mysql数据库中的blob列(包含文件pdf,doc,xsl,xml和image),使用doc,xsl和xml我没有任何问题但是使用pdf文件我无法获得结果
public class Indexfile {
public static void main(String[] args) throws Exception {
RemoteControlServiceConnection a = new RemoteControlServiceConnection(
"jdbc:mysql://localhost:3306/Test","root", "root" );
Connection conn = a.getConnexionMySQL();
final File INDEX_DIR = new File("index");
IndexWriter writer = new IndexWriter(INDEX_DIR,
new StandardAnalyzer(),
true);
String query = "SELECT id, name ,document FROM Table_document";
Statement statement = conn.createStatement();
ResultSet result = statement.executeQuery(query);
while (result.next()) {
Document document = new Document();
document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NO));
document.add(new Field("name", result.getString("name"), Field.Store.YES, Field.Index.TOKENIZED));
document.add(new Field("document", result.getString("document"), Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(text);
}
}
writer.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我用的搜索
public class searchlucene {
public static void main(String[] args) throws Exception {
StandardAnalyzer analyzer = new StandardAnalyzer();
String qu = "montbel*"; // put your keyword here
// String IndexStoreDir = "index-directory";
try {
Query q = new QueryParser("document", analyzer).parse(qu);
int hitspp = 100; //hits per page
IndexSearcher searcher = new IndexSearcher(IndexReader.open("index"));
TopDocCollector collector = new TopDocCollector(hitspp);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
System.out.println("Found " + hits.length + " hits.");
for (int i = 0; i < hits.length; ++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("name"));
}
searcher.close();
} catch (Exception ex1) {
}
}}
Run Code Online (Sandbox Code Playgroud)