private async Task InitializeAsync()
        {
            // Check whether the index exists or not.
            var indexExistsResponse = await elasticClient.IndexExistsAsync(indexName + "-" + DateTime.Now.ToString(RollingIndexDateFormat));

            if (!indexExistsResponse.Exists)
            {
                // We don't (probably) have created the index yet. Enter the lock,
                // then check again (aka: Double-Check Lock pattern).
                await semaphore.WaitAsync().ConfigureAwait(false);

                try
                {
                    if (!indexExistsResponse.Exists)
                    {
                        // If the index does not exist, create a new one with the current date and alias it.
                        var createIndexResponse = await elasticClient.CreateIndexAsync(indexName + "-" + DateTime.Now.ToString(RollingIndexDateFormat), c => c
                                                                                       .Mappings(ms => ms.Map <DocumentItem>(m => m.AutoMap())).Settings(s => s.Setting(IndexMappingTotalFieldsLimitSetting, 100000))).ConfigureAwait(false);

                        var aliasResponse = await elasticClient.AliasAsync(ac => ac.Add(a => a.Index(indexName + "-" + DateTime.Now.ToString(RollingIndexDateFormat)).Alias(indexName))).ConfigureAwait(false);
                    }
                }
                finally
                {
                    semaphore.Release();
                }
            }
        }
Пример #2
0
        private static List <Func <object> > IndexCommandsAsync(ElasticClient elastic)
        {
            return(new List <Func <object> >
            {
                () => elastic.CreateIndexAsync("test_index_1"),
                () => elastic.IndexExistsAsync("test_index_1"),
                () => elastic.UpdateIndexSettingsAsync(new UpdateIndexSettingsRequest("test_index_1")
                {
                    IndexSettings = new IndexSettings()
                    {
                        Sorting = new SortingSettings
                        {
                            Fields = new Field("Title"),
                        },
                    },
                }),
                () => elastic.AliasAsync(new BulkAliasRequest
                {
                    Actions = new List <IAliasAction>
                    {
                        new AliasAddAction
                        {
                            Add = new AliasAddOperation
                            {
                                Index = "test_index_1",
                                Alias = "test_index_2",
                            },
                        },
                    },
                }),
                () => elastic.GetAliasesPointingToIndexAsync("test_index_1"),
                () => elastic.PutAliasAsync("test_index_1", "test_index_3"),
                () => elastic.AliasExistsAsync(new AliasExistsRequest("test_index_1")),
                () => elastic.DeleteAliasAsync(new DeleteAliasRequest("test_index_1", "test_index_3")),
                () => elastic.DeleteAliasAsync(new DeleteAliasRequest("test_index_1", "test_index_2")),
                () => elastic.CreateIndexAsync("test_index_4"),
#if (ELASTICSEARCH_6_1 && !DEFAULT_SAMPLES)
                () => elastic.SplitIndexAsync("test_index_1", "test_index_4"),
#endif
                () => elastic.DeleteIndexAsync("test_index_4"),
                () => elastic.CloseIndexAsync("test_index_1"),
                () => elastic.OpenIndexAsync("test_index_1"),
                () => elastic.PutIndexTemplateAsync(new PutIndexTemplateRequest("test_template_1")),
                () => elastic.IndexTemplateExistsAsync("test_template_1"),
                () => elastic.DeleteIndexTemplateAsync("test_template_1"),
                () => elastic.IndicesShardStoresAsync(),
                () => elastic.IndicesStatsAsync("test_index_1"),
                () => elastic.DeleteIndexAsync("test_index_1"),
                () => elastic.GetAliasAsync(new GetAliasRequest()),
            });
        }
Пример #3
0
        private async Task CreateIndexAsync(string indexName)
        {
            await esclient.CreateIndexAsync(indexName,
                                            c => c.Settings(s => s.Analysis(analysisDescriptor => analysisDescriptor.TokenFilters(
                                                                                tokenFilters => tokenFilters.WordDelimiter("camelfilter", t => t.SplitOnCaseChange())
                                                                                ).Analyzers(analyzers => analyzers.Pattern(
                                                                                                "loggername", p => new PatternAnalyzer {
                Lowercase = true,
                Pattern = @"[^\w]+"
            }).Custom("camelcase", analyzer => analyzer.Tokenizer("whitespace").Filters(
                          "camelfilter", "lowercase"))))
                                                            .NumberOfShards(ElasticSearchClientConfiguration.ShardsNum)
                                                            .NumberOfReplicas(ElasticSearchClientConfiguration.ReplicasNum))
                                            .Mappings(m => m.Map <ElasticLogRecord>(am => am.AutoMap())));

            await esclient.AliasAsync(a => a.Add(add => add.Index(indexName).Alias(AliasName)));
        }
Пример #4
0
 private static List <Func <object> > IndexCommandsAsync(ElasticClient elastic)
 {
     return(new List <Func <object> >
     {
         () => elastic.CreateIndexAsync("test_index_1"),
         () => elastic.IndexExistsAsync("test_index_1"),
         () => elastic.UpdateIndexSettingsAsync(new UpdateIndexSettingsRequest("test_index_1")
         {
             IndexSettings = new IndexSettings()
             {
                 // V6 Feature
                 // Sorting = new SortingSettings
                 // {
                 //     Fields = new Field("Title"),
                 // },
             },
         }),
         () => elastic.AliasAsync(new BulkAliasRequest
         {
             Actions = new List <IAliasAction>
             {
                 new AliasAddAction
                 {
                     Add = new AliasAddOperation
                     {
                         Index = "test_index_1",
                         Alias = "test_index_2",
                     },
                 },
             },
         }),
         () => elastic.GetAliasesPointingToIndexAsync("test_index_1"),
         () => elastic.PutAliasAsync("test_index_1", "test_index_3"),
         () => elastic.DeleteAliasAsync(new DeleteAliasRequest("test_index_1", "test_index_3")),
         () => elastic.DeleteAliasAsync(new DeleteAliasRequest("test_index_1", "test_index_2")),
         () => elastic.CreateIndexAsync("test_index_4"),
         () => elastic.DeleteIndexAsync("test_index_4"),
         () => elastic.CloseIndexAsync("test_index_1"),
         () => elastic.OpenIndexAsync("test_index_1"),
         () => elastic.PutIndexTemplateAsync(new PutIndexTemplateRequest("test_template_1")),
         () => elastic.IndexTemplateExistsAsync("test_template_1"),
         () => elastic.DeleteIndexTemplateAsync("test_template_1"),
         () => elastic.IndicesShardStoresAsync(),
         () => elastic.IndicesStatsAsync("test_index_1"),
         () => elastic.DeleteIndexAsync("test_index_1"),
         () => elastic.GetAliasAsync(new GetAliasRequest()),
         () => elastic.ReindexOnServerAsync(new ReindexOnServerRequest
         {
             Source = new ReindexSource
             {
                 Remote = new RemoteSource
                 {
                     Host = new Uri("http://" + Host()),
                     Username = "******",
                     Password = "******"
                 },
                 Index = "some_index",
                 Size = 10
             },
             Destination = new ReindexDestination
             {
                 Index = "some_index_clone",
             }
         })
     });
 }