【问题标题】:error in indexing document with pylucene使用 pylucene 索引文档时出错
【发布时间】:2016-01-17 10:46:49
【问题描述】:

我想索引一些文件并从中检索一些信息。

我在python中使用这段代码来索引文档:

import sys ;
import lucene ;

from java.io import File ;
from org.apache.lucene.analysis.standard import StandardAnalyzer ;
from org.apache.lucene.document import Document, Field ;
from org.apache.lucene.index import IndexWriter, IndexWriterConfig ;
from org.apache.lucene.store import SimpleFSDirectory ;
from org.apache.lucene.util import Version ;

if __name__ == '__main__':
lucene.initVM();
indexDir = SimpleFSDirectory(File("myProjects/python/")) ;
writerConfig =
IndexWriterConfig(Version.LUCENE_4_10_1,StandardAnalyzer());
writer = IndexWriter(indexDir,writerConfig);

print "%d docs in index" % writer.numDocs();
print "Reading lines from sys.stdin..."
n = 0;
l = 0;
for n, l in enumerate(sys.stdin):
    doc = Document ;
    doc.add(Field("text", l, Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);
print "Indexed %d lines from stdin (%d docs in index)" %
(n,writer.numDocs());
print "Closing index of %d docs..." % writer.numDocs();
writer.close();

但我遇到了这个错误:

Traceback (most recent call last):
File "/home/ahoora/myProjects/python/index.py", line 24, in <module>
doc.add(Field("text", l, Field.Store.YES, Field.Index.ANALYZED));
TypeError: descriptor 'add' requires a 'Document' object but received
a 'Field'

我该如何解决?

谢谢

【问题讨论】:

    标签: lucene information-retrieval pylucene


    【解决方案1】:

    现在解决了。我应该写:

    doc = Document();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      相关资源
      最近更新 更多