public async Task <ResultDetails <string> > CreateIndexWithMappingAsync <T>(IndexDefinition indexDefinition)
        {
            if (indexDefinition == null)
            {
                indexDefinition = new IndexDefinition();
            }
            _traceProvider.Trace(TraceEventType.Verbose, "{0}: CreateIndexWithMappingAsync Elasticsearch started", "ElasticsearchContextIndexMapping");
            var resultDetails = new ResultDetails <string> {
                Status = HttpStatusCode.InternalServerError
            };

            try
            {
                var item = Activator.CreateInstance <T>();
                var entityContextInfo = new EntityContextInfo
                {
                    RoutingDefinition = indexDefinition.Mapping.RoutingDefinition,
                    Document          = item,
                    EntityType        = typeof(T),
                    Id = "0"
                };

                string index =
                    _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(
                        entityContextInfo.EntityType).GetIndexForType(entityContextInfo.EntityType);
                MappingUtils.GuardAgainstBadIndexName(index);

                var indexMappings = new IndexMappings(_traceProvider, _elasticsearchSerializerConfiguration);
                indexMappings.CreateIndexSettingsAndMappingsForDocument(
                    index,
                    indexDefinition.IndexSettings,
                    indexDefinition.IndexAliases,
                    indexDefinition.IndexWarmers, entityContextInfo, indexDefinition.Mapping);
                indexDefinition.Mapping.Index = index;
                //indexMappings.CreatePropertyMappingForTopDocument(entityContextInfo, indexDefinition.Mapping);
                await indexMappings.Execute(_client, _connectionString, _traceProvider, _cancellationTokenSource);

                return(resultDetails);
            }
            catch (OperationCanceledException oex)
            {
                _traceProvider.Trace(TraceEventType.Warning, oex, "{1}: CreateIndexWithMappingAsync Request OperationCanceledException: {0}", oex.Message,
                                     "ElasticsearchContextIndexMapping");
                resultDetails.Description = "OperationCanceledException";
                return(resultDetails);
            }
        }
        public ElasticSerializationResult Serialize(IEnumerable <EntityContextInfo> entities)
        {
            if (entities == null)
            {
                return(null);
            }

            _elasticSerializationResult  = new ElasticSerializationResult();
            _elasticsearchCrudJsonWriter = new ElasticsearchCrudJsonWriter();

            foreach (var entity in entities)
            {
                string index = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(entity.EntityType).GetIndexForType(entity.EntityType);
                MappingUtils.GuardAgainstBadIndexName(index);

                if (_saveChangesAndInitMappingsForChildDocuments)
                {
                    _indexMappings.CreateIndexSettingsAndMappingsForDocument(
                        index,
                        new IndexSettings {
                        NumberOfShards = 5, NumberOfReplicas = 1
                    },
                        new IndexAliases(),
                        new IndexWarmers(),
                        entity, new MappingDefinition {
                        Index = index
                    });
                }

                if (entity.DeleteDocument)
                {
                    DeleteEntity(entity);
                }
                else
                {
                    AddUpdateEntity(entity);
                }
            }

            _elasticsearchCrudJsonWriter.Dispose();
            _elasticSerializationResult.Content       = _elasticsearchCrudJsonWriter.Stringbuilder.ToString();
            _elasticSerializationResult.IndexMappings = _indexMappings;
            return(_elasticSerializationResult);
        }