public async Task <ResultDetails <string> > CreateIndexAsync(string index, IndexSettings indexSettings, IndexAliases indexAliases, IndexWarmers indexWarmers)
        {
            if (string.IsNullOrEmpty(index))
            {
                throw new ElasticsearchCrudException("CreateIndexAsync: index is required");
            }
            if (indexSettings == null)
            {
                indexSettings = new IndexSettings {
                    NumberOfShards = 5, NumberOfReplicas = 1
                };
            }
            if (indexAliases == null)
            {
                indexAliases = new IndexAliases();
            }

            if (indexWarmers == null)
            {
                indexWarmers = new IndexWarmers();
            }

            _traceProvider.Trace(TraceEventType.Verbose, "{0}: CreateIndexAsync Elasticsearch started", "ElasticsearchContextIndexMapping");
            var resultDetails = new ResultDetails <string> {
                Status = HttpStatusCode.InternalServerError
            };

            try
            {
                MappingUtils.GuardAgainstBadIndexName(index);

                var indexMappings = new IndexMappings(_traceProvider, _elasticsearchSerializerConfiguration);
                indexMappings.CreateIndexSettingsForDocument(index, indexSettings, indexAliases, indexWarmers);
                await indexMappings.Execute(_client, _connectionString, _traceProvider, _cancellationTokenSource);

                return(resultDetails);
            }
            catch (OperationCanceledException oex)
            {
                _traceProvider.Trace(TraceEventType.Warning, oex, "{1}: CreateIndexAsync Request OperationCanceledException: {0}", oex.Message,
                                     "ElasticsearchContextIndexMapping");
                resultDetails.Description = "OperationCanceledException";
                return(resultDetails);
            }
        }
        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.CreateIndexSettingsForDocument(index, indexDefinition.IndexSettings, indexDefinition.IndexAliases, indexDefinition.IndexWarmers);
                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);
            }
        }
示例#3
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);
        }