Exemplo n.º 1
0
        private async void comboxIndex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EsIndex esIndex = comboxIndex.SelectedItem as EsIndex;

            if (esIndex != null)
            {
                List <string> list = await EsService.GetFieldsByJson(esdata.EsConnInfo, esIndex.Name);

                //comboxField.SelectionChanged -= comboxField_SelectionChanged;
                comboxField.Items.Clear();
                comboxField.Items.Add(new ComboBoxItem()
                {
                    Content = "match_all", IsSelected = true
                });
                comboxField.Items.Add(new ComboBoxItem()
                {
                    Content = "_all"
                });
                foreach (string str in list)
                {
                    comboxField.Items.Add(new ComboBoxItem()
                    {
                        Content = str
                    });
                }
                //comboxField.SelectionChanged += comboxField_SelectionChanged;
                spContent.Children.Clear();
            }
        }
        public async Task <HttpResponseMessage> Post()
        {
            bool indexExist = EsIndex.IndexCheck("product_search");

            if (!indexExist)
            {
                bool indexCreated = EsIndex.CreateIndex("product_search");
                indexExist = indexCreated == true ? indexExist = true
                    : throw new ArgumentException("Index could not be created");
            }
            //StringResponse deleteResponse1 = client.Client.Delete<StringResponse>("product_search", "85f3cnIB0jvdFYl1x5dB");

            Task <List <ProductModel> >          databaseProducts = Task.Run(() => ProductDatabase.GetAllWebProducts());
            Task <ConcurrentBag <ProductModel> > esIndexProducts  = Task.Run(() => EsProductIndex.GetEsSearchableProducts());
            List <ProductModel> updateProducts = new List <ProductModel>();

            if (esIndexProducts != null)
            {
                esIndexProducts.Wait();
                HashSet <string> diffIdentifiers = new HashSet <string>(esIndexProducts.Result.Select(s => s.Url));
                databaseProducts.Wait();
                updateProducts = databaseProducts.Result.Where(m => !diffIdentifiers.Contains(m.Url)).ToList();
            }
            else
            {
                updateProducts = databaseProducts.Result;
            }

            PostResponse postResponse = new PostResponse
            {
                DatabaseItemCount = databaseProducts.Result.Count,
                EsItemCount       = esIndexProducts.Result.Count,
                NewItemCount      = updateProducts.Count,
                ProductModels     = new ConcurrentBag <ProductModel>(updateProducts)
            };

            Byte[] jsonString = JsonSerializer.SerializeToUtf8Bytes(postResponse, postResponse.SerializerOptions);
            await this.Response.Body.WriteAsync(jsonString);

            try
            {
                if (this.Response.StatusCode == 200)
                {
                    Debug.WriteLine("\r\nResponse Status Code is OK and StatusDescription is: {0}",
                                    this.Response.StatusCode);
                }
            }
            catch (WebException e)
            {
                Debug.WriteLine("\r\nWebException Raised. The following error occurred : {0}", e.Status);
            }
            catch (Exception e)
            {
                Debug.WriteLine("\nThe following Exception was raised : {0}", e.Message);
            }

            ConcurrentBag <object> bulkUpdateObjectList = new ConcurrentBag <object>();

            Parallel.ForEach(postResponse.ProductModels, record =>
            {
                bulkUpdateObjectList.Add(new { index = new { _index = "product_search", _type = "doc" } });
                bulkUpdateObjectList.Add(new
                {
                    type        = record.Type,
                    sku         = record.Sku,
                    image       = record.Image,
                    url         = record.Url,
                    price       = record.Price,
                    name        = record.Name,
                    description = record.Description
                });
            });

            Task <StringResponse> esResponse = Task.Run(() =>
                                                        client.Client.BulkAsync <StringResponse>(PostData.MultiJson(bulkUpdateObjectList)));

            esResponse.Wait();

            return(new HttpResponseMessage(HttpStatusCode.Accepted));
        }