9999import org .elasticsearch .index .get .GetStats ;
100100import org .elasticsearch .index .get .ShardGetService ;
101101import org .elasticsearch .index .mapper .DateFieldMapper ;
102- import org .elasticsearch .index .mapper .DocumentMapperForType ;
102+ import org .elasticsearch .index .mapper .DocumentMapper ;
103103import org .elasticsearch .index .mapper .IdFieldMapper ;
104104import org .elasticsearch .index .mapper .MappedFieldType ;
105105import org .elasticsearch .index .mapper .MapperParsingException ;
@@ -830,7 +830,7 @@ private Engine.IndexResult applyIndexOperation(Engine engine, long seqNo, long o
830830 sourceWithResolvedType = new SourceToParse (sourceToParse .index (), resolvedType , sourceToParse .id (),
831831 sourceToParse .source (), sourceToParse .getXContentType (), sourceToParse .routing (), sourceToParse .dynamicTemplates ());
832832 }
833- operation = prepareIndex (docMapper ( resolvedType ) , sourceWithResolvedType ,
833+ operation = prepareIndex (mapperService , resolvedType , sourceWithResolvedType ,
834834 seqNo , opPrimaryTerm , version , versionType , origin , autoGeneratedTimeStamp , isRetry , ifSeqNo , ifPrimaryTerm );
835835 Mapping update = operation .parsedDoc ().dynamicMappingsUpdate ();
836836 if (update != null ) {
@@ -848,16 +848,24 @@ private Engine.IndexResult applyIndexOperation(Engine engine, long seqNo, long o
848848 return index (engine , operation );
849849 }
850850
851- public static Engine .Index prepareIndex (DocumentMapperForType docMapper , SourceToParse source , long seqNo ,
851+ public static Engine .Index prepareIndex (MapperService mapperService , String type , SourceToParse source , long seqNo ,
852852 long primaryTerm , long version , VersionType versionType , Engine .Operation .Origin origin ,
853853 long autoGeneratedIdTimestamp , boolean isRetry ,
854854 long ifSeqNo , long ifPrimaryTerm ) {
855855 long startTime = System .nanoTime ();
856856 assert source .dynamicTemplates ().isEmpty () || origin == Engine .Operation .Origin .PRIMARY :
857857 "dynamic_templates parameter can only be associated with primary operations" ;
858- ParsedDocument doc = docMapper .getDocumentMapper ().parse (source );
859- if (docMapper .getMapping () != null ) {
860- doc .addDynamicMappingsUpdate (docMapper .getMapping ());
858+ DocumentMapper documentMapper = mapperService .documentMapper (type );
859+ Mapping mapping = null ;
860+ if (documentMapper == null ) {
861+ documentMapper = DocumentMapper .createEmpty (type , mapperService );
862+ mapping = documentMapper .mapping ();
863+ }
864+ ParsedDocument doc = documentMapper .parse (source );
865+ if (mapping != null ) {
866+ //If we are indexing but there is no mapping we create one. This is to ensure that whenever at least a document is indexed
867+ // some mappings do exist. It covers for the case of indexing an empty doc (`{}`).
868+ doc .addDynamicMappingsUpdate (mapping );
861869 }
862870 Term uid = new Term (IdFieldMapper .NAME , Uid .encodeId (doc .id ()));
863871 return new Engine .Index (uid , doc , seqNo , primaryTerm , version , versionType , origin , startTime , autoGeneratedIdTimestamp , isRetry ,
@@ -955,20 +963,21 @@ private Engine.DeleteResult applyDeleteOperation(Engine engine, long seqNo, long
955963 // In order to work around this issue, we make deletions create types. This way, we
956964 // fail if index and delete operations do not use the same type.
957965 // TODO: clean this up when types are gone
966+ String resolvedType = mapperService .resolveDocumentType (type );
958967 try {
959- Mapping update = docMapper (type ).getMapping ();
960- if (update != null ) {
961- return new Engine .DeleteResult (update );
968+ DocumentMapper documentMapper = mapperService .documentMapper (resolvedType );
969+ if (documentMapper == null ) {
970+ documentMapper = DocumentMapper .createEmpty (resolvedType , mapperService );
971+ return new Engine .DeleteResult (documentMapper .mapping ());
962972 }
963973 } catch (MapperParsingException | IllegalArgumentException | TypeMissingException e ) {
964974 return new Engine .DeleteResult (e , version , getOperationPrimaryTerm (), seqNo , false );
965975 }
966- if (mapperService . resolveDocumentType ( type ) .equals (mapperService .mappingLookup ().getType ()) == false ) {
976+ if (resolvedType .equals (mapperService .mappingLookup ().getType ()) == false ) {
967977 // We should never get there due to the fact that we generate mapping updates on deletes,
968978 // but we still prefer to have a hard exception here as we would otherwise delete a
969979 // document in the wrong type.
970- throw new IllegalStateException ("Deleting document from type [" +
971- mapperService .resolveDocumentType (type ) + "] while current type is [" +
980+ throw new IllegalStateException ("Deleting document from type [" + resolvedType + "] while current type is [" +
972981 mapperService .mappingLookup ().getType () + "]" );
973982 }
974983 final Term uid = new Term (IdFieldMapper .NAME , Uid .encodeId (id ));
@@ -978,11 +987,9 @@ private Engine.DeleteResult applyDeleteOperation(Engine engine, long seqNo, long
978987 }
979988
980989 private Engine .Delete prepareDelete (String type , String id , Term uid , long seqNo , long primaryTerm , long version ,
981- VersionType versionType , Engine .Operation .Origin origin ,
982- long ifSeqNo , long ifPrimaryTerm ) {
990+ VersionType versionType , Engine .Operation .Origin origin , long ifSeqNo , long ifPrimaryTerm ) {
983991 long startTime = System .nanoTime ();
984- return new Engine .Delete (mapperService .resolveDocumentType (type ), id , uid , seqNo , primaryTerm , version , versionType ,
985- origin , startTime , ifSeqNo , ifPrimaryTerm );
992+ return new Engine .Delete (type , id , uid , seqNo , primaryTerm , version , versionType , origin , startTime , ifSeqNo , ifPrimaryTerm );
986993 }
987994
988995 private Engine .DeleteResult delete (Engine engine , Engine .Delete delete ) throws IOException {
@@ -2920,11 +2927,6 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
29202927 };
29212928 }
29222929
2923- private DocumentMapperForType docMapper (String type ) {
2924- return mapperService .documentMapperWithAutoCreate (
2925- mapperService .resolveDocumentType (type ));
2926- }
2927-
29282930 private EngineConfig newEngineConfig (LongSupplier globalCheckpointSupplier ) {
29292931 final Sort indexSort = indexSortSupplier .get ();
29302932 final Engine .Warmer warmer = reader -> {
0 commit comments