Exemplo n.º 1
0
        /// <summary>
        /// Gets the indices that are mapped to an alias.
        /// </summary>
        /// <param name="alias"></param>
        public string[] GetIndicesForAlias()
        {
            if (_client.AliasExists(aed => aed.Name(this._config.AliasName)).Exists)
            {
                //Soo, if we ask for alias where the index name is that alias,
                //then it is like we asked for host/<alias>/_aliases and then
                //it will return only those indices mapped to that alias.
                var response = _client.GetAlias(ga =>
                                                ga
                                                .Index(_config.AliasName)
                                                );

                if (!response.IsValid)
                {
                    throw new Exception("Error getting Indices for alias: " + this._config.AliasName, response.OriginalException);
                }
                else
                {
                    return(response.Indices.Keys.ToArray());
                }
            }
            else
            {
                return(new string[] { });
            }
        }
Exemplo n.º 2
0
        public IExistsResponse AliasExists(Func <AliasExistsDescriptor, IAliasExistsRequest> selector, string callerName = "")
        {
            var timer  = Stopwatch.StartNew();
            var result = _client.AliasExists(selector);

            SendLog(result.ApiCall, null, timer.ElapsedMilliseconds, $"Alias Exists {callerName}");
            return(result);
        }
Exemplo n.º 3
0
        public virtual void ConfigureIndexes(IElasticClient client, IEnumerable<IElasticIndex> indexes = null) {
            if (indexes == null)
                indexes = GetIndexes();

            foreach (var idx in indexes) {
                int currentVersion = GetAliasVersion(client, idx.AliasName);

                IIndicesOperationResponse response = null;
                var templatedIndex = idx as ITemplatedElasticIndex;
                if (templatedIndex != null)
                    response = client.PutTemplate(idx.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(idx.AliasName));
                else if (!client.IndexExists(idx.VersionedName).Exists)
                    response = client.CreateIndex(idx.VersionedName, descriptor => idx.CreateIndex(descriptor).AddAlias(idx.AliasName));

                Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template.");
                
                // Add existing indexes to the alias.
                if (!client.AliasExists(idx.AliasName).Exists) {
                    if (templatedIndex != null) {
                        var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(idx.VersionedName)).Select(kvp => kvp.Key).ToList();
                        if (indices.Count > 0) {
                            var descriptor = new AliasDescriptor();
                            foreach (string name in indices)
                                descriptor.Add(add => add.Index(name).Alias(idx.AliasName));

                            response = client.Alias(descriptor);
                        }
                    } else {
                        response = client.Alias(a => a.Add(add => add.Index(idx.VersionedName).Alias(idx.AliasName)));
                    }

                    Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias.");
                }

                // already on current version
                if (currentVersion >= idx.Version || currentVersion < 1)
                    continue;

                var reindexWorkItem = new ReindexWorkItem {
                    OldIndex = String.Concat(idx.AliasName, "-v", currentVersion),
                    NewIndex = idx.VersionedName,
                    Alias = idx.AliasName,
                    DeleteOld = true,
                    ParentMaps = idx.GetIndexTypes()
                            .Select(kvp => new ParentMap {Type = kvp.Value.Name, ParentPath = kvp.Value.ParentPath})
                            .Where(m => !String.IsNullOrEmpty(m.ParentPath))
                            .ToList()
                };

                bool isReindexing = _lockProvider.IsLockedAsync(String.Concat("reindex:", reindexWorkItem.Alias, reindexWorkItem.OldIndex, reindexWorkItem.NewIndex)).Result;
                // already reindexing
                if (isReindexing)
                    continue;

                // enqueue reindex to new version
                _lockProvider.TryUsingAsync("enqueue-reindex", () => _workItemQueue.EnqueueAsync(reindexWorkItem), TimeSpan.Zero, CancellationToken.None).Wait();
            }
        }
        public void ConfigureIndexes(IElasticClient client) {
            foreach (var index in GetIndexes()) {
                IIndicesOperationResponse response = null;
                int currentVersion = GetAliasVersion(client, index.Name);

                var templatedIndex = index as ITemplatedElasticSeachIndex;
                if (templatedIndex != null)
                    response = client.PutTemplate(index.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(index.Name));
                else if (!client.IndexExists(index.VersionedName).Exists)
                    response = client.CreateIndex(index.VersionedName, descriptor => index.CreateIndex(descriptor).AddAlias(index.Name));

                Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template.");

                // Add existing indexes to the alias.
                if (!client.AliasExists(index.Name).Exists) {
                    if (templatedIndex != null) {
                        var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(index.VersionedName)).Select(kvp => kvp.Key).ToList();
                        if (indices.Count > 0) {
                            var descriptor = new AliasDescriptor();
                            foreach (string name in indices)
                                descriptor.Add(add => add.Index(name).Alias(index.Name));

                            response = client.Alias(descriptor);
                        }
                    } else {
                        response = client.Alias(a => a.Add(add => add.Index(index.VersionedName).Alias(index.Name)));
                    }

                    Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias.");
                }

                // already on current version
                if (currentVersion >= index.Version || currentVersion < 1)
                    continue;

                // upgrade
                _lockProvider.TryUsingAsync("reindex", async () => {
                    await _workItemQueue.EnqueueAsync(new ReindexWorkItem {
                        OldIndex = String.Concat(index.Name, "-v", currentVersion),
                        NewIndex = index.VersionedName,
                        Alias = index.Name,
                        DeleteOld = true
                    });
                }, TimeSpan.Zero, CancellationToken.None);
            }
        }
Exemplo n.º 5
0
        private static void EnsureExpectedAliasesExist(IElasticClient client, IEnumerable <string> expectedAliasNames)
        {
            var missingAliasNames = expectedAliasNames
                                    .Where(aliasName => !(client.AliasExists(aliasName).Exists || client.IndexExists(aliasName).Exists))
                                    .ToList();

            if (missingAliasNames.Count == 0)
            {
                return;
            }

            var message = string.Format(
                "Failed to find {0} Elasticsearch alias(es): \"{1}\".",
                missingAliasNames.Count, ToCommaSeparatedString(missingAliasNames));

            throw new Exception(message);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 重建索引
        /// </summary>
        /// <param name="client"></param>
        /// <param name="oldIndexName">就索引名字</param>
        /// <param name="newIndexName">新索引名字</param>
        /// <param name="shardsNumber"></param>
        /// <returns></returns>
        public static bool ReIndex <T>(IElasticClient client, string oldIndexName, string newIndexName, int shardsNumber = 1) where T : class
        {
            //检查新索引别名
            var alias = client.AliasExists($"{newIndexName}-reindex");

            //如果改别名不存在
            if (!alias.Exists)
            {
                //检查是否存在新索引
                IExistsResponse newIndex = client.IndexExists(newIndexName);
                client.UpdateIndexSettings(Indices.Index(newIndexName), setting => setting.IndexSettings(isetting => isetting.Setting("max_result_window", 100000)));
                //如果新索引不存在则创建一个新索引,需要修改各项创建索引时才需要的参数则在这处理
                if (!newIndex.Exists)
                {
                    CreateIndexDescriptor createIndex = new CreateIndexDescriptor(newIndexName)
                                                        .Settings(s => s.NumberOfShards(shardsNumber).Analysis(a => a.Analyzers(aa => aa.Language("standard_listing", sa => sa.Language(Language.Chinese)))))
                                                        .Mappings(ms => ms.Map <T>(m => m.AutoMap()));
                    ICreateIndexResponse create = client.CreateIndex(createIndex);
                    Console.WriteLine($"{newIndexName}索引创建:{create.ApiCall.Success}");
                    //给新创建的索引添加一个索引别名表示时重新创建的
                    var r2 = client.Alias(t => t.Add(a => a.Index(newIndexName).Alias($"{newIndexName}-reindex")));
                    Console.WriteLine($"索引别名{newIndexName}=>{newIndexName}:{r2.ApiCall.Success}");
                }
                //将老索引中的文档索引导新建的索引
                var r1 = client.ReindexOnServer(r => r.Source(s => s.Index(Indices.Index(oldIndexName))).Destination(d => d.Index(newIndexName)));
                Console.WriteLine($"重新索引:{r1.ApiCall.Success}");
                if (r1.ApiCall.Success)
                {
                    //老索引中的所有文档全部到新索引之后删除老索引
                    client.DeleteIndex(Indices.Index(oldIndexName));
                    //新索引设置别名为老索引的名字
                    var r3 = client.Alias(t => t.Add(a => a.Index(newIndexName).Alias(oldIndexName)));
                    Console.WriteLine($"索引别名{newIndexName}=>{oldIndexName}:{r3.ApiCall.Success}");
                }
            }
            return(alias.Exists);
        }
Exemplo n.º 7
0
 public static IExistsResponse AliasExists(this IElasticClient client, string aliasName, string indexName = null)
 {
     return(client.AliasExists(new AliasExistsRequest(indexName, aliasName)));
 }
        public void ConfigureIndexes(IElasticClient client)
        {
            foreach (var index in GetIndexes())
            {
                IIndicesOperationResponse response = null;
                int currentVersion = GetAliasVersion(client, index.Name);

                var templatedIndex = index as ITemplatedElasticSeachIndex;
                if (templatedIndex != null)
                {
                    response = client.PutTemplate(index.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(index.Name));
                }
                else if (!client.IndexExists(index.VersionedName).Exists)
                {
                    response = client.CreateIndex(index.VersionedName, descriptor => index.CreateIndex(descriptor).AddAlias(index.Name));
                }

                Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template.");

                // Add existing indexes to the alias.
                if (!client.AliasExists(index.Name).Exists)
                {
                    if (templatedIndex != null)
                    {
                        var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(index.VersionedName)).Select(kvp => kvp.Key).ToList();
                        if (indices.Count > 0)
                        {
                            var descriptor = new AliasDescriptor();
                            foreach (string name in indices)
                            {
                                descriptor.Add(add => add.Index(name).Alias(index.Name));
                            }

                            response = client.Alias(descriptor);
                        }
                    }
                    else
                    {
                        response = client.Alias(a => a.Add(add => add.Index(index.VersionedName).Alias(index.Name)));
                    }

                    Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias.");
                }

                // already on current version
                if (currentVersion >= index.Version || currentVersion < 1)
                {
                    continue;
                }

                // upgrade
                _lockProvider.TryUsingAsync("reindex", async() => {
                    await _workItemQueue.EnqueueAsync(new ReindexWorkItem {
                        OldIndex  = String.Concat(index.Name, "-v", currentVersion),
                        NewIndex  = index.VersionedName,
                        Alias     = index.Name,
                        DeleteOld = true
                    });
                }, TimeSpan.Zero, CancellationToken.None);
            }
        }
Exemplo n.º 9
0
        public virtual void ConfigureIndexes(IElasticClient client, IEnumerable <IElasticIndex> indexes = null)
        {
            if (indexes == null)
            {
                indexes = GetIndexes();
            }

            foreach (var idx in indexes)
            {
                int currentVersion = GetAliasVersion(client, idx.AliasName);

                IIndicesOperationResponse response = null;
                var templatedIndex = idx as ITemplatedElasticIndex;
                if (templatedIndex != null)
                {
                    response = client.PutTemplate(idx.VersionedName, template => templatedIndex.CreateTemplate(template).AddAlias(idx.AliasName));
                }
                else if (!client.IndexExists(idx.VersionedName).Exists)
                {
                    response = client.CreateIndex(idx.VersionedName, descriptor => idx.CreateIndex(descriptor).AddAlias(idx.AliasName));
                }

                Debug.Assert(response == null || response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the index or template.");

                // Add existing indexes to the alias.
                if (!client.AliasExists(idx.AliasName).Exists)
                {
                    if (templatedIndex != null)
                    {
                        var indices = client.IndicesStats().Indices.Where(kvp => kvp.Key.StartsWith(idx.VersionedName)).Select(kvp => kvp.Key).ToList();
                        if (indices.Count > 0)
                        {
                            var descriptor = new AliasDescriptor();
                            foreach (string name in indices)
                            {
                                descriptor.Add(add => add.Index(name).Alias(idx.AliasName));
                            }

                            response = client.Alias(descriptor);
                        }
                    }
                    else
                    {
                        response = client.Alias(a => a.Add(add => add.Index(idx.VersionedName).Alias(idx.AliasName)));
                    }

                    Debug.Assert(response != null && response.IsValid, response?.ServerError != null ? response.ServerError.Error : "An error occurred creating the alias.");
                }

                // already on current version
                if (currentVersion >= idx.Version || currentVersion < 1)
                {
                    continue;
                }

                var reindexWorkItem = new ReindexWorkItem {
                    OldIndex   = String.Concat(idx.AliasName, "-v", currentVersion),
                    NewIndex   = idx.VersionedName,
                    Alias      = idx.AliasName,
                    DeleteOld  = true,
                    ParentMaps = idx.GetIndexTypes()
                                 .Select(kvp => new ParentMap {
                        Type = kvp.Value.Name, ParentPath = kvp.Value.ParentPath
                    })
                                 .Where(m => !String.IsNullOrEmpty(m.ParentPath))
                                 .ToList()
                };

                bool isReindexing = _lockProvider.IsLockedAsync(String.Concat("reindex:", reindexWorkItem.Alias, reindexWorkItem.OldIndex, reindexWorkItem.NewIndex)).Result;
                // already reindexing
                if (isReindexing)
                {
                    continue;
                }

                // enqueue reindex to new version
                _lockProvider.TryUsingAsync("enqueue-reindex", () => _workItemQueue.EnqueueAsync(reindexWorkItem), TimeSpan.Zero, CancellationToken.None).Wait();
            }
        }