IndexReader.terms() は、オプションの Field() オブジェクトを受け入れます。フィールド オブジェクトは、フィールド名と値の 2 つの引数で構成され、lucene では「用語フィールド」と「用語テキスト」と呼ばれます。
Field 引数に「用語テキスト」の空の値を指定することで、関心のある用語から用語の反復を開始できます。
lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
# Query the lucene index for the terms starting at a term named "field_name"
terms = ireader.terms(Term("field_name", "")) #Start at the field "field_name"
facets = {'other': 0}
while terms.next():
if terms.term().field() != "field_name": #We've got every value
break
print "Field Name:", terms.term().field()
print "Field Value:", terms.term().text()
print "Matching Docs:", int(ireader.docFreq(term))
PyLucene でファセットを実行する方法を探している他の人がこの投稿に出くわすことを願っています。重要なのは、用語をそのまま索引付けすることです。完全を期すために、これはフィールド値にインデックスを付ける方法です。
dir = SimpleFSDirectory(File(indexdir))
analyzer = StandardAnalyzer(Version.LUCENE_30)
writer = IndexWriter(dir, analyzer, True, IndexWriter.MaxFieldLength(512))
print "Currently there are %d documents in the index..." % writer.numDocs()
print "Adding %s Documents to Index..." % docs.count()
for val in terms:
doc = Document()
#Store the field, as-is, with term-vectors.
doc.add(Field("field_name", val, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES))
writer.addDocument(doc)
writer.optimize()
writer.close()