Apache Lucene open-source search software
Support updateDocuments methods for ColumnBatch (#16168)
Prithvi S180be1179b26
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex d1ef8dce73c..ad7b71d0e74 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -345,6 +345,9 @@ New Features* GITHUB#16110: Add count() to BayesianScoreQuery.BayesianScoreWeight, delegating tothe inner weight. (Prithvi S)+* GITHUB#16117: Add updateDocuments(Term, ColumnBatch) and updateDocuments(Query, ColumnBatch)+ to IndexWriter for atomic delete-and-add columnar operations. (Prithvi S)+Improvements---------------------* GITHUB#15948: Improve BayesianScoreQuery and LogOddsFusionQuery with base rate prior,diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex d2be47854e8..bf806dd08c0 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -1537,6 +1537,40 @@ public class IndexWriterreturn updateBatch(null, columnBatch);}+ /**+ * Atomically deletes documents matching the provided term and adds a batch of documents in+ * column-oriented format.+ *+ * <p>See {@link #addBatch(ColumnBatch)}.+ *+ * @param delTerm the term to identify the documents to be deleted, or null if no deletion should+ * occur+ * @param columnBatch the column-oriented batch of documents to add+ * @return The <a href="#sequence_number">sequence number</a> for this operation+ * @throws IOException if there is a low-level IO error+ * @lucene.experimental+ */+ public long updateDocuments(Term delTerm, ColumnBatch columnBatch) throws IOException {+ return updateBatch(+ delTerm == null ? null : DocumentsWriterDeleteQueue.newNode(delTerm), columnBatch);+ }++ /**+ * Similar to {@link #updateDocuments(Term, ColumnBatch)}, but takes a query instead of a term to+ * identify the documents to be deleted.+ *+ * @param delQuery the query to identify the documents to be deleted, or null if no deletion+ * should occur+ * @param columnBatch the column-oriented batch of documents to add+ * @return The <a href="#sequence_number">sequence number</a> for this operation+ * @throws IOException if there is a low-level IO error+ * @lucene.experimental+ */+ public long updateDocuments(Query delQuery, ColumnBatch columnBatch) throws IOException {+ return updateBatch(+ delQuery == null ? null : DocumentsWriterDeleteQueue.newNode(delQuery), columnBatch);+ }+private long updateBatch(final DocumentsWriterDeleteQueue.Node<?> delNode, ColumnBatch columnBatch)throws IOException {diff --git a/lucene/core/src/test/org/apache/lucene/index/TestColumnBatchIndexing.java b/lucene/core/src/test/org/apache/lucene/index/TestColumnBatchIndexing.javaindex e531ff5f5f5..8435aa1fc28 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestColumnBatchIndexing.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestColumnBatchIndexing.java@@ -31,6 +31,7 @@ import static org.apache.lucene.document.column.ColumnBatchTestUtil.simpleBatch;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.Random;@@ -1773,4 +1774,102 @@ public class TestColumnBatchIndexing extends LuceneTestCase {}dir.close();}++ public void testUpdateDocumentsByTerm() throws IOException {+ Directory dir = newDirectory();+ try (IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {+ FieldType idType = new FieldType(StringField.TYPE_NOT_STORED);+ idType.freeze();++ BytesRef[] ids12 = new BytesRef[] {newBytesRef("1"), newBytesRef("2")};+ BytesRef[] ids3 = new BytesRef[] {newBytesRef("3")};++ // Add initial batch with string id field (indexed for TermQuery)+ w.addBatch(+ simpleBatch(+ 2,+ new ArrayBinaryColumn("id", idType, new int[] {0, 1}, ids12),+ new ArrayLongColumn(+ "value", NumericDocValuesField.TYPE, new int[] {0, 1}, new long[] {100, 200})));++ // Update documents where id="1": delete and add new batch+ w.updateDocuments(+ new Term("id", "1"),+ simpleBatch(+ 1,+ new ArrayBinaryColumn("id", idType, new int[] {0}, ids3),+ new ArrayLongColumn(+ "value", NumericDocValuesField.TYPE, new int[] {0}, new long[] {300})));++ w.forceMerge(1);+ try (DirectoryReader r = DirectoryReader.open(w)) {+ IndexSearcher s = new IndexSearcher(r);+ // Old doc with id="1" should be deleted+ assertEquals(0, s.count(new TermQuery(new Term("id", "1"))));+ // New doc with id="3" should exist+ assertEquals(1, s.count(new TermQuery(new Term("id", "3"))));+ // Doc with id="2" should still exist+ assertEquals(1, s.count(new TermQuery(new Term("id", "2"))));++ // Should have 2 live docs: value=200 and value=300+ assertEquals(2, r.numDocs());++ LeafReader leaf = getOnlyLeafReader(r);+ NumericDocValues valueDv = leaf.getNumericDocValues("value");+ assertNotNull(valueDv);++ List<Long> values = new ArrayList<>();+ while (valueDv.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {+ values.add(valueDv.longValue());+ }+ Collections.sort(values);+ assertEquals(2, values.size());+ assertEquals(200, values.get(0).longValue());+ assertEquals(300, values.get(1).longValue());+ }+ }+ dir.close();+ }++ public void testUpdateDocumentsByQuery() throws IOException {+ Directory dir = newDirectory();+ try (IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {+ FieldType idType = new FieldType(StringField.TYPE_NOT_STORED);+ idType.freeze();++ BytesRef[] ids12 = new BytesRef[] {newBytesRef("1"), newBytesRef("2")};+ BytesRef[] ids3 = new BytesRef[] {newBytesRef("3")};++ // Add initial batch with string id field (indexed for TermQuery)+ w.addBatch(+ simpleBatch(+ 2,+ new ArrayBinaryColumn("id", idType, new int[] {0, 1}, ids12),+ new ArrayLongColumn(+ "value", NumericDocValuesField.TYPE, new int[] {0, 1}, new long[] {100, 200})));++ // Update documents where id="2" using a Query: delete and add new batch+ w.updateDocuments(+ new TermQuery(new Term("id", "2")),+ simpleBatch(+ 1,+ new ArrayBinaryColumn("id", idType, new int[] {0}, ids3),+ new ArrayLongColumn(+ "value", NumericDocValuesField.TYPE, new int[] {0}, new long[] {300})));++ w.forceMerge(1);+ try (DirectoryReader r = DirectoryReader.open(w)) {+ IndexSearcher s = new IndexSearcher(r);+ // Old doc with id="2" should be deleted+ assertEquals(0, s.count(new TermQuery(new Term("id", "2"))));+ // New doc with id="3" should exist+ assertEquals(1, s.count(new TermQuery(new Term("id", "3"))));+ // Doc with id="1" should still exist+ assertEquals(1, s.count(new TermQuery(new Term("id", "1"))));++ assertEquals(2, r.numDocs());+ }+ }+ dir.close();+ }}