Skip to content

Commit 308adaa

Browse files
authored
Account for soft-deletes (#52781)
The default IndexWriterConfig is used during the initialization of searchable snapshots Directory instances. Since the snapshot might contain soft deletes, the IndexWriterConfig should also declares a soft delete field otherwise it might just fail opening some Lucene files.
1 parent f1f598e commit 308adaa

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

x-pack/plugin/searchable-snapshots/src/main/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotRepository.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77

88
import org.apache.lucene.index.IndexWriter;
99
import org.apache.lucene.index.IndexWriterConfig;
10+
import org.apache.lucene.index.NoMergePolicy;
1011
import org.apache.lucene.store.Directory;
1112
import org.elasticsearch.cluster.metadata.IndexMetaData;
1213
import org.elasticsearch.cluster.metadata.RepositoryMetaData;
1314
import org.elasticsearch.common.Strings;
1415
import org.elasticsearch.common.blobstore.BlobContainer;
16+
import org.elasticsearch.common.lucene.Lucene;
1517
import org.elasticsearch.common.settings.Setting;
1618
import org.elasticsearch.common.settings.Settings;
1719
import org.elasticsearch.index.IndexSettings;
@@ -93,7 +95,11 @@ private Directory makeDirectory(IndexSettings indexSettings, ShardPath shardPath
9395
}
9496
directory = new InMemoryNoOpCommitDirectory(directory);
9597

96-
try (IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig())) {
98+
final IndexWriterConfig indexWriterConfig = new IndexWriterConfig(null)
99+
.setSoftDeletesField(Lucene.SOFT_DELETES_FIELD)
100+
.setMergePolicy(NoMergePolicy.INSTANCE);
101+
102+
try (IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig)) {
97103
final Map<String, String> userData = new HashMap<>();
98104
indexWriter.getLiveCommitData().forEach(e -> userData.put(e.getKey(), e.getValue()));
99105

x-pack/plugin/searchable-snapshots/src/test/java/org/elasticsearch/xpack/searchablesnapshots/AbstractSearchableSnapshotsRestTestCase.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testSearchableSnapshots() throws Exception {
7171

7272
final StringBuilder bulkBody = new StringBuilder();
7373
for (int i = 0; i < numDocs; i++) {
74-
bulkBody.append("{\"index\":{}}\n");
74+
bulkBody.append("{\"index\":{\"_id\":\"").append(i).append("\"}}\n");
7575
bulkBody.append("{\"field\":").append(i).append(",\"text\":\"Document number ").append(i).append("\"}\n");
7676
}
7777

@@ -80,8 +80,21 @@ public void testSearchableSnapshots() throws Exception {
8080
documents.setJsonEntity(bulkBody.toString());
8181
assertOK(client().performRequest(documents));
8282

83+
if (randomBoolean()) {
84+
final StringBuilder bulkUpdateBody = new StringBuilder();
85+
for (int i = 0; i < randomIntBetween(1, numDocs); i++) {
86+
bulkUpdateBody.append("{\"update\":{\"_id\":\"").append(i).append("\"}}\n");
87+
bulkUpdateBody.append("{\"doc\":{").append("\"text\":\"Updated document number ").append(i).append("\"}}\n");
88+
}
89+
90+
final Request bulkUpdate = new Request(HttpPost.METHOD_NAME, '/' + indexName + "/_bulk");
91+
bulkUpdate.addParameter("refresh", Boolean.TRUE.toString());
92+
bulkUpdate.setJsonEntity(bulkUpdateBody.toString());
93+
assertOK(client().performRequest(bulkUpdate));
94+
}
95+
8396
logger.info("force merging index [{}]", indexName);
84-
forceMerge(indexName, true, true);
97+
forceMerge(indexName, randomBoolean(), randomBoolean());
8598

8699
final String snapshot = "searchable-snapshot";
87100

0 commit comments

Comments
 (0)