Exemple #1
0
        private void PrepareIndexes(IRootResolver resolver)
        {
            _elasticClient = resolver.Resolve <IElasticClient>();

            var readModelTypes =
                GetLoadableTypes <ElasticsearchTypeAttribute>(typeof(ElasticsearchThingyReadModel).Assembly);

            foreach (var readModelType in readModelTypes)
            {
                var esType = readModelType.GetTypeInfo()
                             .GetCustomAttribute <ElasticsearchTypeAttribute>();

                var aliasResponse = _elasticClient.GetAlias(x => x.Name(esType.Name));

                if (aliasResponse.ApiCall.Success)
                {
                    if (aliasResponse.Indices != null)
                    {
                        foreach (var indice in aliasResponse?.Indices)
                        {
                            _elasticClient.DeleteAlias(indice.Key, esType.Name);

                            _elasticClient.DeleteIndex(indice.Key,
                                                       d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)));
                        }

                        _elasticClient.DeleteIndex(esType.Name,
                                                   d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)));
                    }
                }

                var indexName = GetIndexName(esType.Name);

                _indexes.Add(indexName);

                _elasticClient.CreateIndex(indexName, c => c
                                           .Settings(s => s
                                                     .NumberOfShards(1)
                                                     .NumberOfReplicas(0))
                                           .Aliases(a => a.Alias(esType.Name))
                                           .Mappings(m => m
                                                     .Map(TypeName.Create(readModelType), d => d
                                                          .AutoMap())));
            }
        }