Ejemplo n.º 1
0
 public void DeleteFromIndex(Auto auto)
 {
     try
     {
         var response = HttpWebService.MakeHttpWebRequest($"{XCarsConfiguration.AmazonESDomenEndpoint}" + $"/{XCarsConfiguration.AmazonESDomenIndex}" + $"/{XCarsConfiguration.AmazonESDomenIndexMapping}/" + auto.ID, "DELETE");
     }
     catch (Exception ex)
     { }
 }
Ejemplo n.º 2
0
        public void UpdateCustomFields(Dictionary <int, List <Tuple <string, string, string> > > param)
        {
            int length = 10000;

            int.TryParse(XCarsConfiguration.AmazonESDomenIndexBulkUpdateMaxLength, out length);

            string postData = @"";
            string quot     = "";

            int j = 0;

            foreach (var item in param)
            {
                postData += @"{ ""update"" : {""_id"" : """ + item.Key + @""", ""_type"" : """ + XCarsConfiguration.AmazonESDomenIndexMapping + @""", ""_index"" : """ + XCarsConfiguration.AmazonESDomenIndex + @"""} }";
                postData += @"{ ""doc"" : {";

                int i = 0;
                foreach (var listItem in item.Value)
                {
                    if (i > 0)
                    {
                        postData += ",";
                    }

                    quot = @"""";
                    if (listItem.Item3 == "int" || listItem.Item3 == "bool")
                    {
                        quot = @"";
                    }
                    postData += @"""" + listItem.Item1 + @""" : " + quot + @"" + listItem.Item2 + quot;
                    i++;
                }

                postData += @"} }
";
                j++;
                if (j == length)
                {
                    HttpWebService.MakeHttpWebRequest($"{XCarsConfiguration.AmazonESDomenEndpoint}" + $"/{XCarsConfiguration.AmazonESDomenIndex}" + "/_bulk", "POST", postData);
                    j        = 0;
                    postData = "";
                }
            }

            if (j > 0)
            {
                HttpWebService.MakeHttpWebRequest($"{XCarsConfiguration.AmazonESDomenEndpoint}" + $"/{XCarsConfiguration.AmazonESDomenIndex}" + "/_bulk", "POST", postData);
            }
        }
Ejemplo n.º 3
0
        public void AddToIndex(Auto auto)
        {
            try
            {
                List <Tuple <string, string, string> > additionalFields = new List <Tuple <string, string, string> >()
                {
                    new Tuple <string, string, string>("InFavorites", auto.AutoFavorites.Count.ToString(), "int")
                };

                var postData = BuildPostData(auto, additionalFields);
                var response = HttpWebService.MakeHttpWebRequest($"{XCarsConfiguration.AmazonESDomenEndpoint}" + $"/{XCarsConfiguration.AmazonESDomenIndex}" + $"/{XCarsConfiguration.AmazonESDomenIndexMapping}/" + auto.ID, "PUT", postData);
            }
            catch (Exception ex)
            { }
        }
Ejemplo n.º 4
0
        public void UpdatePriceForAllAutosInIndex(double rate)
        {
            int start = 0;

            int length = 10000;

            int.TryParse(XCarsConfiguration.AmazonESDomenIndexBulkUpdateMaxLength, out length);

            Dictionary <int, List <Tuple <string, string, string> > > param = new Dictionary <int, List <Tuple <string, string, string> > >();

            bool entitiesExist = true;

            while (entitiesExist)
            {
                var postData = @"{
                    ""from"": " + start + @", 
                    ""size"" : " + length + @",
	                ""_source"": [
                        ""AutoID"",
                        ""PriceUSD"",
		                ""PriceUAH""
	                ]"    ;
                postData += @",
	                ""query"":
	                {
                        ""bool"":
		                {
                            ""must"":
                            [ ],
			                ""filter"":
			                [
				                {""term"": {""Status.ID"": 2 }}
			                ]
		                }
	                }
                }";

                var response            = HttpWebService.MakeHttpWebRequest($"{XCarsConfiguration.AmazonESDomenEndpoint}" + $"/{XCarsConfiguration.AmazonESDomenIndex}" + "/_search", "POST", postData);
                var reader              = new StreamReader(response.GetResponseStream());
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText             = reader.ReadToEnd();
                var responseJSON        = (dynamic)js.Deserialize <dynamic>(objText);

                int total = 0;
                foreach (var item in responseJSON["hits"]["hits"])
                {
                    int autoID = 0;
                    int.TryParse(item["_source"]["AutoID"].ToString(), out autoID);
                    if (autoID == 0)
                    {
                        continue;
                    }

                    int priceUSDSearch = 0;
                    int priceUAHSearch = 0;

                    string priceUSDStr = null;
                    string priceUAHStr = null;
                    foreach (var property in item["_source"].Keys)
                    {
                        if (property == "PriceUSD")
                        {
                            priceUSDStr = item["_source"]["PriceUSD"].ToString();
                        }
                        if (property == "PriceUAH")
                        {
                            priceUAHStr = item["_source"]["PriceUAH"].ToString();
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(priceUSDStr) && priceUSDStr != "0")
                    {
                        int.TryParse(priceUSDStr, out priceUSDSearch);
                    }
                    else if (!string.IsNullOrWhiteSpace(priceUAHStr) && priceUAHStr != "0")
                    {
                        int.TryParse(priceUAHStr, out priceUSDSearch);
                        double tmp = priceUSDSearch;
                        tmp            = tmp / rate;
                        priceUSDSearch = (int)tmp;
                    }

                    if (!string.IsNullOrWhiteSpace(priceUAHStr) && priceUAHStr != "0")
                    {
                        int.TryParse(priceUAHStr, out priceUAHSearch);
                    }
                    else if (!string.IsNullOrWhiteSpace(priceUSDStr) && priceUSDStr != "0")
                    {
                        int.TryParse(priceUSDStr, out priceUAHSearch);
                        double tmp = priceUAHSearch;
                        tmp            = tmp * rate;
                        priceUAHSearch = (int)tmp;
                    }

                    param[autoID] = new List <Tuple <string, string, string> >();
                    param[autoID].Add(new Tuple <string, string, string>("PriceUSDSearch", priceUSDSearch.ToString(), "int"));
                    param[autoID].Add(new Tuple <string, string, string>("PriceUAHSearch", priceUAHSearch.ToString(), "int"));

                    total++;
                }

                if (total < length)
                {
                    entitiesExist = false;
                }
                start += total;
            }

            UpdateCustomFields(param);
        }