示例#1
0
        public static async Task <ConcurrentBag <ProductModel> > GetEsSearchableProducts()
        {
            EsClient       client         = new EsClient();
            StringResponse searchResponse = new StringResponse();

            try
            {
                searchResponse = await client.Client.SearchAsync <StringResponse>("product_search", PostData.Serializable(
                                                                                      new
                {
                    from = 0,
                    size = 10000,

                    query = new
                    {
                        match = new
                        {
                            type = "product"
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
                Debug.WriteLine(ex.InnerException.ToString());
            }

            string responseJson = searchResponse.Body;

            using JsonDocument document = JsonDocument.Parse(responseJson);
            JsonElement root          = document.RootElement;
            JsonElement sourceElement = root.GetProperty("hits").GetProperty("hits");

            ConcurrentBag <ProductModel> products = new ConcurrentBag <ProductModel>();

            Parallel.ForEach(sourceElement.EnumerateArray().AsParallel(), source =>
            {
                if (source.TryGetProperty("_source", out JsonElement rec))
                {
                    ProductModel product = new ProductModel
                    {
                        Type        = rec.GetProperty("type").GetString(),
                        Sku         = rec.GetProperty("sku").GetString(),
                        Image       = rec.GetProperty("image").GetString(),
                        Url         = rec.GetProperty("url").GetString(),
                        Price       = rec.GetProperty("price").GetDecimal(),
                        Name        = rec.GetProperty("name").GetString(),
                        Description = rec.GetProperty("description").GetString()
                    };

                    products.Add(product);
                }
            });
            return(products);
        }
示例#2
0
        public static bool IndexCheck(string index)
        {
            EsClient client = new EsClient();
            bool     success;

            try
            {
                success = client.Client.Indices.Exists <StringResponse>(index).Success;
            }
            catch { success = false; }

            return(success);
        }
示例#3
0
        public static bool CreateIndex(string index)
        {
            EsClient client = new EsClient();

            client.Client.Indices.Create <StringResponse>(index, PostData.Serializable(new { }));
            bool success;

            try
            {
                success = client.Client.Indices.Create <StringResponse>(index, PostData.Serializable(new { })).Success;
            }
            catch { success = false; }

            return(success);
        }