public async Task ExecuteAsync(IndexCommand[] commands, CancellationToken ct = default) { var args = new List <object>(); foreach (var command in commands) { CommandFactory.CreateCommands(command, args, indexName); } if (args.Count == 0) { return; } var result = await client.BulkAsync <StringResponse>(PostData.MultiJson(args), ctx : ct); if (!result.Success) { throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); } }
public async Task <bool> Send(IReadOnlyList <string> entries) { StringBuilder content = new(entries.Count * (200 + 75)); foreach (string entry in entries) { content.Append($"{{\"index\":{{}}"); content.Append("\n"); content.Append(entry); content.Append("\n"); } string body = content.ToString(); StringResponse result = await _client.BulkAsync <StringResponse>(_index, PostData.String(body)); if (result.HttpStatusCode is >= 200 and < 300) { return(true); } return(false); }
public async Task ExecuteAsync(params IndexCommand[] commands) { var args = new List <object>(); foreach (var command in commands) { switch (command) { case UpsertIndexEntry upsert: Upsert(upsert, args); break; case UpdateIndexEntry update: Update(update, args); break; case DeleteIndexEntry delete: Delete(delete, args); break; } } if (args.Count > 0) { var result = await client.BulkAsync <StringResponse>(PostData.MultiJson(args)); if (!result.Success) { throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); } } if (waitForTesting) { await Task.Delay(1000); } }
public async Task ExecuteAsync(params IndexCommand[] commands) { var args = new List <object>(); foreach (var command in commands) { CommandFactory.CreateCommands(command, args, indexName); } if (args.Count > 0) { var result = await client.BulkAsync <StringResponse>(PostData.MultiJson(args)); if (!result.Success) { throw new InvalidOperationException($"Failed with ${result.Body}", result.OriginalException); } } if (waitForTesting) { await Task.Delay(1000); } }
public async Task BulkIndexAsync(List <T> items) { var data = new List <object>(); items.ForEach(item => { data.Add(new { index = new { _index = index, _type = "_doc" } }); data.Add(new { name = item }); }); var indexResponse = await lowlevelClient.BulkAsync <StringResponse>(PostData.MultiJson(data)); var response = indexResponse.Body; }
public static async Task Expert(ElasticLowLevelClient lowlevelClient, string fileName, string province) { var file = new FileInfo(fileName); ExcelPackage.LicenseContext = LicenseContext.Commercial; using (var package = new ExcelPackage(file)) { var worksheet = package.Workbook.Worksheets[0]; var rowCount = worksheet.Dimension.Rows; var colCount = worksheet.Dimension.Columns; int cout = 0; List <object> list = new(); for (int row = 2; row <= rowCount; row++) { var model = new Person(); for (int col = 1; col <= colCount; col++) { switch (col) { case 2: model.Level = worksheet.Cells[row, col].Value?.ToString(); break; case 3: model.IdNumber = worksheet.Cells[row, col].Value?.ToString(); break; case 4: model.Gender = worksheet.Cells[row, col].Value?.ToString(); break; case 5: model.City = worksheet.Cells[row, col].Value?.ToString(); break; case 6: model.School = worksheet.Cells[row, col].Value?.ToString(); break; case 7: { var str = worksheet.Cells[row, col].Value?.ToString(); model.Score = Convert.ToDouble(str != "缺考" ? str : "-11"); break; } case 8: model.IsPassed = worksheet.Cells[row, col].Value?.ToString() == "是"; break; } } model.Province = province; // await lowlevelClient.IndexAsync<BytesResponse>("csp", PostData.Serializable(model)); list.Add(new { index = new { _index = "csp", _type = "person", _id = Guid.NewGuid() } }); list.Add(model); cout++; } dynamic result = await lowlevelClient.BulkAsync <StringResponse>(PostData.MultiJson(list)); Console.WriteLine($"处理了{province} {cout}条记录\n {result}"); } }
public static void InsertIntoES(string csv_filename, ElasticLowLevelClient lowLevelClient, bool isAsync, int bulkLimit, int shardCount, int replicaCount) { var idelResponse = lowLevelClient.IndicesDelete <BytesResponse>("people"); var icrResponse = lowLevelClient.IndicesCreate <BytesResponse>("people", PostData.Serializable( new { settings = new { number_of_shards = shardCount, number_of_replicas = replicaCount }, mappings = new { _doc = new { properties = new { FirstName = new { type = "text" }, LastName = new { type = "text" }, UserName = new { type = "text" }, SSC_Grade = new { type = "text" }, HSC_Grade = new { type = "text" }, Bachelor_Grade = new { type = "text" }, Age = new { type = "integer" }, Gender = new { type = "text" }, Email = new { type = "text" }, DateOfBirth = new { type = "date" }, Streent = new { type = "text" }, Suite = new { type = "text" }, City = new { type = "text" }, ZipCode = new { type = "text" }, } } } } )); var people = new List <object> { new { index = new { _index = "people", _type = "_doc" } } }; int counter = 0; int id = 0; using (StreamReader reader = new StreamReader(csv_filename)) { reader.ReadLine(); while (true) { string line = reader.ReadLine(); if (line == null) { break; } var values = line.Split(","); if (id == 0) { people = new List <object> { }; } if (counter >= bulkLimit) { if (isAsync) { lowLevelClient.BulkAsync <StringResponse>(PostData.MultiJson(people)); } else { lowLevelClient.Bulk <StringResponse>(PostData.MultiJson(people)); } counter = 0; people = new List <object> { }; } else { people.Add(new { index = new { _index = "people", _type = "_doc", _id = $"{id}" } }); people.Add( new { FirstName = values[0], LastName = values[1], UserName = values[2], SSC_Grade = values[3], HSC_Grade = values[4], Bachelor_Grade = values[5], Age = Int32.Parse(values[6]), Gender = values[7], Email = values[8], DateOfBirth = DateTime.Parse(values[9]), Street = values[10], Suite = values[11], City = values[12], ZipCode = values[13], }); counter++; id++; } Console.WriteLine(id); } if (isAsync) { lowLevelClient.BulkAsync <StringResponse>(PostData.MultiJson(people)); } else { lowLevelClient.Bulk <StringResponse>(PostData.MultiJson(people)); } } }