【问题标题】:Tika in Action book examples Lucene StandardAnalyzer does not workTika in Action 书籍示例 Lucene StandardAnalyzer 不起作用
【发布时间】:2014-03-31 05:44:21
【问题描述】:

首先,对于 Tika 和 Lucene,我完全是个菜鸟。我正在通过 Tika in Action 书尝试示例。在第 5 章中给出了这个例子:

package tikatest01;

import java.io.File;
import org.apache.tika.Tika;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;

public class LuceneIndexer {

    private final Tika tika;
    private final IndexWriter writer;

    public LuceneIndexer(Tika tika, IndexWriter writer) {
        this.tika = tika;
        this.writer = writer;
    }

    public void indexDocument(File file) throws Exception {
        Document document = new Document();
        document.add(new Field(
            "filename", file.getName(),
            Store.YES, Index.ANALYZED));
        document.add(new Field(
            "fulltext", tika.parseToString(file),
            Store.NO, Index.ANALYZED));
        writer.addDocument(document);
    }
}

还有这个主要方法:

package tikatest01;

import java.io.File;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.tika.Tika;

public class TikaTest01 {

    public static void main(String[] args) throws Exception {

        String filename = "C:\\testdoc.pdf";
        File file = new File(filename);

        IndexWriter writer = new IndexWriter(
            new SimpleFSDirectory(file),
            new StandardAnalyzer(Version.LUCENE_30), 
            MaxFieldLength.UNLIMITED);
        try {
            LuceneIndexer indexer = new LuceneIndexer(new Tika(), writer);
            indexer.indexDocument(file);
            } 
        finally {
            writer.close();
            }
    }
}

我已将库 tika-app-1.5.jar、lucene-core-4.7.0.jar 和 lucene-analyzers-common-4.7.0.jar 添加到项目中。

问题:

在当前版本的 Lucene 中,Field.Index 已被弃用,我应该改用什么?

未找到 MaxFieldLength。我缺少导入?

【问题讨论】:

  • 使用 Lucene 3.6 或者更全面地学习所有这些 API。
  • 更全面地学习 API 正是我阅读这些书籍的原因。然而,一切似乎都写在 Lucene 3.x 上,而不是 4.x :S
  • 好的。部分回答了我的第二个问题。我需要将 lucene-analyzers-common-4.7.0.jar 添加到我的项目中并导入 org.apache.lucene.analysis.standard.StandardAnalyzer MaxFieldLength 问题仍然存在。为此更新了问题。

标签: java lucene apache-tika


【解决方案1】:

对于 Lucene 4.7,此索引器代码:

package tikatest01;

import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.tika.Tika;

public class LuceneIndexer {

    private final Tika tika;
    private final IndexWriter writer;

    public LuceneIndexer(Tika tika, IndexWriter writer) {
        this.tika = tika;
        this.writer = writer;
    }

    public void indexDocument(File file) throws Exception {
        Document document = new Document();
        document.add(new TextField(
                "filename", file.getName(), Store.YES));
        document.add(new TextField(
                "fulltext", tika.parseToString(file), Store.NO));
        writer.addDocument(document);
    }
}

主类的代码如下:

package tikatest01;

import java.io.File;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.apache.tika.Tika;

public class TikaTest01 {

    public static void main(String[] args) throws Exception {

        String dirname = "C:\\MyTestDir\\";
        File dir = new File(dirname);


        IndexWriter writer = new IndexWriter(
            new SimpleFSDirectory(dir), 
            new IndexWriterConfig(
                Version.LUCENE_47, 
                new StandardAnalyzer(Version.LUCENE_47)));
        try {
            LuceneIndexer indexer = new LuceneIndexer(new Tika(), writer);
            indexer.indexDocument(dir);
            } 
        finally {
            writer.close();
            }
    }
}

【讨论】:

    【解决方案2】:

    对于 Lucene 4.7,IndexWriter 没有这种构造函数 看看 API - http://lucene.apache.org/core/4_7_0/core/org/apache/lucene/index/IndexWriter.html

    它只显示了具有 2 个参数的构造函数,因此您需要将此示例应用于新的 Lucene API

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多