Skip to content

Commit 979a327

Browse files
committed
Fix realtime get nested fields with synthetic source
1 parent c7b61bd commit 979a327

File tree

4 files changed

+229
-43
lines changed

4 files changed

+229
-43
lines changed

muted-tests.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,6 @@ tests:
265265
- class: org.elasticsearch.lucene.RollingUpgradeSearchableSnapshotIndexCompatibilityIT
266266
method: testMountSearchableSnapshot {p0=[9.0.0, 9.0.0, 8.18.0]}
267267
issue: https://github.com/elastic/elasticsearch/issues/119551
268-
- class: org.elasticsearch.index.engine.LuceneSyntheticSourceChangesSnapshotTests
269-
method: testSkipNonRootOfNestedDocuments
270-
issue: https://github.com/elastic/elasticsearch/issues/119553
271268
- class: org.elasticsearch.lucene.RollingUpgradeSearchableSnapshotIndexCompatibilityIT
272269
method: testSearchableSnapshotUpgrade {p0=[9.0.0, 9.0.0, 8.18.0]}
273270
issue: https://github.com/elastic/elasticsearch/issues/119560

server/src/internalClusterTest/java/org/elasticsearch/get/GetActionIT.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.elasticsearch.action.get.MultiGetResponse;
2525
import org.elasticsearch.action.index.IndexRequest;
2626
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
27+
import org.elasticsearch.action.support.WriteRequest;
2728
import org.elasticsearch.action.support.broadcast.BroadcastResponse;
2829
import org.elasticsearch.common.Randomness;
2930
import org.elasticsearch.common.Strings;
@@ -35,6 +36,7 @@
3536
import org.elasticsearch.index.IndexModule;
3637
import org.elasticsearch.index.engine.EngineTestCase;
3738
import org.elasticsearch.index.engine.VersionConflictEngineException;
39+
import org.elasticsearch.index.mapper.SourceFieldMapper;
3840
import org.elasticsearch.plugins.Plugin;
3941
import org.elasticsearch.rest.RestStatus;
4042
import org.elasticsearch.test.ESIntegTestCase;
@@ -932,6 +934,102 @@ public void testGetRemoteIndex() {
932934
);
933935
}
934936

937+
public void testRealTimeGetNestedFields() {
938+
String index = "test";
939+
SourceFieldMapper.Mode sourceMode = randomFrom(SourceFieldMapper.Mode.values());
940+
assertAcked(
941+
prepareCreate(index).setMapping("title", "type=keyword", "author", "type=nested")
942+
.setSettings(
943+
indexSettings(1, 0).put("index.refresh_interval", -1)
944+
.put(SourceFieldMapper.INDEX_MAPPER_SOURCE_MODE_SETTING.getKey(), sourceMode)
945+
)
946+
);
947+
ensureGreen();
948+
String source0 = """
949+
{
950+
"title": "t0",
951+
"author": [
952+
{
953+
"name": "a0"
954+
}
955+
]
956+
}
957+
""";
958+
prepareIndex(index).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).setId("0").setSource(source0, XContentType.JSON).get();
959+
// start tracking translog locations
960+
assertTrue(client().prepareGet(index, "0").setRealtime(true).get().isExists());
961+
String source1 = """
962+
{
963+
"title": ["t1"],
964+
"author": [
965+
{
966+
"name": "a1"
967+
}
968+
]
969+
}
970+
""";
971+
prepareIndex(index).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).setId("1").setSource(source1, XContentType.JSON).get();
972+
String source2 = """
973+
{
974+
"title": ["t1", "t2"],
975+
"author": [
976+
{
977+
"name": "a1"
978+
},
979+
{
980+
"name": "a2"
981+
}
982+
]
983+
}
984+
""";
985+
prepareIndex(index).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).setId("2").setSource(source2, XContentType.JSON).get();
986+
String source3 = """
987+
{
988+
"title": ["t1", "t3", "t2"]
989+
}
990+
""";
991+
prepareIndex(index).setRefreshPolicy(WriteRequest.RefreshPolicy.NONE).setId("3").setSource(source3, XContentType.JSON).get();
992+
GetResponse translog1 = client().prepareGet(index, "1").setRealtime(true).get();
993+
GetResponse translog2 = client().prepareGet(index, "2").setRealtime(true).get();
994+
GetResponse translog3 = client().prepareGet(index, "3").setRealtime(true).get();
995+
assertTrue(translog1.isExists());
996+
assertTrue(translog2.isExists());
997+
assertTrue(translog3.isExists());
998+
switch (sourceMode) {
999+
case STORED -> {
1000+
assertThat(translog1.getSourceAsBytesRef().utf8ToString(), equalTo(source1));
1001+
assertThat(translog2.getSourceAsBytesRef().utf8ToString(), equalTo(source2));
1002+
assertThat(translog3.getSourceAsBytesRef().utf8ToString(), equalTo(source3));
1003+
}
1004+
case SYNTHETIC -> {
1005+
assertThat(translog1.getSourceAsBytesRef().utf8ToString(), equalTo("""
1006+
{"author":{"name":"a1"},"title":"t1"}"""));
1007+
assertThat(translog2.getSourceAsBytesRef().utf8ToString(), equalTo("""
1008+
{"author":[{"name":"a1"},{"name":"a2"}],"title":["t1","t2"]}"""));
1009+
assertThat(translog3.getSourceAsBytesRef().utf8ToString(), equalTo("""
1010+
{"title":["t1","t2","t3"]}"""));
1011+
}
1012+
case DISABLED -> {
1013+
assertNull(translog1.getSourceAsBytesRef());
1014+
assertNull(translog2.getSourceAsBytesRef());
1015+
assertNull(translog3.getSourceAsBytesRef());
1016+
}
1017+
}
1018+
assertFalse(client().prepareGet(index, "1").setRealtime(false).get().isExists());
1019+
assertFalse(client().prepareGet(index, "2").setRealtime(false).get().isExists());
1020+
assertFalse(client().prepareGet(index, "3").setRealtime(false).get().isExists());
1021+
refresh(index);
1022+
GetResponse lucene1 = client().prepareGet(index, "1").setRealtime(randomBoolean()).get();
1023+
GetResponse lucene2 = client().prepareGet(index, "2").setRealtime(randomBoolean()).get();
1024+
GetResponse lucene3 = client().prepareGet(index, "3").setRealtime(randomBoolean()).get();
1025+
assertTrue(lucene1.isExists());
1026+
assertTrue(lucene2.isExists());
1027+
assertTrue(lucene3.isExists());
1028+
assertThat(translog1.getSourceAsBytesRef(), equalTo(lucene1.getSourceAsBytesRef()));
1029+
assertThat(translog2.getSourceAsBytesRef(), equalTo(lucene2.getSourceAsBytesRef()));
1030+
assertThat(translog3.getSourceAsBytesRef(), equalTo(lucene3.getSourceAsBytesRef()));
1031+
}
1032+
9351033
private void assertGetFieldsAlwaysWorks(String index, String docId, String[] fields) {
9361034
assertGetFieldsAlwaysWorks(index, docId, fields, null);
9371035
}

0 commit comments

Comments
 (0)