2012-02-01 18 views
3

私はLuceneのTerm Vectorsにはかなり新しく、自分の用語集が可能な限り効率的であることを確認したいと考えています。 私は一意の用語を取得してから、ファセットを実行するために用語のdocFreq()を取得します。Lucene(PyLucene)との単一フィールド用語の検索

私が使用してインデックスからすべての文書の用語を収集しています:

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

をこれが正常に動作しますが、唯一の特定のフィールド(すべての文書全体)のための条件を返す方法がある - それはないだろうもっと効率的?

ような:

ireader.terms(Field="country") 
+0

私はこれが解決策になるかもしれないと思う... http://wiki.apache.org/lucene-java/LuceneFAQ#How_do_I_retrieve_all_the_values_of_a_particular_field_that_exists_within_an_index.2C_across_all_documents.3F –

答えて

3

IndexReader.terms()オプションのフィールド()オブジェクトを受け付けます。 フィールドオブジェクトは、2つの引数、フィールド名、および "用語フィールド"と "用語テキスト"を呼び出す値から構成されます。

'term text'に空の値を指定してField引数を指定することで、関連する用語で用語の繰り返しを開始できます。 PyLuceneにファセットを実行する方法を探して

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)) 

うまくいけば、他の人は、この記事に出くわし表示されます。キーは、そのまま用語を索引付けすることです。完全性のために、これはフィールド値をどのように索引付けするかです。

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() 
関連する問題