On Thu, Jan 16, 2014 at 6:30 AM, Mindaugas Žakšauskas <min...@gmail.com> wrote: > Hi, > > I was wondering what would be the best approach to deal with the > situation when some documents are deleted and it is unclear on whether > deletions have resulted any pending commits. > > In a single thread scenario this seems to be as simple as > > 1 indexWriter.deleteDocuments(query); // same for terms arg > 2 if (indexWriter.hasUncommittedChanges()) { > 3 indexWriter.commit(); > 4 }
hasUncommittedChanges will return true if you deleted (by Term or Query), even if that Term or Query matches no documents. > However, I am not sure if this works as nicely in multi threaded > environment, where a possible race condition can happen between lines > 2 & 3. Yeah. > How feasible would it be for .deleteDocuments() to return a number > telling how many documents have actually been deleted? This isn't really possible: IndexWriter just buffers up any pending deleted Term/Query, because resolving those to the actual docIDs is so costly (requires opening SegmentReader for each segment, looking up all deleted Term or running all deleted Query). It only resolves once too much RAM is used by the buffered deletes, or once commit is called, or when a merge needs to kick off. > Also, how cheap is .hasUncommittedChanges()? - my quick code scan > shows that there's no I/O is involved, but maybe somebody can confirm > this? It should be cheap ... just several volatile reads (e.g. AtomicInt/Long). > And if this is cheap, why this method isn't invoked in the > beginning of indexWriter.commit()? Just history :) That method was added waay after IW.commit was added. But then maybe also paranoia/defensive: it would be bad if a bug in hasUncommittedChanges meant that commit() failed to write stuff to disk. Better to isolate the bug in that case. > Thank you. You're welcome! Mike McCandless http://blog.mikemccandless.com --------------------------------------------------------------------- To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org For additional commands, e-mail: java-user-h...@lucene.apache.org