示例#1
0
        /// <summary>
        /// 根据已知Id,进行添加或更新
        /// </summary>
        /// <typeparam name="Tentity"></typeparam>
        /// <param name="id"></param>
        /// <param name="entity"></param>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public virtual async Task <bool> AddOrUpdateAsync <Tentity>(Id id, Tentity entity, string indexName = null) where Tentity : class
        {
            IExistsResponse exists = string.IsNullOrWhiteSpace(indexName) ? await Context.DocumentExistsAsync <Tentity>(id) : await Context.DocumentExistsAsync <Tentity>(id, q => q.Index(indexName));

            if (exists.Exists)
            {
                return(await UpdateAsync(entity, indexName));
            }
            else
            {
                return(await AddAsync(entity, indexName));
            }
        }
        public async Task <bool> ExistsAsync(long id)
        {
            IExistsResponse result = await this._esClient.DocumentExistsAsync <T>(id, d => d
                                                                                  .Index(this._entityIndex)
                                                                                  .Type(this._entityType));

            if (!result.IsValid)
            {
                this._logger.LogError(result.OriginalException, $"Error deleting entities of type {this._entityType} from index {this._entityIndex}.", new[] { result });
                throw result.OriginalException;
            }

            return(result.Exists);
        }
示例#3
0
        public override async Task <bool> ExistsAsync(CancellationToken cancellationToken)
        {
            // Create the request.
            IIndexExistsRequest request = new IndexExistsDescriptor(Indices.Index <T>());

            // Get the response.
            IExistsResponse response = await ElasticClient.IndexExistsAsync(request, cancellationToken)
                                       .ConfigureAwait(false);

            // Throw if invalid.
            response.ThrowIfError();

            // Return the response.
            return(response.Exists);
        }
        public ElasticSearch(IOptions <AWSConfiguration> AWSConfiguration) : base(AWSConfiguration.Value.elasticSearchEndpoint)
        {
            _connnectionPool      = new SingleNodeConnectionPool(new Uri(AWSConfiguration.Value.elasticSearchEndpoint));
            _awsConnectionSetting = new ConnectionSettings(_connnectionPool);
            _elasticClient        = new ElasticClient(_awsConnectionSetting);

            try
            {
                IExistsResponse _checkIndex = _elasticClient.IndexExists(_indexName);

                _awsConnectionSetting.DefaultIndex(_indexName).DefaultTypeName(_indexName);
                _elasticClient = new ElasticClient(_awsConnectionSetting);
            }
            catch
            {
            }
        }
示例#5
0
        private async Task EnsureIndexExists(string indexName, ElasticClient esClient)
        {
            IExistsResponse existsResult = await esClient.IndexExistsAsync(indexName).ConfigureAwait(false);

            if (!existsResult.IsValid)
            {
                this.ReportEsRequestError(existsResult, "Index exists check");
            }

            if (existsResult.Exists)
            {
                return;
            }

            // TODO: allow the consumer to fine-tune index settings
            IndexState indexSettings = new IndexState();

            indexSettings.Settings = new IndexSettings();
            indexSettings.Settings.NumberOfReplicas = this.connectionData.Configuration.NumberOfReplicas;
            indexSettings.Settings.NumberOfShards   = this.connectionData.Configuration.NumberOfShards;
            indexSettings.Settings.Add("refresh_interval", this.connectionData.Configuration.RefreshInterval);
            if (this.connectionData.Configuration.DefaultPipeline != null)
            {
                indexSettings.Settings.Add("default_pipeline", this.connectionData.Configuration.DefaultPipeline);
            }

            ICreateIndexResponse createIndexResult = await esClient.CreateIndexAsync(indexName, c => c.InitializeUsing(indexSettings)).ConfigureAwait(false);

            if (!createIndexResult.IsValid)
            {
                try
                {
                    if (createIndexResult.ServerError?.Error?.Type != null &&
                        Regex.IsMatch(createIndexResult.ServerError.Error.Type, "index.*already.*exists.*exception", RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(500)))
                    {
                        // This is fine, someone just beat us to create a new index.
                        return;
                    }
                }
                catch (RegexMatchTimeoutException) { }

                this.ReportEsRequestError(createIndexResult, "Create index");
            }
        }
示例#6
0
        public async Task <IActionResult> DelDoc([FromForm] int id)
        {
            //判断文档是否存在,这里是根据索引名字(数据库),索引type(表),和该文档的id来判断的(主键)
            IExistsResponse existsResponse = await _esClient.DocumentExistsAsync(new DocumentExistsRequest("users", "appuser", id));

            if (existsResponse.Exists)
            {
                //删除对应id的文档
                var res = await _esClient.DeleteByQueryAsync <AppUser>(d =>
                {
                    return(d.Index("users").Type("appuser").Query(q => q.Term(tm => tm.Field(new Field("id")).Value(id))));
                });

                if (res.IsValid)
                {
                    return(Ok());
                }
                return(BadRequest());
            }
            return(NoContent());
        }
        private async Task EnsureIndexExists(string currentIndexName, ElasticClient esClient)
        {
            IExistsResponse existsResult = await esClient.IndexExistsAsync(currentIndexName);

            if (!existsResult.IsValid)
            {
                this.ReportEsRequestError(existsResult, "Index exists check");
            }

            if (existsResult.Exists)
            {
                return;
            }

            // TODO: allow the consumer to fine-tune index settings
            IndexState indexState = new IndexState();

            indexState.Settings = new IndexSettings();
            indexState.Settings.NumberOfReplicas = 1;
            indexState.Settings.NumberOfShards   = 5;
            indexState.Settings.Add("refresh_interval", "15s");

            ICreateIndexResponse createIndexResult = await esClient.CreateIndexAsync(currentIndexName, c => c.InitializeUsing(indexState));

            if (!createIndexResult.IsValid)
            {
                if (createIndexResult.ServerError != null &&
                    createIndexResult.ServerError.Error != null &&
                    string.Equals(createIndexResult.ServerError.Error.Type, "IndexAlreadyExistsException", StringComparison.OrdinalIgnoreCase))
                {
                    // This is fine, someone just beat us to create a new index.
                    return;
                }

                this.ReportEsRequestError(createIndexResult, "Create index");
            }
        }
示例#8
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);
        }
示例#9
0
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="client"></param>
 /// <param name="indexName">索引名</param>
 /// <param name="newIndexName">新索引名,重建索引用</param>
 /// <param name="max_result_window"></param>
 /// <param name="shardsNumber">分片数</param>
 /// <returns></returns>
 public static IElasticClient CreateOrUpdateIndex <T>(this IElasticClient client, string indexName, string newIndexName = null, long max_result_window = 100000, int shardsNumber = 1) where T : class
 {
     try
     {
         if (!string.IsNullOrWhiteSpace(newIndexName))
         {
             var x = ReIndex <T>(client, indexName, newIndexName, shardsNumber);
             if (x == false)
             {
                 indexName = newIndexName;
             }
         }
         IExistsResponse index = client.IndexExists(indexName);
         client.UpdateIndexSettings(Indices.Index(indexName), setting => setting.IndexSettings(isetting => isetting.Setting("max_result_window", max_result_window)));
         if (!index.Exists)
         {
             CreateIndexDescriptor newIndex = new CreateIndexDescriptor(indexName)
                                              .Settings(s => s.NumberOfShards(shardsNumber).NumberOfReplicas(1).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(newIndex);
             Console.WriteLine($"[{indexName}]\t索引已创建。");
         }
         else
         {
             IPutMappingResponse putMap = client.Map <T>(m => m.AutoMap());
             if (putMap.ApiCall.Success)
             {
                 Console.WriteLine($"[{indexName}]\t索引已更新。");
             }
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
     }
     return(client);
 }
示例#10
0
        protected void CreateInitialIndex(ElasticClient client)
        {
            pluginInstance.LogInfo("Setting up initial index and alias");

            IExistsResponse aliasExists = client.AliasExists("active-homeseer-index");

            if (aliasExists.Exists)
            {
                pluginInstance.LogInfo("Alias 'active-homeseer-index' already exists!");
                return;
            }

            pluginInstance.LogInfo("Creating alias and initial index");

            try
            {
                client.CreateIndex("homeseer-events-00001", index => index.Aliases(alias => alias.Alias("active-homeseer-index")));
            }
            catch (Exception e)
            {
                pluginInstance.LogError(string.Format("Error creating index or alias: {0}", e.Message));
                throw;
            }
        }
示例#11
0
        public async Task <IActionResult> InsertDoc([FromBody] AppUser u)
        {
            //判断索引是否存在
            var indexExist = await _esClient.IndexExistsAsync("users");

            //判断文档是否存在,这里是根据索引名字(数据库),索引type(表),和该文档的id来判断的(主键)
            IExistsResponse existsResponse = await _esClient.DocumentExistsAsync(new DocumentExistsRequest("users", "appuser", u.Id));

            if (indexExist.Exists && !existsResponse.Exists)
            {
                ////创建索引users,如果索引不存在会自动创建,并将上面user的值写入该索引的文档
                var response = await _esClient.IndexAsync(u, index =>
                {
                    return(index.Index("users"));
                });

                //写入成功
                if (response.IsValid)
                {
                    return(Ok());
                }
            }
            return(BadRequest());
        }
 protected override void ExpectResponse(IExistsResponse response)
 {
     response.Should().NotBeNull();
     response.Exists.Should().BeTrue();
 }
示例#13
0
 protected override void ExpectResponse(IExistsResponse response)
 {
     response.Exists.Should().BeFalse();
 }