/// <summary>
        /// 创建索引
        /// </summary>
        /// <param name="esCreateIndexSettings">创建索引配置</param>
        /// <param name="mappingHandle">映射处理</param>
        internal static void CreateIndex(EsCreateIndexSettings esCreateIndexSettings, Action <ElasticClient, string> mappingHandle = null)
        {
            var aliasIndex     = esCreateIndexSettings.AliasIndex;
            var numberOfShards = esCreateIndexSettings.NumberOfShards;
            var index          = esCreateIndexSettings.Index;
            var currClient     = GetClient();

            if (string.IsNullOrEmpty(aliasIndex))
            {
                aliasIndex = EsClientProvider.BaseEsConnectionSettings.EsDefaultIndex;
            }

            //验证索引是否存在
            if (!currClient.IndexExists(index).Exists)
            {
                IIndexState indexState = new IndexState()
                {
                    Settings = new IndexSettings()
                    {
                        NumberOfReplicas = 0,
                        NumberOfShards   = numberOfShards
                    }
                };
                //按别名创建索引
                if (!string.IsNullOrEmpty(aliasIndex) && !aliasIndex.Equals(index))
                {
                    currClient.CreateIndex(index, c => c.InitializeUsing(indexState).Aliases(a => a.Alias(aliasIndex)));
                }
                else
                {
                    currClient.CreateIndex(index, c => c.InitializeUsing(indexState));
                }
            }
            mappingHandle?.Invoke(currClient, index);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 创建索引
        /// </summary>
        private void IndexCreateAndMapping()
        {
            var esMappingSettings = new EsCreateIndexSettings()
            {
                NumberOfShards = NumberOfShards,
                AliasIndex     = AliasIndex,
                Index          = CurrentIndex
            };

            EsClientProvider.CreateIndex(esMappingSettings, EntityMapping);
            IndexCreateEnd();
        }