简体   繁体   中英

Lucene 3.4 overwriting the document in index

I am trying to add a document to the index, but when I try to run the following function, it returns numDocs as 1 and overwrites the document on the previous one! Am I doing something wrong? I did the same on another project and it worked fine!

    IndexWriter writer;
    Directory dir = FSDirectory.open(new File("/home/omid/Desktop/Indexes"));
    writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_34), true, IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new NumericField("id", Field.Store.YES, false).setIntValue(writer.numDocs()));
    doc.add(new NumericField("day", Field.Store.YES, false).setIntValue(13));
    doc.add(new NumericField("month", Field.Store.YES, false).setIntValue(1));
    doc.add(new NumericField("year", Field.Store.YES, false).setIntValue(1387));
    doc.add(new Field("content", new FileReader(new File(filename)), Field.TermVector.YES));
    doc.add(new Field("address", filename,Field.Store.YES, Field.Index.NOT_ANALYZED));
    writer.addDocument(doc);
    writer.close();
    mes = "Indexed Filename: " + filename;
    mes = mes + "<BR>Number of docs: " + writer.numDocs();

Is there anything changed from lucene 3.0 in 3.4 or I am doing it wr

The IndexWriter constructor 's third argument specifies whether the index should be created or not. If create=true , as in your example, any old index is deleted and replaced with the new one. If it's false, it will append to an existing index.

Also, this style of IndexWriter constructor is deprecated in 3.4, and absent in 4.x. It would be much better to pass an IndexWriterConfig into the IndexWriter , instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM