private void checkHitsForUpdate(LuceneHits hits) { if (hits.length() == 0) { throw new LuceneIndexAccessException("The identifier returns no document."); } if (hits.length() > 1) { throw new LuceneIndexAccessException("The identifier returns more than one document."); } }
public void updateDocument( Term identifierTerm, DocumentModifier documentModifier, Analyzer analyzer) { LuceneIndexReader reader = IndexReaderFactoryUtils.getIndexReader(indexFactory); LuceneSearcher searcher = null; Document updatedDocument = null; try { searcher = reader.createSearcher(); LuceneHits hits = searcher.search(new TermQuery(identifierTerm)); checkHitsForUpdate(hits); updatedDocument = documentModifier.updateDocument(hits.doc(0)); } catch (Exception ex) { throw new LuceneIndexAccessException("Error during updating a document.", ex); } finally { SearcherFactoryUtils.closeSearcher(searcher); IndexReaderFactoryUtils.releaseIndexReader(indexFactory, reader); } deleteDocuments(identifierTerm); addDocument(updatedDocument, analyzer); }