Skip to content

Commit f7b771c

Browse files
committed
Change
1 parent feb815c commit f7b771c

File tree

9 files changed

+72
-20
lines changed

9 files changed

+72
-20
lines changed

server/src/main/java/org/elasticsearch/action/index/ModernSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public ModernSource(BytesReference originalSource, XContentType contentType, int
4646
this.structuredSource = structuredSource;
4747
}
4848

49+
public ModernSource(BytesReference source) {
50+
this(source, XContentHelper.xContentType(source));
51+
}
52+
4953
public void ensureStructured() {
5054
if (structuredSource == null) {
5155
assert originalSource != null;

server/src/main/java/org/elasticsearch/index/IndexingSlowLog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,11 @@ private static Map<String, Object> prepareMap(
229229
map.put("elasticsearch.slowlog.routing", doc.routing());
230230
}
231231

232-
if (maxSourceCharsToLog == 0 || doc.source() == null || doc.source().length() == 0) {
232+
if (maxSourceCharsToLog == 0 || doc.bytesSource() == null || doc.bytesSource().length() == 0) {
233233
return map;
234234
}
235235
try {
236-
String source = XContentHelper.convertToJson(doc.source(), reformat, doc.getXContentType());
236+
String source = XContentHelper.convertToJson(doc.bytesSource(), reformat, doc.getXContentType());
237237
String trim = Strings.cleanTruncate(source, maxSourceCharsToLog).trim();
238238
StringBuilder sb = new StringBuilder(trim);
239239
StringBuilders.escapeJson(sb, 0);

server/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.elasticsearch.action.ActionListener;
4242
import org.elasticsearch.action.admin.indices.flush.FlushRequest;
4343
import org.elasticsearch.action.index.IndexRequest;
44+
import org.elasticsearch.action.index.ModernSource;
4445
import org.elasticsearch.action.support.PlainActionFuture;
4546
import org.elasticsearch.action.support.SubscribableListener;
4647
import org.elasticsearch.action.support.UnsafePlainActionFuture;
@@ -1921,12 +1922,16 @@ public List<LuceneDocument> docs() {
19211922
}
19221923

19231924
public BytesReference source() {
1925+
return this.doc.bytesSource();
1926+
}
1927+
1928+
public ModernSource modernSource() {
19241929
return this.doc.source();
19251930
}
19261931

19271932
@Override
19281933
public int estimatedSizeInBytes() {
1929-
return (id().length() * 2) + source().length() + 12;
1934+
return (id().length() * 2) + this.doc.source().originalSourceSize() + 12;
19301935
}
19311936

19321937
/**

server/src/main/java/org/elasticsearch/index/mapper/ParsedDocument.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.apache.lucene.document.Field;
1313
import org.apache.lucene.document.StoredField;
1414
import org.apache.lucene.util.BytesRef;
15+
import org.elasticsearch.action.index.ModernSource;
1516
import org.elasticsearch.common.bytes.BytesArray;
1617
import org.elasticsearch.common.bytes.BytesReference;
1718
import org.elasticsearch.index.mapper.MapperService.MergeReason;
@@ -37,7 +38,7 @@ public class ParsedDocument {
3738

3839
private final long normalizedSize;
3940

40-
private BytesReference source;
41+
private ModernSource source;
4142
private XContentType xContentType;
4243
private Mapping dynamicMappingsUpdate;
4344

@@ -102,6 +103,30 @@ public ParsedDocument(
102103
XContentType xContentType,
103104
Mapping dynamicMappingsUpdate,
104105
long normalizedSize
106+
) {
107+
this(
108+
version,
109+
seqID,
110+
id,
111+
routing,
112+
documents,
113+
new ModernSource(source, xContentType),
114+
xContentType,
115+
dynamicMappingsUpdate,
116+
normalizedSize
117+
);
118+
}
119+
120+
public ParsedDocument(
121+
Field version,
122+
SeqNoFieldMapper.SequenceIDFields seqID,
123+
String id,
124+
String routing,
125+
List<LuceneDocument> documents,
126+
ModernSource source,
127+
XContentType xContentType,
128+
Mapping dynamicMappingsUpdate,
129+
long normalizedSize
105130
) {
106131
this.version = version;
107132
this.seqID = seqID;
@@ -142,7 +167,11 @@ public List<LuceneDocument> docs() {
142167
return this.documents;
143168
}
144169

145-
public BytesReference source() {
170+
public BytesReference bytesSource() {
171+
return this.source.originalSourceBytes();
172+
}
173+
174+
public ModernSource source() {
146175
return this.source;
147176
}
148177

@@ -151,8 +180,7 @@ public XContentType getXContentType() {
151180
}
152181

153182
public void setSource(BytesReference source, XContentType xContentType) {
154-
this.source = source;
155-
this.xContentType = xContentType;
183+
this.source = new ModernSource(source, xContentType);
156184
}
157185

158186
/**

server/src/main/java/org/elasticsearch/index/termvectors/TermVectorsService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private static Fields generateTermVectorsFromDoc(IndexShard indexShard, TermVect
345345
}
346346
return generateTermVectors(
347347
indexShard,
348-
XContentHelper.convertToMap(parsedDocument.source(), true, request.xContentType()).v2(),
348+
XContentHelper.convertToMap(parsedDocument.bytesSource(), true, request.xContentType()).v2(),
349349
documentFields,
350350
request.offsets(),
351351
request.perFieldAnalyzer(),

server/src/main/java/org/elasticsearch/index/translog/Translog.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.lucene.store.AlreadyClosedException;
1313
import org.elasticsearch.TransportVersions;
14+
import org.elasticsearch.action.index.ModernSource;
1415
import org.elasticsearch.common.Strings;
1516
import org.elasticsearch.common.UUIDs;
1617
import org.elasticsearch.common.bytes.BytesReference;
@@ -1166,7 +1167,7 @@ public static final class Index extends Operation {
11661167
private final String id;
11671168
private final long autoGeneratedIdTimestamp;
11681169
private final long version;
1169-
private final BytesReference source;
1170+
private final ModernSource source;
11701171
private final String routing;
11711172

11721173
private static Index readFrom(StreamInput in) throws IOException {
@@ -1195,7 +1196,7 @@ public Index(Engine.Index index, Engine.IndexResult indexResult) {
11951196
indexResult.getSeqNo(),
11961197
index.primaryTerm(),
11971198
indexResult.getVersion(),
1198-
index.source(),
1199+
index.modernSource(),
11991200
index.routing(),
12001201
index.getAutoGeneratedIdTimestamp()
12011202
);
@@ -1209,6 +1210,18 @@ public Index(
12091210
BytesReference source,
12101211
String routing,
12111212
long autoGeneratedIdTimestamp
1213+
) {
1214+
this(id, seqNo, primaryTerm, version, new ModernSource(source), routing, autoGeneratedIdTimestamp);
1215+
}
1216+
1217+
public Index(
1218+
String id,
1219+
long seqNo,
1220+
long primaryTerm,
1221+
long version,
1222+
ModernSource source,
1223+
String routing,
1224+
long autoGeneratedIdTimestamp
12121225
) {
12131226
super(seqNo, primaryTerm);
12141227
this.id = id;
@@ -1225,10 +1238,12 @@ public Type opType() {
12251238

12261239
@Override
12271240
public long estimateSize() {
1228-
return (2 * id.length()) + source.length() + (routing != null ? 2 * routing.length() : 0) + (4 * Long.BYTES); // timestamp,
1229-
// seq_no,
1230-
// primary_term,
1231-
// and version
1241+
// TODO: is original source size correct here?
1242+
return (2 * id.length()) + source.originalSourceSize() + (routing != null ? 2 * routing.length() : 0) + (4 * Long.BYTES);
1243+
// timestamp,
1244+
// seq_no,
1245+
// primary_term,
1246+
// and version
12321247
}
12331248

12341249
public String id() {
@@ -1240,7 +1255,7 @@ public String routing() {
12401255
}
12411256

12421257
public BytesReference source() {
1243-
return this.source;
1258+
return this.source.originalSourceBytes();
12441259
}
12451260

12461261
public long version() {
@@ -1257,7 +1272,7 @@ public void writeBody(final StreamOutput out) throws IOException {
12571272
if (format < FORMAT_NO_DOC_TYPE) {
12581273
out.writeString(MapperService.SINGLE_MAPPING_NAME);
12591274
}
1260-
out.writeBytesReference(source);
1275+
out.writeBytesReference(source.originalSourceBytes());
12611276
out.writeOptionalString(routing);
12621277
out.writeLong(version);
12631278
out.writeLong(autoGeneratedIdTimestamp);

server/src/test/java/org/elasticsearch/index/mapper/SourceFieldMapperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void testNoFormat() throws Exception {
8181
)
8282
);
8383

84-
assertThat(XContentHelper.xContentType(doc.source()), equalTo(XContentType.JSON));
84+
assertThat(XContentHelper.xContentType(doc.bytesSource()), equalTo(XContentType.JSON));
8585

8686
doc = documentMapper.parse(
8787
new SourceToParse(
@@ -91,7 +91,7 @@ public void testNoFormat() throws Exception {
9191
)
9292
);
9393

94-
assertThat(XContentHelper.xContentType(doc.source()), equalTo(XContentType.SMILE));
94+
assertThat(XContentHelper.xContentType(doc.bytesSource()), equalTo(XContentType.SMILE));
9595
}
9696

9797
public void testIncludes() throws Exception {

test/framework/src/main/java/org/elasticsearch/index/mapper/MapperTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ public final void testIndexTimeStoredFieldsAccess() throws IOException {
980980
SearchLookup lookup = new SearchLookup(
981981
f -> fieldType,
982982
(f, s, t) -> { throw new UnsupportedOperationException(); },
983-
(ctx, docid) -> Source.fromBytes(doc.source())
983+
(ctx, docid) -> Source.fromBytes(doc.bytesSource())
984984
);
985985

986986
withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), ir -> {

test/framework/src/main/java/org/elasticsearch/search/fetch/HighlighterTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected final Map<String, HighlightField> highlight(MapperService mapperServic
7272
HighlightPhase highlightPhase = new HighlightPhase(getHighlighters());
7373
FetchSubPhaseProcessor processor = highlightPhase.getProcessor(fetchContext(context, search));
7474
Map<String, List<Object>> storedFields = storedFields(processor.storedFieldsSpec(), doc);
75-
Source source = Source.fromBytes(doc.source());
75+
Source source = Source.fromBytes(doc.bytesSource());
7676
FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext(
7777
SearchHit.unpooled(0, "id"),
7878
ir.leaves().get(0),

0 commit comments

Comments
 (0)