java - 如何在 PDFTextStream 的 setFieldName 中获取文档的文件路径?

标签 java eclipse lucene indexing filepath

我在使用 PDFTextStreamLucene 索引 pdf 文件时遇到问题。问题是使用 PDFTextStream 的方法构建文本,但我无法获取存储在索引文件目录中的文档的文件路径,我尝试过:

setFieldName(file.getPath(),"path"); 但我无法获取文件路径。有什么建议么?

这是我的代码:

public class PDFDocument {
//Constructor vacío
 IndexWriter writer;
 File directorio;
public PDFDocument(){
    directorio= new File("C:/indexpdf");
}
/*Metódo estático para agregar un documento PDF a un IndexWriter de Lucene
 * pasando como parámetros IndexWriter, y el archivo PDF
 */

public void agregarPDFaIndex() throws IOException{
    writer= new IndexWriter(new File("C:/indexpdfsalida"), new StandardAnalyzer(), true);
    //Se crea e inicializa una nueva instancia de DocumentFactoryCofig
    DocumentFactoryConfig config= new DocumentFactoryConfig();;


    config.setCopyAllPDFAttrs(false);
    //los datos del documento PDF se almacenan, se tokenizan y se indexan
    config.setPDFAttrSettings(true, true, true);
    /*Se configuran los nombre explicitos que deben ser usados en los Fields 
     * que crean una nueva instancia de un Document de Lucene        * 
     */
    File[] files= directorio.listFiles();

    for(File file: files){
        if(file.canRead() && !file.isDirectory() && file.getName().endsWith(".pdf")){
            System.out.println("Indexando el archivo: "+file.getAbsolutePath());
            config.setMainTextFieldName("content");
            Document doc= new Document();

            config.setTextSettings(false, true, true);
            config.setFieldName(PDFTextStream.ATTR_AUTHOR, "autor");
            config.setFieldName(PDFTextStream.ATTR_CREATION_DATE, "fecha_creacion");
            config.setFieldName(PDFTextStream.ATTR_MOD_DATE,"ultimo_mod");
            config.setFieldName(PDFTextStream.ATTR_TITLE,"titulo");
            config.setFieldName(DocumentFactoryConfig.DEFAULT_MAIN_TEXT_FIELD_NAME, "content");
            config.setFieldName(PDFTextStream.ATTR_CREATOR,"creador");
            config.setFieldName(PDFTextStream.ATTR_PRODUCER, "productor");
            config.setFieldName(PDFTextStream.ATTR_SUBJECT, "asunto");
            config.setFieldName(file.getPath(), "path");

            doc= PDFDocumentFactory.buildPDFDocument(file, config);

            System.out.println(doc.get("path"));
            writer.addDocument(doc);
        }
    }
    writer.optimize();
    writer.close();
    System.out.println("OK");

}
public static void main(String[]args) throws IOException{
    PDFDocument doc= new PDFDocument();
    doc.agregarPDFaIndex();
}
}

最佳答案

我没有看到任何您可以在 PDFTextStream 中使用的常量。但是,在将其添加到索引之前,直接将其添加到由 PDFDocumentFactory 生成的 Lucene 文档中会非常简单:

doc= PDFDocumentFactory.buildPDFDocument(file, config);

doc.addField(new StringField("path", file.getPath(), Field.Store.YES));

System.out.println(doc.get("path"));
writer.addDocument(doc);

您可能希望根据需要使用不同的字段类型(StringField 用于精确匹配,TextField 用于全文搜索,StoredField 不在该字段上搜索)

关于java - 如何在 PDFTextStream 的 setFieldName 中获取文档的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24209003/

相关文章:

java - SolrJ 从 Java 连接到 Solr

java - Eclipse 崩溃无法打开

java - 为什么 .array() 对从映射的 FileChannels 返回的 ByteBuffers 不起作用?

eclipse - 忽略 FindBugs 的单个发现

java - 在 eclipse 中设置 java 配置。多个 .param 文件

solr - 如何在Solr 3.3中实现mod功能

.net - 什么是最好和最活跃的开源 .Net 搜索技术?

java - 并行流 Java 8 的 CPU 核心数

Java 区分大小写 : Class file collision: A resource exists with a different case

search - Elasticsearch 聚合列表中的二级排序