# Common imports:
import sys
from os import path, listdir
from org.apache.lucene.document import Document, Field, StringField, TextField
from org.apache.lucene.util import Version
from org.apache.lucene.store import RAMDirectory
from datetime import datetime
# Indexer imports:
from org.apache.lucene.analysis.miscellaneous import LimitTokenCountAnalyzer
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriter, IndexWriterConfig
# from org.apache.lucene.store import SimpleFSDirectory
# Retriever imports:
from org.apache.lucene.search import IndexSearcher
from org.apache.lucene.index import DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
# ---------------------------- global constants ----------------------------- #
BASE_DIR = path.dirname(path.abspath(sys.argv[0]))
INPUT_DIR = BASE_DIR + "/input/"
INDEX_DIR = BASE_DIR + "/lucene_index/"
我在试着测试幽门烯库。我只为导入测试编写了这段代码。它不起作用。我得到了
bigissue@vmi995554:~/myluceneproj$ cd /home/bigissue/myluceneproj ; /usr/bin/env /usr/bin/python3.10 /home/bigissue/.vscode/extensions/ms-python.python-2022.16.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 36991 -- /home/bigissue/myluceneproj/hello_lucene.py
Traceback (most recent call last):
File "/home/bigissue/myluceneproj/hello_lucene.py", line 29, in <module>
from org.apache.lucene.document import Document, Field, StringField, TextField
ModuleNotFoundError: No module named 'org'
bigissue@vmi995554:~/myluceneproj$
我已经运行了python3.10 -m pip list
,并且有"lucene“模块。如果我导入lucene很好,但是python不识别org模块。为什么?
更新
我下载了Lucene9.1并设置了环境变量(/etc/environment
):
CLASSPATH=".:/usr/lib/jvm/temurin-17-jdk-amd64/lib:/home/bigissue/all_lucene/lucene-9.4.1/modules:/home/bigissue/all_lucene/lucene-9.1.0/modules" export CLASSPATH
我下载了幽门螺杆菌-9.1.0,并且我已经安装了第一个jcc
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0$ pwd
/home/bigissue/all_lucene/pylucene-9.1.0/jcc
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py build
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0$python3.10 setup.py install
我还下载了ant apache。
然后pylucene 9.1 cd ..
我有编辑Makefile vim /home/bigissue/all_lucene/pylucene-9.1.0/Makefile
PREFIX_PYTHON=/usr/bin
ANT=/home/bigissue/all_lucene/apache-ant-1.10.12
PYTHON=$(PREFIX_PYTHON)/python3.10
JCC=$(PYTHON) -m jcc --shared
NUM_FILES=10
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0: make
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0: make install
如果我运行python3.10 -m pip,那么我就会看到它。
bigissue@vmi995554:~/all_lucene/pylucene-9.1.0$ python3.10 -m pip list | grep -i "lucene"
lucene 9.1.0
现在我已经进口了lucene
import sys
from os import path, listdir
from lucene import *
directory = RAMDirectory()
但我得到
ImportError: cannot import name 'RAMDirectory' from 'lucene' (/usr/local/lib/python3.10/dist-packages/lucene-9.1.0-py3.10-linux-x86_64.egg/lucene/__init__.py)
发布于 2022-10-29 10:13:35
Python不使用这种导入。只要进口lucene就行。
如果这不能解决你的问题,对不起!
发布于 2022-10-29 10:15:14
您可以使用from lucene import whatever
。
参见特性文档,其中它声明:
“Java在PyLucene模块中的平面命名空间中公开了所有Java类。”
所以,在Java中使用import org.apache.lucene.index.IndexReader;
,而在PyLucene中使用from lucene import IndexReader
。
更新
关于您在对问题的评论中提到的最新错误:
ImportError: cannot import name 'RAMDirectory' from 'lucene'
Lucene的RAMDirectory
已经被废弃了很长时间,并且最终成为了Lucene的从9.0版中删除。
您可以使用不同的目录实现。
推荐:MMapDirectory
--但是还有其他选项,如ByteBuffersDirectory
(请注意,一个新的错误/问题应该通过提出一个新的问题来解决。)
https://stackoverflow.com/questions/74247605
复制相似问题