-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductElasticService.cs
151 lines (143 loc) · 5.87 KB
/
ProductElasticService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using Bogus;
using Elastic.Clients.Elasticsearch;
using Elastic.Clients.Elasticsearch.QueryDsl;
using Elasticsearch.WebAPI.Dtos;
using Elasticsearch.WebAPI.Models;
namespace Elasticsearch.WebAPI.Services
{
public class ProductElasticService : IProductElasticService
{
private readonly ElasticsearchClient _client;
public ProductElasticService()
{
var settings = new ElasticsearchClientSettings(new Uri("http://localhost:9200"))
.DefaultIndex("products");
_client = new ElasticsearchClient(settings);
}
public async Task<CreateResponse> Create(CreateProductDto createProductDto, CancellationToken cancellationToken)
{
Product product = new Product()
{
Name = createProductDto.Name,
Price = createProductDto.Price,
Stock = createProductDto.Stock,
Description = createProductDto.Description,
};
CreateRequest<Product> createRequest = new CreateRequest<Product>(product.Id.ToString())
{
Document = product
};
CreateResponse createResponse = await _client.CreateAsync(createRequest, cancellationToken);
return createResponse;
}
public async Task<DeleteResponse> DeleteById(Guid id, CancellationToken cancellationToken)
{
DeleteResponse deleteResponse = await _client.DeleteAsync("products", id, cancellationToken);
return deleteResponse;
}
public async Task<SearchResponse<Product>> GetAll(CancellationToken cancellationToken)
{
SearchRequest searchRequest = new SearchRequest("products")
{
Size = 1000,
Sort = new List<SortOptions>
{
SortOptions.Field(new Field("name.keyword"), new FieldSort()
{
Order=SortOrder.Desc,
})
},
};
SearchResponse<Product> searchResponse = await _client.SearchAsync<Product>(searchRequest, cancellationToken);
return searchResponse;
}
public async Task<SearchResponse<Product>> GetByNameFuzzy(string value, CancellationToken cancellationToken)
{
SearchRequest searchRequest = new SearchRequest("products")
{
Size = 1000,
Sort = new List<SortOptions>
{
SortOptions.Field(new Field("name.keyword"), new FieldSort()
{
Order=SortOrder.Desc,
})
},
Query = new FuzzyQuery(new Field("name"))
{
Value = value
}
};
SearchResponse<Product> searchResponse = await _client.SearchAsync<Product>(searchRequest, cancellationToken);
return searchResponse;
}
public async Task<SearchResponse<Product>> GetByNameMatch(string query, CancellationToken cancellationToken)
{
SearchRequest searchRequest = new SearchRequest("products")
{
Size = 1000,
Sort = new List<SortOptions>
{
SortOptions.Field(new Field("name.keyword"), new FieldSort()
{
Order=SortOrder.Desc,
})
},
Query = new MatchQuery(new Field("name"))
{
Query = query
}
};
SearchResponse<Product> searchResponse = await _client.SearchAsync<Product>(searchRequest, cancellationToken);
return searchResponse;
}
public async Task<SearchResponse<Product>> GetByNameWildcard(string value, CancellationToken cancellationToken)
{
SearchRequest searchRequest = new SearchRequest("products")
{
Size = 1000,
Sort = new List<SortOptions>
{
SortOptions.Field(new Field("name.keyword"), new FieldSort()
{
Order=SortOrder.Desc,
})
},
Query = new WildcardQuery(new Field("name"))
{
Value = $"*{value}*"
}
};
SearchResponse<Product> searchResponse = await _client.SearchAsync<Product>(searchRequest, cancellationToken);
return searchResponse;
}
public async Task SeedData(CancellationToken cancellationToken)
{
for (int i = 0; i < 100; i++)
{
Faker faker = new Faker();
Product product = new Product()
{
Name = faker.Commerce.ProductName(),
Price = Convert.ToDecimal(faker.Commerce.Price()),
Stock = faker.Commerce.Random.Int(1, 20),
Description = faker.Commerce.ProductDescription(),
};
CreateRequest<Product> createRequest = new CreateRequest<Product>(product.Id.ToString())
{
Document = product
};
await _client.CreateAsync(createRequest, cancellationToken);
}
}
public async Task<UpdateResponse<Product>> Update(UpdateProductDto updateProductDto, CancellationToken cancellationToken)
{
UpdateRequest<Product, UpdateProductDto> updateRequest = new UpdateRequest<Product, UpdateProductDto>("products", updateProductDto.Id.ToString())
{
Doc = updateProductDto
};
UpdateResponse<Product> updateResponse = await _client.UpdateAsync(updateRequest, cancellationToken);
return updateResponse;
}
}
}