IndexReader.terms() 接受一个可选的 Field() 对象。字段对象由两个参数组成,字段名称和值,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 中执行 faceting 的人会看到这篇文章。关键是按原样索引术语。只是为了完整起见,这就是字段值的索引方式。
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()