Skip to content

Commit 2733247

Browse files
committed
adapt to index, doc[]
1 parent 7e26340 commit 2733247

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

Source/Ecommerce.Application/Services/Workers/Elastic/ElasticTaskNotificationHandler.cs

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Reflection.Metadata;
2+
using Ecommerce.Application.Common.Defaults;
23
using Ecommerce.Application.Common.Interfaces.Providers.Search.Elastic;
34
using Ecommerce.Application.Common.Models.Search.Elastic.Documents;
45
using MediatR;
@@ -17,37 +18,46 @@ public async Task Handle(
1718
CancellationToken cancellationToken
1819
)
1920
{
21+
if (!await elasticSearch.IsReachableAsync())
22+
return;
23+
2024
taskQueue.QueueBackgroundWorkItem(async x =>
2125
{
2226
try
2327
{
2428
if (notification.ElasticAction == ElasticAction.Index)
2529
{
2630
logger.LogInformation("Indexing documents to ElasticSearch.");
27-
List<ProductDocument> failed = [];
31+
List<ElasticDocumentBase> failed = [];
2832
foreach (var kvp in notification.IndexDocs)
2933
{
3034
var indexName = kvp.Key;
31-
var document = (ProductDocument)kvp.Value;
32-
var success = await elasticSearch.IndexAsync(indexName, document, cancellationToken);
33-
if (!success)
34-
{
35-
failed.Add(document);
36-
logger.LogError(
37-
"Failed to index document {DocumentId} in index {IndexName}.",
38-
document.Id,
39-
indexName
40-
);
41-
}
35+
36+
if (indexName == ElasticIndices.ProductIndex)
37+
foreach (var doc in kvp.Value)
38+
{
39+
var document = (ProductDocument)doc;
40+
var success = await elasticSearch.IndexAsync(
41+
indexName,
42+
document,
43+
cancellationToken
44+
);
45+
if (!success)
46+
{
47+
failed.Add(document);
48+
logger.LogError(
49+
"Failed to index document \nDocumentId: {DocumentId} \nProductName: {ProductName}\n in index {IndexName}.",
50+
document.Id,
51+
document.Name,
52+
indexName
53+
);
54+
}
55+
}
4256
}
4357

4458
if (failed.Count > 0)
4559
{
46-
logger.LogError(
47-
"Failed to index {Count} documents in ElasticSearch. \n{@DocumentNames}",
48-
failed.Count,
49-
string.Join(",\n", failed.Select(x => x.Name))
50-
);
60+
logger.LogError("Failed to index {Count} documents in ElasticSearch.", failed.Count);
5161
}
5262
else
5363
{

Source/Ecommerce.Application/UseCases/Products/Commands/CreateProduct/CreateProductCommandHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ await _publisher.Publish(
176176
new ElasticTaskNotification()
177177
{
178178
ElasticAction = ElasticAction.Index,
179-
IndexDocs = new Dictionary<string, ElasticDocumentBase>
179+
IndexDocs = new Dictionary<string, ElasticDocumentBase[]>
180180
{
181-
{ ElasticIndices.ProductIndex, _mapper.Map<ProductDocument>(product) },
181+
{ ElasticIndices.ProductIndex, [_mapper.Map<ProductDocument>(product)] },
182182
},
183183
}
184184
);

Source/Ecommerce.Application/UseCases/Products/Queries/GetFilteredProducts/GetFilteredProductsQueryHandler.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,20 @@ CancellationToken cancellationToken
7575
_logger.LogInformation("No products found in ElasticSearch, falling back to DB search.");
7676
GetProductsResult products = await _productRepository.GetFilteredProductsAsync(request);
7777

78-
// Index the products in ElasticSearch for future searches
78+
// Schedule an indexing job for the non-indexed products for future searches
7979
await _publisher.Publish(
8080
new ElasticTaskNotification()
8181
{
8282
ElasticAction = ElasticAction.Index,
83-
IndexDocs = products.Items.ToDictionary(
84-
k => ElasticIndices.ProductIndex,
85-
v => (ElasticDocumentBase)_mapper.Map<ProductDocument>(v)
86-
),
83+
IndexDocs = new Dictionary<string, ElasticDocumentBase[]>
84+
{
85+
{
86+
ElasticIndices.ProductIndex,
87+
products
88+
.Items.Select(v => (ElasticDocumentBase)_mapper.Map<ProductDocument>(v))
89+
.ToArray()
90+
},
91+
},
8792
}
8893
);
8994

0 commit comments

Comments
 (0)