Add updateDocuments API which accept a query (#12341)

This commit is contained in:
Patrick Zhai 2023-06-01 04:37:04 -07:00 committed by GitHub
parent 4bf1b94209
commit 52ab16731e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 1 deletions

View File

@ -130,6 +130,9 @@ API Changes
* GITHUB#12268: Add BitSet.clear() without parameters for clearing the entire set
(Jonathan Ellis)
* GITHUB#12341: add new IndexWriter#updateDocuments(Query, Iterable<Document>) API
to update documents using a query. (Patrick Zhai)
New Features
---------------------

View File

@ -142,6 +142,10 @@ final class DocumentsWriterDeleteQueue implements Accountable, Closeable {
return new TermNode(term);
}
static Node<Query> newNode(Query query) {
return new QueryNode(query);
}
static Node<DocValuesUpdate[]> newNode(DocValuesUpdate... updates) {
return new DocValuesUpdatesNode(updates);
}
@ -437,6 +441,23 @@ final class DocumentsWriterDeleteQueue implements Accountable, Closeable {
}
}
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);

View File

@ -1521,6 +1521,19 @@ public class IndexWriter
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)

View File

@ -3476,7 +3476,12 @@ public class TestIndexWriter extends LuceneTestCase {
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),