我想索引一些文件并从中检索一些信息。
我在 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'
我该如何解决?
谢谢