Jak uzyskać listę wszystkich tokenów z indeksu Lucene 8.6.1 przy użyciu PyLucene?

Nov 22 2020

Z tego pytania mam pewne wskazówki . Najpierw tworzę indeks jak poniżej.

import lucene
from  org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.index import IndexWriterConfig, IndexWriter, DirectoryReader
from org.apache.lucene.store import SimpleFSDirectory
from java.nio.file import Paths
from org.apache.lucene.document import Document, Field, TextField
from org.apache.lucene.util import BytesRefIterator

index_path = "./index"

lucene.initVM()

analyzer = StandardAnalyzer()
config = IndexWriterConfig(analyzer)
if len(os.listdir(index_path))>0:
    config.setOpenMode(IndexWriterConfig.OpenMode.APPEND)

store = SimpleFSDirectory(Paths.get(index_path))
writer = IndexWriter(store, config)

doc = Document()
doc.add(Field("docid", "1",  TextField.TYPE_STORED))
doc.add(Field("title", "qwe rty", TextField.TYPE_STORED))
doc.add(Field("description", "uio pas", TextField.TYPE_STORED))
writer.addDocument(doc)

writer.close()
store.close()

Następnie próbuję uzyskać wszystkie terminy w indeksie dla jednego pola, jak poniżej.

store = SimpleFSDirectory(Paths.get(index_path))
reader = DirectoryReader.open(store)

Próba 1: próba użycia metody next()użytej w tym pytaniu, która wydaje się być metodą BytesRefIteratorzaimplementowaną przez TermsEnum.

for lrc in reader.leaves():
    terms = lrc.reader().terms('title')
    terms_enum = terms.iterator()
    while terms_enum.next():
        term = terms_enum.term()
        print(term.utf8ToString())

Jednak wydaje mi się, że nie mam dostępu do tej next()metody.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-47-6515079843a0> in <module>
      2     terms = lrc.reader().terms('title')
      3     terms_enum = terms.iterator()
----> 4     while terms_enum.next():
      5         term = terms_enum.term()
      6         print(term.utf8ToString())

AttributeError: 'TermsEnum' object has no attribute 'next'

Próba 2: próba zmiany pętli while zgodnie z sugestią w komentarzach do tego pytania .

while next(terms_enum):
    term = terms_enum.term()
    print(term.utf8ToString())

Jednak wydaje się, że TermsEnumnie jest to iterator w Pythonie.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-48-d490ad78fb1c> in <module>
      2     terms = lrc.reader().terms('title')
      3     terms_enum = terms.iterator()
----> 4     while next(terms_enum):
      5         term = terms_enum.term()
      6         print(term.utf8ToString())

TypeError: 'TermsEnum' object is not an iterator

Zdaję sobie sprawę, że na moje pytanie można odpowiedzieć zgodnie z sugestią zawartą w tym pytaniu . W takim razie wydaje mi się, że moje pytanie naprawdę brzmi: jak uzyskać wszystkie warunki TermsEnum?

Odpowiedzi

1 PSK Nov 23 2020 at 00:46

Okazało się, że poniżej prac z tu i ze test_FieldEnumeration()w test_Pylucene.pypliku, który jest w pylucene-8.6.1/test3/.

for term in BytesRefIterator.cast_(terms_enum):
    print(term.utf8ToString())

Z przyjemnością przyjmuję odpowiedź, która ma więcej wyjaśnień niż to.