public ESDocument <T> Get <T>(string indexname, string typename, string documentid) { string url = _esBaseUrl + indexname + "/" + typename + "/" + documentid; var httpresponse = esreqest.DoRequest(url, string.Empty); if (!httpresponse.Successed) { return(null); } var response = JsonHelper.JsonToEntity <GetDocumentResponse <T> >(httpresponse.ResponseContent); if (!response.Found) { return(null); } ESDocument <T> doc = new ESDocument <T>(); doc.Document = response.Data; doc.DocumentID = response.ID; doc.Version = response.Version; return(doc); }
/// <summary> /// 部分更新 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="doc"></param> /// <param name="predicate"></param> /// <returns></returns> public BulkOpBuilder Update <T>(ESDocument <T> doc, params Expression <Func <T, object> >[] predicate) { if (doc != null) { sb.AppendFormat("{{\"{0}\":{{ \"_index\":\"{1}\",\"_type\":\"{2}\",\"_id\":\"{3}\",\"_retry_on_conflict\":3}}}}\n", OpTypes.update, doc.IndexName, doc.DocumentType, doc.DocumentID); if (predicate == null || predicate.Length == 0) { sb.AppendFormat("{{\"doc\":{0}}}\n", doc.ToString()); } else { sb.Append("{{"); string membername = string.Empty; using (Newtonsoft.Json.JsonTextWriter jw = new Newtonsoft.Json.JsonTextWriter(new StringWriter(sb))) { foreach (var selecter in predicate) { jw.WritePropertyName(JsonHelper.GetJsonTag(selecter, out membername)); var val = doc.Document.Eval(membername); jw.WriteValue(val); } } sb.Append("}}\n"); } } return(this); }
/// <summary> /// 删除 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="doc"></param> /// <returns></returns> public BulkOpBuilder Delete <T>(ESDocument <T> doc) { if (doc != null) { sb.AppendFormat("{{ \"{0}\": {{ \"_index\": \"{1}\", \"_type\": \"{2}\", \"_id\": \"{3}\" }}}}\n", OpTypes.delete, doc.IndexName, doc.DocumentType, doc.DocumentID); } return(this); }
/// <summary> /// 文档不存在时创建 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="doc"></param> /// <returns></returns> public BulkOpBuilder Create <T>(ESDocument <T> doc) { if (doc != null) { if (string.IsNullOrWhiteSpace(doc.DocumentID)) { throw new ArgumentNullException("DocumentID"); } sb.AppendFormat("{{ \"{0}\": {{ \"_index\": \"{1}\", \"_type\": \"{2}\", \"_id\": \"{3}\" }}}}\n", OpTypes.create, doc.IndexName, doc.DocumentType, doc.DocumentID); sb.Append(doc.ToString() + "\n"); } return(this); }
/// <summary> /// 创建或者替换,如果没有指定ID,则创建,指定了则替换 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="doc"></param> /// <returns></returns> public BulkOpBuilder Index <T>(ESDocument <T> doc) { if (doc != null) { if (string.IsNullOrWhiteSpace(doc.DocumentID)) { sb.AppendFormat("{{ \"{0}\": {{ \"_index\": \"{1}\", \"_type\": \"{2}\" }}}}\n", OpTypes.index, doc.IndexName, doc.DocumentType); sb.Append(doc.ToString() + "\n"); } else { sb.AppendFormat("{{ \"{0}\": {{ \"_index\": \"{1}\", \"_type\": \"{2}\",\"_id\":{3} }}}}\n", OpTypes.index, doc.IndexName, doc.DocumentType, doc.DocumentID); sb.Append(doc.ToString() + "\n"); } } return(this); }
/// <summary> /// 索引文档 /// </summary> /// <param name="document"></param> public string Index <T>(ESDocument <T> document) { if (document == null || document.IsEmptyDocument()) { return(string.Empty); } string url = _esBaseUrl + document.IndexName + "/" + document.DocumentType + "/" + (document.DocumentID ?? ""); var httpresponse = esreqest.DoRequest(url, document.ToString(), document.DocumentID == null ? WebRequestMethodEnum.POST : WebRequestMethodEnum.PUT, false); if (!httpresponse.Successed) { return(string.Empty); } var response = JsonHelper.JsonToEntity <IndexResponse>(httpresponse.ResponseContent); return(response.ID); }