【问题标题】:PyLucene Indexer and retriever samplePyLucene 索引器和检索器示例
【发布时间】:2017-12-06 06:20:09
【问题描述】:

我是 Lucene 的新手。我想在 Python 3 中编写 PyLucene 6.5 的示例代码。我更改了 this 版本的示例代码。但是,我可以找到很少的文档,我不确定更改是否正确。

# 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()

此代码读取输入并存储在index 目录中。

# 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 检索(搜索),但它不会返回任何内容。它有什么问题?

【问题讨论】:

    标签: python python-3.x lucene pylucene


    【解决方案1】:

    我认为最好的入门方法是下载 PyLucene 的 tarball(您选择的版本):

    https://www.apache.org/dist/lucene/pylucene/

    在里面你会找到一个带有 python 测试的 test3/ 文件夹(对于 python3,否则 test2/ 对于 python2)。这些涵盖了常见的操作,例如索引、读取、搜索等等。鉴于 Pylucene 相关文档严重缺乏,我发现这些非常有用。

    尤其是test_Pylucene.py

    注意

    如果更改日志对您来说不够直观,这也是快速掌握更改并跨版本调整代码的好方法。

    (为什么我没有在这个答案中提供代码:在 SO 的 PyLucene 答案中提供代码 sn-ps 的问题是,一旦新版本发布,这些很快就会过时,正如我们在大多数现有的上所看到的那样。)

    【讨论】:

      【解决方案2】:
      In []: tft.indexOptions()
      Out[]: <IndexOptions: NONE>
      

      尽管有记录表明DOCS_AND_FREQS_AND_POSITIONS 是默认设置,但现在情况已不再如此。这是TextField 的默认值; FieldType 必须明确地 setIndexOptions

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-08
        • 2013-06-16
        • 2015-12-20
        相关资源
        最近更新 更多