java - 如何使用lucene索引pdf文件

标签 java mysql pdf lucene

我必须在我的项目中使用 lucene 创建一个全文搜索,所以我必须在 mysql 数据库中索引一个 blob 列(包含文件 pdf、doc、xsl、xml 和图像),我没有使用 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();


    }
}

我使用的搜索

    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) {
      }
}}

最佳答案

解析任何类型的文件使用Tika project ,然后用 Lucene 索引它。 Tika 已经包含了太多的 API (pdfBox....)

关于java - 如何使用lucene索引pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23762015/

相关文章:

java - 机器的IP地址

java - Sessionscoped托管bean不保存变量jsf

java - android 转换结果时出错 java.io.FileNotFoundException

MySQL 分组依据和计数...我可以进行嵌套分组吗?

pdf - 谷歌使用什么应用程序在 gmail 中显示 PDF 附件

java - 为什么在java中左移改变符号值

mysql - 使用 "reindexed"数组更新 MySQL 数据库

php - Mysql 在表上连接列,同时从另一个连接到 php

c# - 将图像渲染为 pdf 是模糊的

java - PDFBox IllegalArgumentException : No glyph in font Webdings