Esempio n. 1
0
        public static string Save(IEnumerable <iElasticSearchObject> results, string collection)
        {
            if (results != null && results.Count() > 0)
            {
                ElasticHelper  retVal = new ElasticHelper();
                HttpWebRequest req    = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/result/_bulk", ElasticURL, collection));
                req.ContentType = "application/json";
                req.Method      = "PUT";
                req.Timeout     = 1000 * 60 * 3;//3minutes

                StringBuilder sb = new StringBuilder();
                foreach (iElasticSearchObject r in results)
                {
                    try
                    {
                        sb.AppendLine(string.Format("{{ \"index\": {{ \"_id\":\"{0}\" }} }}", r._id));
                        sb.AppendLine(Newtonsoft.Json.JsonConvert.SerializeObject(r, Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings {
                            NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
                        }));
                    }
                    catch
                    { }
                }


                string eh = req.GetResponseString(sb.ToString());
                //if (eh != null)
                //{
                //    retVal = Newtonsoft.Json.JsonConvert.DeserializeObject<ElasticHelper>(eh);
                //}
                //return retVal;
                return(eh);
            }
            return("");
        }
Esempio n. 2
0
        public static ElasticHelper Delete(string id)
        {
            ElasticHelper  retVal = new ElasticHelper();
            HttpWebRequest req    = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/result/{2}", ElasticURL, Collection, id));

            req.ContentType = "application/json";
            req.Method      = "DELETE";
            string eh = req.GetResponseString();

            if (eh != null)
            {
                retVal = Newtonsoft.Json.JsonConvert.DeserializeObject <ElasticHelper>(eh);
            }
            return(retVal);
        }
Esempio n. 3
0
        public static ElasticHelper Save(string id, object value)
        {
            ElasticHelper retVal = new ElasticHelper();
            //HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/result/{2}", ElasticURL, Collection, id));
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/result/{2}", ElasticURL, Collection, id));

            req.ContentType = "application/json";
            req.Method      = "PUT";
            string ser = Newtonsoft.Json.JsonConvert.SerializeObject(value, Newtonsoft.Json.Formatting.Indented, new Newtonsoft.Json.JsonSerializerSettings {
                NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
            });
            string eh = req.GetResponseString(ser);

            if (eh != null)
            {
                retVal = Newtonsoft.Json.JsonConvert.DeserializeObject <ElasticHelper>(eh);
            }
            return(retVal);
        }
Esempio n. 4
0
        public static t FillEntity <t>(string id, string collection)
        {
            t tmp = default(t);

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}{1}/result/{2}", ElasticHelper.ElasticURL, collection, id));

            req.ContentType = "application/json";
            req.Method      = "GET";
            string retval = req.GetResponseString();

            if (!string.IsNullOrEmpty(retval))
            {
                ElasticHelper eh = Newtonsoft.Json.JsonConvert.DeserializeObject <ElasticHelper>(retval);
                if (eh != null && eh.found && eh._source != null)
                {
                    tmp = (t)Newtonsoft.Json.JsonConvert.DeserializeObject(eh._source.ToString(), typeof(t));
                }
            }

            return(tmp);
        }
Esempio n. 5
0
 public static t FillEntity <t>(string id)
 {
     return(ElasticHelper.FillEntity <t>(id, ElasticHelper.Collection));
 }