Skip to content

Commit

Permalink
Add updateDocuments API which accept a query (reopen) (#12346)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaih committed Jun 4, 2023
1 parent 349b458 commit d76dd26
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ API Changes
* GITHUB#12268: Add BitSet.clear() without parameters for clearing the entire set
(Jonathan Ellis)

* GITHUB#12346: add new IndexWriter#updateDocuments(Query, Iterable<Document>) API
to update documents atomically, with respect to refresh and commit using a query. (Patrick Zhai)

New Features
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ static Node<Term> newNode(Term term) {
return new TermNode(term);
}

static Node<Query> newNode(Query query) {
return new QueryNode(query);
}

static Node<DocValuesUpdate[]> newNode(DocValuesUpdate... updates) {
return new DocValuesUpdatesNode(updates);
}
Expand Down Expand Up @@ -437,6 +441,23 @@ public String toString() {
}
}

private static final class QueryNode extends Node<Query> {

QueryNode(Query query) {
super(query);
}

@Override
void apply(BufferedUpdates bufferedDeletes, int docIDUpto) {
bufferedDeletes.addQuery(item, docIDUpto);
}

@Override
public String toString() {
return "del=" + item;
}
}

private static final class QueryArrayNode extends Node<Query[]> {
QueryArrayNode(Query[] query) {
super(query);
Expand Down
13 changes: 13 additions & 0 deletions lucene/core/src/java/org/apache/lucene/index/IndexWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,19 @@ public long updateDocuments(
delTerm == null ? null : DocumentsWriterDeleteQueue.newNode(delTerm), docs);
}

/**
* Similar to {@link #updateDocuments(Term, Iterable)}, but take a query instead of a term to
* identify the documents to be updated
*
* @lucene.experimental
*/
public long updateDocuments(
Query delQuery, Iterable<? extends Iterable<? extends IndexableField>> docs)
throws IOException {
return updateDocuments(
delQuery == null ? null : DocumentsWriterDeleteQueue.newNode(delQuery), docs);
}

private long updateDocuments(
final DocumentsWriterDeleteQueue.Node<?> delNode,
Iterable<? extends Iterable<? extends IndexableField>> docs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3473,7 +3473,12 @@ public int numDeletesToMerge(
Document doc = new Document();
doc.add(new StringField("id", id, Field.Store.YES));
if (mixDeletes && random().nextBoolean()) {
writer.updateDocuments(new Term("id", id), Arrays.asList(doc, doc));
if (random().nextBoolean()) {
writer.updateDocuments(new Term("id", id), Arrays.asList(doc, doc));
} else {
writer.updateDocuments(
new TermQuery(new Term("id", id)), Arrays.asList(doc, doc));
}
} else {
writer.softUpdateDocuments(
new Term("id", id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import org.apache.lucene.internal.tests.IndexWriterAccess;
import org.apache.lucene.internal.tests.TestSecrets;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.analysis.MockAnalyzer;
import org.apache.lucene.tests.util.LuceneTestCase;
Expand Down Expand Up @@ -283,7 +284,12 @@ public long updateDocuments(
w.softUpdateDocuments(
delTerm, docs, new NumericDocValuesField(config.getSoftDeletesField(), 1));
} else {
seqNo = w.updateDocuments(delTerm, docs);
if (r.nextInt(10) < 3) {
// 30% chance
seqNo = w.updateDocuments(new TermQuery(delTerm), docs);
} else {
seqNo = w.updateDocuments(delTerm, docs);
}
}
maybeFlushOrCommit();
return seqNo;
Expand Down

0 comments on commit d76dd26

Please sign in to comment.