|
| 1 | +using System.Text.Json; |
| 2 | +using Ecommerce.Application.Common.Interfaces.Providers.Search.Elastic; |
| 3 | +using Ecommerce.Application.Common.Models.Search.Elastic; |
| 4 | +using Elastic.Clients.Elasticsearch; |
| 5 | +using Elastic.Clients.Elasticsearch.QueryDsl; |
| 6 | + |
| 7 | +public class ElasticSearch<TDocument>(ElasticsearchClient elasticsearch) : IElasticSearch<TDocument> |
| 8 | + where TDocument : class |
| 9 | +{ |
| 10 | + public async Task<bool> DeleteAsync( |
| 11 | + string indexName, |
| 12 | + string id, |
| 13 | + CancellationToken cancellationToken = default |
| 14 | + ) |
| 15 | + { |
| 16 | + var dr = await elasticsearch.DeleteAsync( |
| 17 | + new DeleteRequest() { Index = indexName, Id = id }, |
| 18 | + cancellationToken |
| 19 | + ); |
| 20 | + return dr.IsSuccess(); |
| 21 | + } |
| 22 | + |
| 23 | + public async Task<TDocument?> GetAsync( |
| 24 | + string indexName, |
| 25 | + string id, |
| 26 | + CancellationToken cancellationToken = default |
| 27 | + ) |
| 28 | + { |
| 29 | + var gr = await elasticsearch.GetAsync<TDocument>( |
| 30 | + new GetRequest() { Index = indexName, Id = id }, |
| 31 | + cancellationToken |
| 32 | + ); |
| 33 | + |
| 34 | + // Check if the request was successful AND if the document was found |
| 35 | + if (!gr.IsSuccess()) |
| 36 | + { |
| 37 | + // Log the error from gr.DebugInformation or gr.ElasticsearchServerError |
| 38 | + Console.WriteLine( |
| 39 | + $"Elasticsearch Get failed: {gr.DebugInformation ?? gr.ElasticsearchServerError?.Error.ToString()}" |
| 40 | + ); |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + if (!gr.Found) |
| 45 | + { |
| 46 | + // Document not found, this is not an error, just means it doesn't exist |
| 47 | + Console.WriteLine($"Document with ID {id} not found in index {indexName}."); |
| 48 | + return null; |
| 49 | + } |
| 50 | + |
| 51 | + return gr.Source; |
| 52 | + } |
| 53 | + |
| 54 | + public async Task<bool> IndexAsync( |
| 55 | + string indexName, |
| 56 | + TDocument document, |
| 57 | + CancellationToken cancellationToken = default |
| 58 | + ) |
| 59 | + { |
| 60 | + var ind = await elasticsearch.IndexAsync( |
| 61 | + new IndexRequest<TDocument>() { Document = document, Index = indexName } |
| 62 | + ); |
| 63 | + return ind.IsSuccess(); |
| 64 | + } |
| 65 | + |
| 66 | + public async Task<IEnumerable<TDocument>> SearchAsync( |
| 67 | + ElasticSearchFilterParams filterParams, |
| 68 | + CancellationToken cancellationToken = default |
| 69 | + ) |
| 70 | + { |
| 71 | + var sr = await elasticsearch.SearchAsync<TDocument>( |
| 72 | + new SearchRequest(filterParams.Index) { Query = new Query() { } }, |
| 73 | + cancellationToken |
| 74 | + ); |
| 75 | + if (sr != null && sr.IsSuccess()) |
| 76 | + return sr.Documents; |
| 77 | + else |
| 78 | + return Enumerable.Empty<TDocument>(); |
| 79 | + } |
| 80 | + |
| 81 | + public async Task<bool> UpdateAsync( |
| 82 | + string indexName, |
| 83 | + string id, |
| 84 | + TDocument document, |
| 85 | + CancellationToken cancellationToken = default |
| 86 | + ) |
| 87 | + { |
| 88 | + var ur = await elasticsearch.UpdateAsync<TDocument, TDocument>( |
| 89 | + new UpdateRequest<TDocument, TDocument>() |
| 90 | + { |
| 91 | + Index = indexName, |
| 92 | + Id = id, |
| 93 | + Doc = document, |
| 94 | + } |
| 95 | + ); |
| 96 | + return ur.IsSuccess(); |
| 97 | + } |
| 98 | +} |
0 commit comments