0

I want to index some files and retrieve some information from them.

I use this piece of code in python to index document:

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

but i faced with this error :

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'

How can i fix it?

Thanks

1 Answers1

0

It is solved now. I should write:

doc = Document();
NG_
  • 6,895
  • 7
  • 45
  • 67