Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
 /// <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);
 }
Exemplo n.º 3
0
        /// <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);
        }
Exemplo n.º 4
0
        /// <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);
        }