Skip to content

Commit b87132c

Browse files
authored
Fix realtime get of numeric fields (#58121)
Using realtime get on numeric fields when reading from the translog would yield a ClassCastException. Closes #57462
1 parent c7cbf80 commit b87132c

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

server/src/main/java/org/elasticsearch/index/get/ShardGetService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ private GetResult innerGetLoadFromStoredFields(String id, String[] storedFields,
255255
DocValuesType.NONE, -1, Collections.emptyMap(), 0, 0, 0, false);
256256
StoredFieldVisitor.Status status = fieldVisitor.needsField(fieldInfo);
257257
if (status == StoredFieldVisitor.Status.YES) {
258-
if (indexableField.binaryValue() != null) {
258+
if (indexableField.numericValue() != null) {
259+
fieldVisitor.objectField(fieldInfo, indexableField.numericValue());
260+
} else if (indexableField.binaryValue() != null) {
259261
fieldVisitor.binaryField(fieldInfo, indexableField.binaryValue());
260262
} else if (indexableField.stringValue() != null) {
261263
fieldVisitor.objectField(fieldInfo, indexableField.stringValue());
262-
} else if (indexableField.numericValue() != null) {
263-
fieldVisitor.objectField(fieldInfo, indexableField.numericValue());
264264
}
265265
} else if (status == StoredFieldVisitor.Status.STOP) {
266266
break;

server/src/test/java/org/elasticsearch/index/shard/ShardGetServiceTests.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,30 @@ public void testGetForUpdate() throws IOException {
9191
closeShards(primary);
9292
}
9393

94-
public void testGetFromTranslogWithSourceMappingOptionsAndStoredFields() throws IOException {
94+
public void testGetFromTranslogWithStringSourceMappingOptionsAndStoredFields() throws IOException {
95+
String docToIndex = "{\"foo\" : \"foo\", \"bar\" : \"bar\"}";
96+
boolean noSource = randomBoolean();
97+
String sourceOptions = noSource ? "\"enabled\": false" : randomBoolean() ? "\"excludes\": [\"fo*\"]" : "\"includes\": [\"ba*\"]";
98+
runGetFromTranslogWithOptions(docToIndex, sourceOptions, noSource ? "" : "{\"bar\":\"bar\"}", "\"text\"", "foo");
99+
}
100+
101+
public void testGetFromTranslogWithLongSourceMappingOptionsAndStoredFields() throws IOException {
102+
String docToIndex = "{\"foo\" : 7, \"bar\" : 42}";
103+
boolean noSource = randomBoolean();
104+
String sourceOptions = noSource ? "\"enabled\": false" : randomBoolean() ? "\"excludes\": [\"fo*\"]" : "\"includes\": [\"ba*\"]";
105+
runGetFromTranslogWithOptions(docToIndex, sourceOptions, noSource ? "" : "{\"bar\":42}", "\"long\"", 7L);
106+
}
107+
108+
private void runGetFromTranslogWithOptions(String docToIndex, String sourceOptions, String expectedResult, String fieldType,
109+
Object expectedFooVal) throws IOException {
95110
Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)
96111
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
97112
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
98113
.build();
99-
String docToIndex = "{\"foo\" : \"foo\", \"bar\" : \"bar\"}";
100-
boolean noSource = randomBoolean();
101-
String sourceOptions = noSource ? "\"enabled\": false" : randomBoolean() ? "\"excludes\": [\"fo*\"]" : "\"includes\": [\"ba*\"]";
102-
String expectedResult = noSource ? "" : "{\"bar\":\"bar\"}";
114+
103115
IndexMetadata metadata = IndexMetadata.builder("test")
104-
.putMapping("{ \"properties\": { \"foo\": { \"type\": \"text\", \"store\": true }, " +
105-
"\"bar\": { \"type\": \"text\"}}, \"_source\": { "
106-
+ sourceOptions + "}}}")
116+
.putMapping("{ \"properties\": { \"foo\": { \"type\": " + fieldType + ", \"store\": true }, " +
117+
"\"bar\": { \"type\": " + fieldType + "}}, \"_source\": { " + sourceOptions + "}}}")
107118
.settings(settings)
108119
.primaryTerm(0, 1).build();
109120
IndexShard primary = newShard(new ShardId(metadata.getIndex(), 0), true, "n1", metadata, null);
@@ -138,7 +149,7 @@ public void testGetFromTranslogWithSourceMappingOptionsAndStoredFields() throws
138149
assertEquals(new String(testGet2.source() == null ? new byte[0] : testGet2.source(), StandardCharsets.UTF_8), expectedResult);
139150
assertTrue(testGet2.getFields().containsKey(RoutingFieldMapper.NAME));
140151
assertTrue(testGet2.getFields().containsKey("foo"));
141-
assertEquals("foo", testGet2.getFields().get("foo").getValue());
152+
assertEquals(expectedFooVal, testGet2.getFields().get("foo").getValue());
142153
try (Engine.Searcher searcher = primary.getEngine().acquireSearcher("test", Engine.SearcherScope.INTERNAL)) {
143154
assertEquals(searcher.getIndexReader().maxDoc(), 2); // we read from the translog
144155
}
@@ -152,7 +163,7 @@ public void testGetFromTranslogWithSourceMappingOptionsAndStoredFields() throws
152163
assertEquals(new String(testGet2.source() == null ? new byte[0] : testGet2.source(), StandardCharsets.UTF_8), expectedResult);
153164
assertTrue(testGet2.getFields().containsKey(RoutingFieldMapper.NAME));
154165
assertTrue(testGet2.getFields().containsKey("foo"));
155-
assertEquals("foo", testGet2.getFields().get("foo").getValue());
166+
assertEquals(expectedFooVal, testGet2.getFields().get("foo").getValue());
156167

157168
closeShards(primary);
158169
}

0 commit comments

Comments
 (0)