/// <summary>
        /// 更新文档
        /// </summary>
        /// <param name="index"></param>
        /// <param name="type"></param>
        /// <param name="doc"></param>
        public bool UpdateDocument(string index, string type, string id, BsonDocument doc)
        {
            bool flag = false;

            if (doc.Names.Count() < 1)
            {
                flag = true;
                return(flag);
            }

            StringResponse resStr = null;

            try
            {
                resStr = client.Update <StringResponse>(index, type, id, PostData.String(BsonDocument.Parse($"{{'doc':{doc}}}").ToJson(new JsonWriterSettings {
                    OutputMode = JsonOutputMode.Strict
                })));
                var resObj = JObject.Parse(resStr.Body);
                if ((int)resObj["_shards"]["total"] == 0 || (int)resObj["_shards"]["successful"] > 0)
                {
                    flag = true;
                }
                else
                {
                    LogUtil.LogInfo(logger, resStr.DebugInformation, nodeId);
                }
            }
            catch (Exception ex)
            {
                if (resStr != null)
                {
                    LogUtil.LogInfo(logger, resStr.DebugInformation, nodeId);
                }
                LogUtil.LogError(logger, ex.ToString(), nodeId);
            }

            return(flag);
        }
        public JsonResult Update(int id, string realname, string description)
        {
            var oResult = new Object();

            try
            {
                var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/"))
                               .RequestTimeout(TimeSpan.FromMinutes(2));

                var lowlevelClient = new ElasticLowLevelClient(settings);

                var searchResponse = lowlevelClient.Update <string>("user", "guest", id.ToString(), new
                {
                    doc = new
                    {
                        RealName    = realname,
                        Description = description
                    }
                });

                bool successful = searchResponse.Success;

                oResult = new
                {
                    Bresult = successful,
                    Notice  = successful ? "修改成功!" : "修改失败!"
                };

                var responseJson = searchResponse.Body;

                return(Json(oResult, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                oResult = new
                {
                    Bresult = false,
                    Notice  = String.Format("修改失败!异常:{0}", ex.Message)
                };
                return(Json(oResult, JsonRequestBehavior.AllowGet));
            }
        }
Exemple #3
0
 public void updateDocument(string id, string doc)
 {
     PostData body     = PostData.String(doc);
     var      response = client.Update <BytesResponse>(indexname, id, body);
 }
        protected override void Execute(CodeActivityContext context)
        {
            {
                String Error   = "";
                bool   Res     = false;
                bool   Success = false;
                String Resp    = "";

                try
                {
                    var node = new Uri(URL.Get(context));
                    ConnectionConfiguration config;

                    if (AuthenticationRequired.Get(context) == true)
                    {
                        config = new ConnectionConfiguration(node).BasicAuthentication(UserName.Get(context), Password.Get(context));
                    }
                    else
                    {
                        config = new ConnectionConfiguration(node);
                    }

                    var lowlevelClient = new ElasticLowLevelClient(config);


                    string data = @"{""doc"": " + JsonData.Get(context) + "}";
                    Console.WriteLine(data);
                    var updateData = lowlevelClient.Update <BytesResponse>
                                         (Index.Get(context), Type.Get(context), ID.Get(context), data);

                    var responseBytes = updateData.Body;
                    if (responseBytes == null)
                    {
                        responseBytes = updateData.ResponseBodyInBytes;
                    }

                    Resp = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);

                    Success = updateData.Success;

                    if (Success == true)
                    {
                        Error = "";
                        Res   = true;

                        ErrorMessage.Set(context, Error);
                        Response.Set(context, Resp);
                        Result.Set(context, Res);
                    }
                    else
                    {
                        throw updateData.OriginalException;
                        //ErrorMessage = insertData.OriginalException.ToString();
                        //Result = false;
                    }
                }
                catch (Exception ex)
                {
                    Error = "Failed to update the data." + ex.Message;
                    Res   = false;

                    ErrorMessage.Set(context, Error);
                    Response.Set(context, Resp);
                    Result.Set(context, Res);
                }
            }
        }