Ejemplo n.º 1
0
        public void DeleteIndexes(int indexToRetain, string indexPrefix)
        {
            indexToRetain = indexToRetain <= 0 ? _defautltIndexCopyCount : indexToRetain;

            var indexNameDelimeter = new[] { '_' };

            var indexToDeleteCount = _client
                                     .IndicesStats(Indices.All, null, string.Empty)
                                     .Indices
                                     .Count(x => x.Key.StartsWith(indexPrefix) && x.Key.Split(indexNameDelimeter).Count() == 2);

            if (indexToDeleteCount > indexToRetain)
            {
                var indicesToBeDelete = _client
                                        .IndicesStats(Indices.All, null, string.Empty)
                                        .Indices
                                        .Where(x => x.Key.StartsWith(indexPrefix))
                                        .OrderByDescending(x => x.Key.Split(indexNameDelimeter).Last())
                                        .Skip(indexToRetain)
                                        .ToList();

                _logger.Debug($"Deleting {indicesToBeDelete.Count()} indexes...");

                foreach (var index in indicesToBeDelete)
                {
                    _logger.Debug($"Deleting {index.Key}");
                    DeleteIndex(index.Key);
                }
            }

            _logger.Debug("Deletion completed...");
        }