java - Lucene:建立索引

标签 java lucene

我很久以前就使用 Lucene 4.6 构建了一个项目。现在目前想升级到 7.3。

这个项目有三个文件,每个文件有一个类(与文件同名):Main、Indexer、Search。

我在 Indexer.java 中遇到问题,更具体地说是在 buildIndex() 中。

Main.java 中,我已经定义了数据目录的位置和索引所在的位置,并通过将路径发送到 buildIndex() 必须在哪里 build :

File dataDirectory = new File("C:\\datalocation");
File indexDirectory = new File("C:\\indexlocation");
(...)

IndexWriter index = Indexer.createIndex(indexDirectory);
Indexer.buildIndex(index, dataDirectory, indexDirectory);
Indexer.closeIndex(index);
System.out.println("Index built");

Indexer.java 内部:

static void buildIndex(IndexWriter index, File dataDirectory,
            File IndexDirectory) throws IOException {
        File[] files = dataDirectory.listFiles();
        for (int i = 0; i < files.length; i++) {
            Document document = new Document();

            Reader reader = new FileReader(files[i]);
//the following line is where error 1 appears:
            document.add(new Field("contents", reader));

            String path = files[i].getCanonicalPath();
//the following line is where error 2 appears:
            document.add(new Field("path", path, Field.Store.YES,Field.Index.NOT_ANALYZED));

            index.addDocument(document);
        }
    }

我遇到的问题是:

  1. The constructor Field(String, Reader) is undefined.

  2. Index cannot be resolved or is not a field

我该如何解决这个问题?

将参数“reader”转换为“IndexableFieldType”

将“阅读器”的类型更改为“IndexableFieldType”不是一个选项。

注意:数据目录路径中有一个 .txt 文件,其中只写了“Maven”。

最佳答案

Field 的这两个构造函数都标记为 deprecated在 Lucene 4 中,后来被删除。

推荐的类是 TextField在这两种情况下,或者也是 StringField对于第二个。

所以第一个看起来像:

document.add(new TextField("contents", reader));

第二个:

document.add(new StringField("path", path, Field.Store.YES));

请注意,我找不到与 Field.Index.NOT_ANALYZED 参数明确等效的参数,尽管(StringField 未标记化,TextField 就是,不知道跟这个有没有直接关系)。

关于java - Lucene:建立索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50794440/

相关文章:

java - libGDX 坐标系

java - 如何绑定(bind)两个对象的引用?

elasticsearch - 如何在ElasticSearch中标记罗马数字术语?

c# - Lucene 拼写检查器排名建议很奇怪

java - 扩展 Lucene 分析器

java - 模仿 Elasticsearch MatchQuery

java - 以下代码中的文件IO疑问

java - 匹配字符串中未被特定字符分隔的特定关键字

java - Netty 4 - 管道头部的出站消息被丢弃

java - Lucene 词干提取器之间的区别 : EnglishStemmer, PorterStemmer、LovinsStemmer