IndexReader.terms()接受一个可选的Field()对象。Field对象由两个参数组成,即字段名称和值,Lucene将其称为“Term Field”和“Term Text”。
通过提供一个带有空值“term text”的Field参数,我们可以从我们关心的术语开始进行术语迭代。
lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
terms = ireader.terms(Term("field_name", ""))
facets = {'other': 0}
while terms.next():
if terms.term().field() != "field_name":
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()
doc.add(Field("field_name", val, Field.Store.YES, Field.Index.NOT_ANALYZED, Field.TermVector.YES))
writer.addDocument(doc)
writer.optimize()
writer.close()