PyLucene 尝试创建 FSDirectory 时出现 InvalidArgsError

InvalidArgsError in PyLucene trying to create FSDirectory

所以我正在尝试在 PyLucene 中实现一个基本的索引编写器。我通常是 java 开发人员,但由于技术限制,我在 python 中执行此操作,否则这不是问题。我正在关注 PyLucene Tarball 中的示例,但是

import lucene

from java.io import File
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.document import Document, Field
from org.apache.lucene.index import IndexWriter, IndexWriterConfig
from org.apache.lucene.store import SimpleFSDirectory
from org.apache.lucene.util import Version
from org.apache.lucene.store import IOContext

lucene.initVM()
fl = File('index')
indexDir = SimpleFSDirectory(fl)
writerConfig = IndexWriterConfig(Version.LUCENE_6_4_1, StandardAnalyzer())

我遇到的问题是,每当我 运行 出现以下错误时:

Traceback (most recent call last):
 File "Indexer.py", line 40, in <module>
indexer = Indexer()
 File "Indexer.py", line 22, in __init__
indexDir = SimpleFSDirectory(fl)
lucene.InvalidArgsError: (<type 'SimpleFSDirectory'>, '__init__', (<File: index>,))

我已经检查了 Java 代码 here 并且似乎有一个构造函数 public SimpleFSDirectory(File path) 看起来这就是我在回溯错误中传递的内容。我是否遗漏了 jcc 中的某些内容?

这是使用 Lucene 6.4.1,我可以成功导入 lucene 和 jcc。

所以一些文档有这个

fl = File('index')
indexDir = SimpleFSDirectory(fl)

在较新的版本中(我正在使用基于 Lucene 6.4.1 的 PyLucene)SimpleFSDirectory 期望 Path 而不是 File(这就是使用 [= 的乐趣python 中的 22=] 库:java 的简洁性和 python 的类型安全。)在上面我也没有意识到我必须 attachCurrentThread

更正后的代码:

path = Paths.get('index')
self.index_directory = SimpleFSDirectory(path)