3

私は Lucene の用語ベクトルにかなり慣れていないので、用語の収集ができる限り効率的であることを確認したいと考えています。一意の用語を取得し、その用語の docFreq() を取得してファセットを実行しています。

以下を使用して、インデックスからすべてのドキュメント用語を収集しています。

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

これは問題なく動作しますが、(すべてのドキュメントにわたって) 特定のフィールドの用語のみを返す方法はありますか? その方が効率的ではないでしょうか?

そのような:

 ireader.terms(Field="country")
4

1 に答える 1

3

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()
于 2012-03-03T23:15:42.890 に答える