コード例 #1
0
        private async Task SetupIndexAsync(IElasticClient connection, string indexName, string entityName, bool forceCreate)
        {
            var exists = Wrap(await connection.IndexExistsAsync(x => x.Index(indexName)).ConfigureAwait(false)).Exists;

            if (exists && forceCreate)
            {
                Wrap(await connection.DeleteIndexAsync(x => x.Index(indexName)).ConfigureAwait(false));
                exists = false;
            }

            if (!exists)
            {
                var createResponse = Wrap(await connection.CreateIndexAsync(createIndexDescriptor => createIndexDescriptor
                                                                            .Index(indexName)
                                                                            .Analysis(a => a
                                                                                      .Analyzers(x => x.Add("lowercaseKeyword", new CustomAnalyzer {
                    Tokenizer = "keyword",
                    Filter    = new[] { "standard", "lowercase" }
                }))
                                                                                      )
                                                                            .AddMapping <TUser>(m => m
                                                                                                .MapFromAttributes()
                                                                                                .IncludeInAll(false)
                                                                                                .IdField(x => x.Path("id"))
                                                                                                .Type(entityName)
                                                                                                )
                                                                            ).ConfigureAwait(false));

                AssertIndexCreateSuccess(createResponse);
                await SeedAsync().ConfigureAwait(false);
            }
        }
コード例 #2
0
 /// <summary>
 /// The delete index API allows to delete an existing index.
 /// <para> </para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-delete-index.html
 /// </summary>
 /// <param name="client"></param>
 /// <param name="index">The name of the index to be deleted</param>
 /// <param name="deleteIndexSelector">A descriptor that further describes the parameters for the delete index operation</param>
 public static Task <IIndicesResponse> DeleteIndexAsync(this IElasticClient client, string index,
                                                        Func <DeleteIndexDescriptor, DeleteIndexDescriptor> deleteIndexSelector)
 {
     index.ThrowIfNullOrEmpty("index");
     deleteIndexSelector = deleteIndexSelector ?? (d => d);
     return(client.DeleteIndexAsync(d => deleteIndexSelector(d).Index(index)));
 }
コード例 #3
0
 private async Task RemoveIndexIfNeeded(IElasticClient client)
 {
     if ((await client.IndexExistsAsync(_setup.IndexName)).Exists)
     {
         await client.DeleteIndexAsync(_setup.IndexName);
     }
 }
コード例 #4
0
 public async Task DeleteIndexAsync(string indexName)
 {
     using (
         BusyStateManager.Start(
             $"Deleting {indexName} triggered by '{UmbracoContext.Current.Security.CurrentUser.Name}'", indexName))
     {
         await client.DeleteIndexAsync(indexName);
     }
 }
コード例 #5
0
        /// <summary>
        /// Delete ElasticSearch index.
        /// </summary>
        public async Task <IDeleteIndexResponse> DeleteIndex()
        {
            if ((await _client.IndexExistsAsync(_serviceConfig.IndexName)).Exists)
            {
                return(await _client.DeleteIndexAsync(_serviceConfig.IndexName));
            }

            return(null);
        }
コード例 #6
0
        public async Task DeleteIndexAsync(string indexName)
        {
            var existsResponse = await _elasticClient.IndexExistsAsync(new IndexExistsRequest(indexName));

            if (existsResponse.Exists)
            {
                await _elasticClient.DeleteIndexAsync(indexName);
            }
        }
コード例 #7
0
 public static Task <DeleteIndexResponse> DeleteIndexAsync(
     this IElasticClient client,
     string index,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(client.DeleteIndexAsync(
                index,
                cancellationToken
                ));
 }
コード例 #8
0
        public static Task <DeleteIndexResponse> DeleteIndexAsync <TDocument>(
            this IElasticClient client, CancellationToken cancellationToken = default(CancellationToken),
            string indexSuffix = null)
            where TDocument : class
        {
            var index = GetIndex <TDocument>(indexSuffix);

            return(client.DeleteIndexAsync(
                       index,
                       cancellationToken));
        }
コード例 #9
0
        public override async Task DestroyAsync(CancellationToken cancellationToken)
        {
            // The request.
            IDeleteIndexRequest request = new DeleteIndexDescriptor(Indices.Index <T>());

            // Delete and return the response.
            IDeleteIndexResponse response = await ElasticClient
                                            .DeleteIndexAsync(request, cancellationToken).ConfigureAwait(false);

            // If it failed, throw an exception.
            response.ThrowIfError();
        }
コード例 #10
0
ファイル: ElasticSearchGateway.cs プロジェクト: moshfeu/Site
        public async Task UpdateDataZeroDownTime(List <Feature> names, List <Feature> highways)
        {
            // init
            var currentNameIndex    = OSM_NAMES_INDEX1;
            var currentHighwayIndex = OSM_HIGHWAYS_INDEX1;
            var newNameIndex        = OSM_NAMES_INDEX2;
            var newHighwayIndex     = OSM_HIGHWAYS_INDEX2;

            if (_elasticClient.IndexExists(OSM_NAMES_INDEX2).Exists)
            {
                currentNameIndex    = OSM_NAMES_INDEX2;
                currentHighwayIndex = OSM_HIGHWAYS_INDEX2;
                newNameIndex        = OSM_NAMES_INDEX1;
                newHighwayIndex     = OSM_HIGHWAYS_INDEX1;
            }
            // create new indexes
            await CreateNamesIndex(newNameIndex);
            await CreateHighwaysIndex(newHighwayIndex);

            // update data
            await UpdateUsingPaging(names, newNameIndex);
            await UpdateUsingPaging(highways, newHighwayIndex);

            // change alias
            await _elasticClient.AliasAsync(a => a
                                            .Remove(i => i.Alias(OSM_NAMES_ALIAS).Index(currentNameIndex))
                                            .Remove(i => i.Alias(OSM_HIGHWAYS_ALIAS).Index(currentHighwayIndex))
                                            .Add(i => i.Alias(OSM_NAMES_ALIAS).Index(newNameIndex))
                                            .Add(i => i.Alias(OSM_HIGHWAYS_ALIAS).Index(newHighwayIndex))
                                            );

            // delete old indexes
            await _elasticClient.DeleteIndexAsync(currentNameIndex);

            await _elasticClient.DeleteIndexAsync(currentHighwayIndex);
        }
コード例 #11
0
        public async Task ReIndex <T>(IReadOnlyCollection <T> items) where T : class, IEntity
        {
            var exists = await _elasticClient.IndexExistsAsync(_indexName);

            if (exists.Exists)
            {
                await _elasticClient.DeleteIndexAsync(_indexName);

                await _elasticClient.CreateIndexAsync(_indexName);
            }

            await Task.WhenAll(items.Select(async item => await AddItem(item)));

            await _elasticClient.RefreshAsync(new RefreshRequest(_indexName));
        }
コード例 #12
0
        public async Task DeleteAllAsync(
            CancellationToken cancellationToken)
        {
            var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <TReadModel>();

            _log.Information($"Deleting ALL '{typeof(TReadModel).PrettyPrint()}' by DELETING INDEX '{readModelDescription.IndexName}'!");

            await _elasticClient.DeleteIndexAsync(
                readModelDescription.IndexName.Value,
                d => d
                .RequestConfiguration(c => c
                                      .AllowedStatusCodes((int)HttpStatusCode.NotFound)),
                cancellationToken)
            .ConfigureAwait(false);
        }
コード例 #13
0
        /// <summary>
        /// 删除索引
        /// </summary>
        /// <param name="client"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public static async Task DeleteIndexIfExistsAsync(this IElasticClient client, string indexName)
        {
            indexName = indexName.ToLower();
            var exist_response = await client.IndexExistsAsync(indexName);

            exist_response.ThrowIfException();

            if (!exist_response.Exists)
            {
                return;
            }

            var response = await client.DeleteIndexAsync(indexName);

            response.ThrowIfException();
        }
コード例 #14
0
        public static async Task <IEnumerable <IDeleteIndexResponse> > DeleteIndexByAliasAsync(
            this IElasticClient client,
            string alias)
        {
            var indicesForAlias = await client.GetIndicesPointingToAliasAsync(alias);

            var responses = new List <IDeleteIndexResponse>(indicesForAlias.Count);

            foreach (var index in indicesForAlias)
            {
                var response = await client.DeleteIndexAsync(index);

                responses.Add(response);
            }

            return(responses);
        }
        public void SwitchAliasToIndex(string indexName)
        {
            var catAliases = _client.CatAliases(new CatAliasesRequest(_aliasName));
            var oldIndexes = catAliases.Records.Select(x => x.Index);

            var response = _client.Alias(a => RemoveIndexesFromAlias(a, oldIndexes).Add(i => i.Index(indexName).Alias(_aliasName)));

            if (!response.IsValid)
            {
                throw new Exception("Failed while switching index alias.", response.OriginalException);
            }

            foreach (var index in oldIndexes)
            {
                _client.DeleteIndexAsync(new DeleteIndexRequest(index));
            }
        }
コード例 #16
0
        private async Task IndexMockData(IElasticClient c, int requestsPerIteration)
        {
            var tokenSource = new CancellationTokenSource();
            await c.DeleteIndexAsync(Index <Project>(), cancellationToken : tokenSource.Token);

            var observableBulk = c.BulkAll(this.MockDataGenerator(100000), f => f
                                           .MaxDegreeOfParallelism(10)
                                           .BackOffTime(TimeSpan.FromSeconds(10))
                                           .BackOffRetries(2)
                                           .Size(1000)
                                           .RefreshOnCompleted()
                                           , tokenSource.Token);
            await observableBulk.ForEachAsync(x => { }, tokenSource.Token);

            var statsRequest = new NodesStatsRequest(NodesStatsMetric.Http);
            var nodeStats    = await c.NodesStatsAsync(statsRequest, tokenSource.Token);

            AssertHttpStats(c, nodeStats, -1, requestsPerIteration);
        }
コード例 #17
0
ファイル: ElasticSearchGateway.cs プロジェクト: ifle/Site
        private async Task UpdateZeroDownTime(string index1, string index2, string alias, Func <string, Task> createIndexDelegate, List <Feature> features)
        {
            var currentIndex = index1;
            var newIndex     = index2;

            if (_elasticClient.IndexExists(index2).Exists)
            {
                currentIndex = index2;
                newIndex     = index1;
            }

            await createIndexDelegate(newIndex);
            await UpdateUsingPaging(features, newIndex);

            await _elasticClient.AliasAsync(a => a
                                            .Remove(i => i.Alias(alias).Index(currentIndex))
                                            .Add(i => i.Alias(alias).Index(newIndex))
                                            );

            await _elasticClient.DeleteIndexAsync(currentIndex);
        }
コード例 #18
0
        public async Task DeleteAllAsync(
            CancellationToken cancellationToken)
        {
            var readModelDescription = _readModelDescriptionProvider.GetReadModelDescription <TReadModel>();

            _log.Information($"Deleting ALL '{typeof(TReadModel).PrettyPrint()}' by DELETING INDEX '{readModelDescription.IndexName}'!");

            var aliasResponse = await _elasticClient.GetAliasAsync(x => x.Name(readModelDescription.IndexName.Value), cancellationToken)
                                .ConfigureAwait(false);

            if (aliasResponse.ApiCall.Success)
            {
                foreach (var indicesKey in aliasResponse.Indices.Keys)
                {
                    await _elasticClient.DeleteIndexAsync(indicesKey,
                                                          d => d.RequestConfiguration(c => c.AllowedStatusCodes((int)HttpStatusCode.NotFound)),
                                                          cancellationToken)
                    .ConfigureAwait(false);
                }
            }
        }
コード例 #19
0
        public async Task <IActionResult> DelIndex([FromForm] string indexName)
        {
            await _esClient.DeleteIndexAsync(new DeleteIndexRequest(Indices.Index(indexName)));

            return(Ok());
        }
コード例 #20
0
        public async Task RecreateIndex()
        {
            await _elasticClient.DeleteIndexAsync(PagesIndexName);

            await EnsureIndexesExist();
        }