Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add updateDocuments API which accept a query (reopen) #12346

Merged
merged 4 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,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 @@ -1521,6 +1521,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 @@ -3476,7 +3476,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