使用Lucene(PyLucene)查找单个字段项

2024-10-02 00:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Lucene的术语向量相当陌生,我想确保我的术语收集尽可能有效。 我得到了唯一的术语,然后检索术语的docFreq()来执行刻面。在

我用以下方法从索引中收集所有文档术语:

lindex = SimpleFSDirectory(File(indexdir))
ireader = IndexReader.open(lindex, True)
terms = ireader.terms() #Returns TermEnum

这很好用,但是有没有一种方法只返回特定字段(跨所有文档)的条件-这样会更有效吗?在

例如:

^{pr2}$

Tags: 方法文档向量file术语termslucene陌生
1条回答
网友
1楼 · 发布于 2024-10-02 00:30:03

在IndexReader.terms()接受可选的Field()对象。 Field对象由两个参数组成:字段名和lucene称之为“Term Field”和“Term Text”的值。在

通过为“term text”提供一个空值的字段参数,我们可以从我们关心的术语开始术语迭代。在

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中执行刻面的人会看到这篇文章。关键是按原样索引术语。为了完整起见,这就是字段值的索引方式。在

^{pr2}$

相关问题 更多 >

    热门问题