|
24 | 24 | import org.elasticsearch.action.get.MultiGetResponse;
|
25 | 25 | import org.elasticsearch.action.index.IndexRequest;
|
26 | 26 | import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
| 27 | +import org.elasticsearch.action.support.WriteRequest; |
27 | 28 | import org.elasticsearch.action.support.broadcast.BroadcastResponse;
|
28 | 29 | import org.elasticsearch.common.Randomness;
|
29 | 30 | import org.elasticsearch.common.Strings;
|
|
35 | 36 | import org.elasticsearch.index.IndexModule;
|
36 | 37 | import org.elasticsearch.index.engine.EngineTestCase;
|
37 | 38 | import org.elasticsearch.index.engine.VersionConflictEngineException;
|
| 39 | +import org.elasticsearch.index.mapper.SourceFieldMapper; |
38 | 40 | import org.elasticsearch.plugins.Plugin;
|
39 | 41 | import org.elasticsearch.rest.RestStatus;
|
40 | 42 | import org.elasticsearch.test.ESIntegTestCase;
|
@@ -932,6 +934,102 @@ public void testGetRemoteIndex() {
|
932 | 934 | );
|
933 | 935 | }
|
934 | 936 |
|
| 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 | + |
935 | 1033 | private void assertGetFieldsAlwaysWorks(String index, String docId, String[] fields) {
|
936 | 1034 | assertGetFieldsAlwaysWorks(index, docId, fields, null);
|
937 | 1035 | }
|
|
0 commit comments