我对Lucene的术语向量很陌生-并希望确保我的术语收集工作尽可能高效.
我得到了唯一的术语,然后检索该术语的docFreq()以进行构面.
我正在使用以下命令从索引中收集所有文档术语:
lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
terms = ireader.terms() #Returns TermEnum
这很好用,但是有一种方法只能返回特定字段的术语(在所有文档中)-这样会更有效吗?
如:
ireader.terms(Field="country")
解决方法:
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中进行刻面的人会看到这篇文章.关键是按原样索引术语.仅出于完整性考虑,这就是应该为字段值建立索引的方式.
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()