2121import org .elasticsearch .common .metrics .MeanMetric ;
2222import org .elasticsearch .core .Nullable ;
2323import org .elasticsearch .index .IndexSettings ;
24- import org .elasticsearch .index .IndexVersion ;
2524import org .elasticsearch .index .IndexVersions ;
2625import org .elasticsearch .index .VersionType ;
2726import org .elasticsearch .index .engine .Engine ;
@@ -93,7 +92,7 @@ public GetResult get(
9392 FetchSourceContext fetchSourceContext ,
9493 boolean forceSyntheticSource
9594 ) throws IOException {
96- return get (
95+ return doGet (
9796 id ,
9897 gFields ,
9998 realtime ,
@@ -107,7 +106,7 @@ public GetResult get(
107106 );
108107 }
109108
110- public GetResult get (
109+ public GetResult mget (
111110 String id ,
112111 String [] gFields ,
113112 boolean realtime ,
@@ -117,7 +116,7 @@ public GetResult get(
117116 boolean forceSyntheticSource ,
118117 MultiEngineGet mget
119118 ) throws IOException {
120- return get (
119+ return doGet (
121120 id ,
122121 gFields ,
123122 realtime ,
@@ -131,7 +130,7 @@ public GetResult get(
131130 );
132131 }
133132
134- private GetResult get (
133+ private GetResult doGet (
135134 String id ,
136135 String [] gFields ,
137136 boolean realtime ,
@@ -144,21 +143,40 @@ private GetResult get(
144143 Function <Engine .Get , Engine .GetResult > engineGetOperator
145144 ) throws IOException {
146145 currentMetric .inc ();
146+ final long now = System .nanoTime ();
147147 try {
148- long now = System .nanoTime ();
149- GetResult getResult = innerGet (
150- id ,
151- gFields ,
152- realtime ,
153- version ,
154- versionType ,
155- ifSeqNo ,
156- ifPrimaryTerm ,
157- fetchSourceContext ,
158- forceSyntheticSource ,
159- engineGetOperator
160- );
148+ var engineGet = new Engine .Get (realtime , realtime , id ).version (version )
149+ .versionType (versionType )
150+ .setIfSeqNo (ifSeqNo )
151+ .setIfPrimaryTerm (ifPrimaryTerm );
161152
153+ final GetResult getResult ;
154+ try (Engine .GetResult get = engineGetOperator .apply (engineGet )) {
155+ if (get == null ) {
156+ getResult = null ;
157+ } else if (get .exists () == false ) {
158+ getResult = new GetResult (
159+ shardId .getIndexName (),
160+ id ,
161+ UNASSIGNED_SEQ_NO ,
162+ UNASSIGNED_PRIMARY_TERM ,
163+ -1 ,
164+ false ,
165+ null ,
166+ null ,
167+ null
168+ );
169+ } else {
170+ // break between having loaded it from translog (so we only have _source), and having a document to load
171+ getResult = innerGetFetch (
172+ id ,
173+ gFields ,
174+ normalizeFetchSourceContent (fetchSourceContext , gFields ),
175+ get ,
176+ forceSyntheticSource
177+ );
178+ }
179+ }
162180 if (getResult != null && getResult .isExists ()) {
163181 existsMetric .inc (System .nanoTime () - now );
164182 } else {
@@ -179,7 +197,7 @@ public GetResult getFromTranslog(
179197 FetchSourceContext fetchSourceContext ,
180198 boolean forceSyntheticSource
181199 ) throws IOException {
182- return get (
200+ return doGet (
183201 id ,
184202 gFields ,
185203 realtime ,
@@ -193,12 +211,8 @@ public GetResult getFromTranslog(
193211 );
194212 }
195213
196- public GetResult getForUpdate (String id , long ifSeqNo , long ifPrimaryTerm ) throws IOException {
197- return getForUpdate (id , ifSeqNo , ifPrimaryTerm , new String [] { RoutingFieldMapper .NAME });
198- }
199-
200214 public GetResult getForUpdate (String id , long ifSeqNo , long ifPrimaryTerm , String [] gFields ) throws IOException {
201- return get (
215+ return doGet (
202216 id ,
203217 gFields ,
204218 true ,
@@ -259,35 +273,6 @@ private static FetchSourceContext normalizeFetchSourceContent(@Nullable FetchSou
259273 return FetchSourceContext .DO_NOT_FETCH_SOURCE ;
260274 }
261275
262- private GetResult innerGet (
263- String id ,
264- String [] gFields ,
265- boolean realtime ,
266- long version ,
267- VersionType versionType ,
268- long ifSeqNo ,
269- long ifPrimaryTerm ,
270- FetchSourceContext fetchSourceContext ,
271- boolean forceSyntheticSource ,
272- Function <Engine .Get , Engine .GetResult > engineGetOperator
273- ) throws IOException {
274- fetchSourceContext = normalizeFetchSourceContent (fetchSourceContext , gFields );
275- var engineGet = new Engine .Get (realtime , realtime , id ).version (version )
276- .versionType (versionType )
277- .setIfSeqNo (ifSeqNo )
278- .setIfPrimaryTerm (ifPrimaryTerm );
279- try (Engine .GetResult get = engineGetOperator .apply (engineGet )) {
280- if (get == null ) {
281- return null ;
282- }
283- if (get .exists () == false ) {
284- return new GetResult (shardId .getIndexName (), id , UNASSIGNED_SEQ_NO , UNASSIGNED_PRIMARY_TERM , -1 , false , null , null , null );
285- }
286- // break between having loaded it from translog (so we only have _source), and having a document to load
287- return innerGetFetch (id , gFields , fetchSourceContext , get , forceSyntheticSource );
288- }
289- }
290-
291276 private GetResult innerGetFetch (
292277 String id ,
293278 String [] storedFields ,
@@ -298,7 +283,6 @@ private GetResult innerGetFetch(
298283 assert get .exists () : "method should only be called if document could be retrieved" ;
299284 // check first if stored fields to be loaded don't contain an object field
300285 MappingLookup mappingLookup = mapperService .mappingLookup ();
301- final IndexVersion indexVersion = indexSettings .getIndexVersionCreated ();
302286 final Set <String > storedFieldSet = new HashSet <>();
303287 boolean hasInferenceMetadataFields = false ;
304288 if (storedFields != null ) {
@@ -338,6 +322,9 @@ private GetResult innerGetFetch(
338322 throw new ElasticsearchException ("Failed to get id [" + id + "]" , e );
339323 }
340324
325+ final boolean supportDocValuesForIgnoredMetaField = indexSettings .getIndexVersionCreated ()
326+ .onOrAfter (IndexVersions .DOC_VALUES_FOR_IGNORED_META_FIELD );
327+
341328 // put stored fields into result objects
342329 if (leafStoredFieldLoader .storedFields ().isEmpty () == false ) {
343330 Set <String > needed = new HashSet <>();
@@ -351,8 +338,7 @@ private GetResult innerGetFetch(
351338 if (false == needed .contains (entry .getKey ())) {
352339 continue ;
353340 }
354- if (IgnoredFieldMapper .NAME .equals (entry .getKey ())
355- && indexVersion .onOrAfter (IndexVersions .DOC_VALUES_FOR_IGNORED_META_FIELD )) {
341+ if (IgnoredFieldMapper .NAME .equals (entry .getKey ()) && supportDocValuesForIgnoredMetaField ) {
356342 continue ;
357343 }
358344 MappedFieldType ft = mapperService .fieldType (entry .getKey ());
@@ -371,9 +357,7 @@ private GetResult innerGetFetch(
371357 // NOTE: when _ignored is requested via `stored_fields` we need to load it from doc values instead of loading it from stored fields.
372358 // The _ignored field used to be stored, but as a result of supporting aggregations on it, it moved from using a stored field to
373359 // using doc values.
374- if (indexVersion .onOrAfter (IndexVersions .DOC_VALUES_FOR_IGNORED_META_FIELD )
375- && storedFields != null
376- && Arrays .asList (storedFields ).contains (IgnoredFieldMapper .NAME )) {
360+ if (supportDocValuesForIgnoredMetaField && storedFields != null && Arrays .asList (storedFields ).contains (IgnoredFieldMapper .NAME )) {
377361 final DocumentField ignoredDocumentField = loadIgnoredMetadataField (docIdAndVersion );
378362 if (ignoredDocumentField != null ) {
379363 if (metadataFields == null ) {
0 commit comments