-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchClient.cs
280 lines (253 loc) · 8.1 KB
/
SearchClient.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Nest;
namespace ElasticSearchConsoleApp
{
public class SearchClient<T> : ISearchClient<T> where T : class
{
private static ElasticClient elastic;
private static string defaultIndexName = "myblogindex"; //ConfigurationManager.AppSettings["ElasticDefaultIndexName"];
private static string endPointUrl = "http://localhost:9200"; //ConfigurationManager.AppSettings["ElasticEndPointUrl"];
public SearchClient()
{
if (elastic == null)
OpenConnection();
}
// open elastic connection
private void OpenConnection()
{
var url = new Uri(endPointUrl);
var settings = new ConnectionSettings(url).DefaultIndex(defaultIndexName);
elastic = new ElasticClient(settings);
}
// add new record into elastic
public bool Add(T item)
{
try
{
return elastic.Index(item).Created;
}
catch (Exception ex)
{
throw ex;
}
}
// add new record into elastic in an asynchronous manner
public async Task<bool> AddAsync(T item)
{
try
{
var response = await elastic.IndexAsync(item);
return response.Created;
}
catch (Exception ex)
{
throw ex;
}
}
// add multiple records into elastic
public long Add(List<T> items)
{
try
{
return elastic.IndexMany(items).Took;
}
catch (Exception ex)
{
throw ex;
}
}
// add multiple records into elastic in an asynchronous manner
public async Task<int> AddAsync(List<T> items)
{
try
{
var response = await elastic.IndexManyAsync(items);
return response.Items.Count;
}
catch (Exception ex)
{
throw ex;
}
}
// get record from elastic using id
public T Get(string id)
{
try
{
return elastic.Get<T>(id).Source;
}
catch (Exception ex)
{
throw ex;
}
}
// get record from elastic using id in an asynchronous manner
public async Task<T> GetAsync(string id)
{
try
{
var response = await elastic.GetAsync<T>(id);
return response.Source;
}
catch (Exception ex)
{
throw ex;
}
}
// update record into elastic
public bool Update(T item)
{
try
{
var response = elastic.Update(new DocumentPath<T>(item), u => u.Doc(item));
if (response.Version > 0)
return true;
else
return false;
}
catch (Exception ex)
{
throw ex;
}
}
// update record into elastic in an asynchronous manner
public async Task<bool> UpdateAsync(T item)
{
try
{
var response = await elastic.UpdateAsync(new DocumentPath<T>(item), u => u.Doc(item));
if (response.Version > 0)
return true;
else
return false;
}
catch (Exception ex)
{
throw ex;
}
}
// delete record from elastic
public bool Delete(string id)
{
try
{
return elastic.Delete(new DeleteRequest<T>(id)).Found;
}
catch (Exception ex)
{
throw ex;
}
}
// delete record from elastic in an asynchronous manner
public async Task<bool> DeleteAsync(string id)
{
try
{
var response = await elastic.DeleteAsync(new DeleteRequest<T>(id));
return response.Found;
}
catch (Exception ex)
{
throw ex;
}
}
// delete multiple records from elastic
public int Delete(List<T> items)
{
try
{
return elastic.DeleteMany(items).Items.Count;
}
catch (Exception ex)
{
throw ex;
}
}
// delete multiple records from elastic in an asynchronous manner
public async Task<int> DeleteAsync(List<T> items)
{
try
{
var response = await elastic.DeleteManyAsync(items);
return response.Items.Count;
}
catch (Exception ex)
{
throw ex;
}
}
// search records for mentioned field from elastic
public List<T> Search(string searchText, int from, int size, Expression<Func<T, object>> fieldToSearchFrom)
{
try
{
var response = elastic.Search<T>(s => s
.From(from - 1) // default index = 0
.Size(size)
.Query(q => q.Match(mq => mq.Field(fieldToSearchFrom).Query(searchText.ToLower())))
);
return response.Documents.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
// search records for mentioned field from elastic in an asynchronous manner
public async Task<List<T>> SearchAsync(string searchText, int from, int size, Expression<Func<T, object>> fieldToSearchFrom)
{
try
{
var response = await elastic.SearchAsync<T>(s => s
.From(from - 1) // default index = 0
.Size(size)
.Query(q => q.Match(mq => mq.Field(fieldToSearchFrom).Query(searchText.ToLower())))
);
return response.Documents.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
// search records for any field from elastic
public List<T> Search(string searchText, int from, int size)
{
try
{
var response = elastic.Search<T>(s => s
.From(from - 1) // default index = 0
.Take(size)
.Query(qry => qry.Bool(b => b.Must(m => m.QueryString(qs => qs.DefaultField("_all").Query(searchText)))))
);
return response.Documents.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
// search records for any field from elastic in an asynchronous manner
public async Task<List<T>> SearchAsync(string searchText, int from, int size)
{
try
{
var response = await elastic.SearchAsync<T>(s => s
.From(from - 1) // default index = 0
.Take(size)
.Query(qry => qry.Bool(b => b.Must(m => m.QueryString(qs => qs.DefaultField("_all").Query(searchText)))))
);
return response.Documents.ToList();
}
catch (Exception ex)
{
throw ex;
}
}
}
}