public async Task <ResultDetails <string> > CreateTypeMappingForIndexAsync <T>(MappingDefinition mappingDefinition)
        {
            if (mappingDefinition == null)
            {
                throw new ElasticsearchCrudException("CreateTypeMappingForIndexAsync: A mapping definition with the parent index is required");
            }
            _traceProvider.Trace(TraceEventType.Verbose, "{0}: CreateTypeMappingForIndex Elasticsearch started", "ElasticsearchContextIndexMapping");
            var resultDetails = new ResultDetails <string> {
                Status = HttpStatusCode.InternalServerError
            };

            try
            {
                var indexMappings = new IndexMappings(_traceProvider, _elasticsearchSerializerConfiguration);

                var item = Activator.CreateInstance <T>();

                var entityContextInfo = new EntityContextInfo
                {
                    RoutingDefinition = mappingDefinition.RoutingDefinition,
                    Document          = item,
                    EntityType        = typeof(T),
                    Id = "0"
                };

                if (string.IsNullOrEmpty(mappingDefinition.Index))
                {
                    string index = _elasticsearchSerializerConfiguration.ElasticsearchMappingResolver.GetElasticSearchMapping(entityContextInfo.EntityType).GetIndexForType(entityContextInfo.EntityType);
                    MappingUtils.GuardAgainstBadIndexName(index);
                    mappingDefinition.Index = index;
                }

                indexMappings.CreatePropertyMappingForTopDocument(entityContextInfo, mappingDefinition);
                await indexMappings.Execute(_client, _connectionString, _traceProvider, _cancellationTokenSource);

                return(resultDetails);
            }
            catch (OperationCanceledException oex)
            {
                _traceProvider.Trace(TraceEventType.Warning, oex, "{1}: CreateTypeMappingForIndexAsync Request OperationCanceledException: {0}", oex.Message,
                                     "ElasticsearchContextIndexMapping");
                resultDetails.Description = "OperationCanceledException";
                return(resultDetails);
            }
        }
Esempio n. 2
0
        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.CreateIndexSettingsForDocument(index, new IndexSettings {
                        NumberOfShards = 5, NumberOfReplicas = 1
                    }, new IndexAliases());
                    _indexMappings.CreatePropertyMappingForTopDocument(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);
        }