Python PyLucene索引器和检索器样本

Python PyLucene索引器和检索器样本,python,python-3.x,lucene,pylucene,Python,Python 3.x,Lucene,Pylucene,我是Lucene的新手。我想用Python 3编写PyLucene 6.5的示例代码。我更改了版本的示例代码。然而,我能找到的文档很少,我不确定更改是否正确 # indexer.py import sys import lucene from java.io import File from org.apache.lucene.analysis.standard import StandardAnalyzer from org.apache.lucene.document import Doc

我是Lucene的新手。我想用Python 3编写PyLucene 6.5的示例代码。我更改了版本的示例代码。然而,我能找到的文档很少,我不确定更改是否正确

# indexer.py
import sys
import lucene

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

if __name__ == "__main__":
    lucene.initVM()
    indexPath = File("index/").toPath()
    indexDir = FSDirectory.open(indexPath)
    writerConfig = IndexWriterConfig(StandardAnalyzer())
    writer = IndexWriter(indexDir, writerConfig)

    print("%d docs in index" % writer.numDocs())
    print("Reading lines from sys.stdin...")

    tft = FieldType()
    tft.setStored(True)
    tft.setTokenized(True)
    for n, l in enumerate(sys.stdin):
        doc = Document()
        doc.add(Field("text", l, tft))
        writer.addDocument(doc)
    print("Indexed %d lines from stdin (%d docs in index)" % (n, writer.numDocs()))
    print("Closing index of %d docs..." % writer.numDocs())
    writer.close()
此代码读取输入并存储在
索引
目录中

# retriever.py
import sys
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.search import IndexSearcher
from org.apache.lucene.index import IndexReader, DirectoryReader
from org.apache.lucene.queryparser.classic import QueryParser
from org.apache.lucene.store import SimpleFSDirectory, FSDirectory
from org.apache.lucene.util import Version

if __name__ == "__main__":
    lucene.initVM()
    analyzer = StandardAnalyzer()
    indexPath = File("index/").toPath()
    indexDir = FSDirectory.open(indexPath)
    reader = DirectoryReader.open(indexDir)
    searcher = IndexSearcher(reader)

    query = QueryParser("text", analyzer).parse("hello")
    MAX = 1000
    hits = searcher.search(query, MAX)

    print("Found %d document(s) that matched query '%s':" % (hits.totalHits, query))
    for hit in hits.scoreDocs:
        print(hit.score, hit.doc, hit.toString())
        doc = searcher.doc(hit.doc)
        print(doc.get("text").encode("utf-8"))
我们应该能够使用
retriever.py
检索(搜索),但它不会返回任何内容。有什么问题吗?

在[]:tft.indexOptions()中
In []: tft.indexOptions()
Out[]: <IndexOptions: NONE>
出[]:
尽管有文件证明这是默认情况,但情况已不再如此。这是
文本字段的默认值;

FieldType
必须
setIndexOptions
明确。

我认为您开始的最佳方式是下载PyLucene的tarball(您选择的版本):

在里面,您将找到一个包含python测试的
test3/
文件夹(对于python3,或者
test2/
对于python2)。这些包括索引、读取、搜索等常见操作。我发现这些非常有用,因为Pylucene周围的文档非常缺乏

特别是检查
测试

注意

如果变更日志对您来说不够直观,那么这也是一种很好的方法,可以快速掌握变更并跨版本调整代码

(为什么我不在这个答案中提供代码:在SO的PyLucene答案中提供代码片段的问题是,新版本发布后,这些代码片段很快就会过时,我们可以在大多数现有版本中看到这一点。)