public async Task <SonicResult> UpdateAsync(JObject product, string language, string state) { var id = GetId(product); if (id == null) { return(SonicResult.Failed(SonicErrorDescriber .StoreFailure("Product has no id"))); } var type = GetType(product); if (type == null) { return(SonicResult.Failed(SonicErrorDescriber .StoreFailure($"Product {id} has no type"))); } var json = JsonConvert.SerializeObject(product); var client = Configuration.GetElasticClient(language, state); try { await client.UpdateAsync(id, type, json); return(SonicResult.Success); } catch (Exception e) { return(SonicResult.Failed(SonicErrorDescriber.StoreFailure(e.Message, e))); } }
public async Task <SonicResult> ResetAsync(string language, string state) { var client = Configuration.GetElasticClient(language, state); try { if (await client.IndexExistsAsync()) { await client.DeleteIndexAsync(); } await client.CreateIndexAsync(GetDefaultIndexSettings().ToString()); return(SonicResult.Success); } catch (Exception ex) { return(SonicResult.Failed(SonicErrorDescriber.StoreFailure(ex.Message, ex))); } }
public async Task <SonicResult> DeleteAsync(JObject product, string language, string state) { var id = GetId(product); if (id == null) { return(SonicResult.Failed(SonicErrorDescriber .StoreFailure("Product has no id"))); } var client = Configuration.GetElasticClient(language, state); try { var type = GetType(product); await client.DeleteAsync(id, type); return(SonicResult.Success); } catch (Exception e) { return(SonicResult.Failed(SonicErrorDescriber.StoreFailure(e.Message, e))); } }
public async Task <SonicResult> BulkCreateAsync(IEnumerable <JObject> products, string language, string state) { if (products == null) { throw new ArgumentNullException(nameof(products)); } var failedResult = new List <SonicError>(); var client = Configuration.GetElasticClient(language, state); var index = Configuration.GetElasticIndex(language, state); var commands = products.Select(p => { var id = GetId(p); if (id == null) { failedResult.Add(SonicErrorDescriber.StoreFailure("Product has no id")); return(string.Empty); } var type = GetType(p); if (type == null) { failedResult.Add(SonicErrorDescriber.StoreFailure($"Product {id} has no type")); return(string.Empty); } var json = JsonConvert.SerializeObject(p, DateTimeConverter); return($"{{\"index\":{{\"_index\":\"{index.Name}\",\"_type\":\"{type}\",\"_id\":\"{id}\"}}}}\n{json}\n"); }); if (failedResult.Any()) { return(SonicResult.Failed(failedResult.ToArray())); } try { var responseText = await client.BulkAsync(string.Join(string.Empty, commands)); var responseJson = JObject.Parse(responseText); if (responseJson.Value <bool>("errors")) { var errors = responseJson["items"] .Select(i => i["index"]?["error"]) .Where(i => i != null) .Select(i => new { Code = i.Value <string>("type"), Description = i.Value <string>("reason") }) .Distinct() .Select(i => new SonicError { Code = i.Code, Description = i.Description, Exception = new Exception($"{i.Code} : {i.Description}") }) .ToArray(); return(SonicResult.Failed(errors)); } return(SonicResult.Success); } catch (Exception ex) { return(SonicResult.Failed(SonicErrorDescriber.StoreFailure(ex.Message, ex))); } }