elasticsearch bulk批量操作

说明: 在做业务增删改时,有时批量执行比循环中单个执行性能要好很多,在es中有批量执行方法(除了script),如下(.net)代码:

        public BulkResponse BulkUpdate(List<IBulkOperation> bulkOperationList)
        {
            BulkRequest bulkRequest = new BulkRequest(base._indexName);
            bulkRequest.Operations = bulkOperationList;
            return _client.Bulk(bulkRequest);
        }

    _client是指IElasticClient对象,添加Nest包。

    下面代码是指:根据索引Ids集合,查询出索引数据,再批量更新SendStatus和时间字段,如下所示:

           var models = res.SourceMany<CrawlHqbuyNewsContentEsModel>(Ids).ToList();
            var operationList = new List<IBulkOperation>();
            var time = DateTime.Now;
            foreach (var model in models)
            {
                model.SendStatus = 1;
                model.UpdateTime = time.ToString(FormatCharacter.DATETIME);
                model.UpdateTimeStamp = TimeStampUtil.GenerateTimeStampBySeconds(time);
                operationList.Add(new BulkIndexOperation<CrawlHqbuyNewsContentEsModel>(model) { Id = model.Id });
            }
            var res2 = _esClient.BulkUpdate(operationList);
            if (!res2.IsValid)
            {
                apiResult.Message = "确认发送状态操作失败,具体咨询开发者!";
                LogUtil.NLog.Error("确认发送状态操作失败," + res2.DebugInformation);
                return apiResult;
            }

            var count = res2.Items.ToList().Count;
            apiResult.Code = StatusCodeEnum.Success;
            apiResult.Message = $"确认发送状态操作成功,共{count}条!";
            LogUtil.NLog.Info($"确认发送状态操作成功,共{count}条!");

    上面代码调用BulkIndexOperation为什么会更新索引数据呢,是因为索引中如果该数据Id存在,就会是更新,如果不存在就是新增。当然可以调用相关类:

    BulkCreateOperation为新增

    BulkDeleteOperation为删除

    BulkUpdateOperation为修改

    

 

posted on 2023-06-21 10:13  花阴偷移  阅读(156)  评论(0编辑  收藏  举报

导航