/
ElasticSearchGateway.cs
945 lines (843 loc) · 37.6 KB
/
ElasticSearchGateway.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
using Elasticsearch.Net;
using IsraelHiking.Common;
using IsraelHiking.Common.Configuration;
using IsraelHiking.Common.Extensions;
using IsraelHiking.DataAccessInterfaces;
using IsraelHiking.DataAccessInterfaces.Repositories;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Nest;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using NetTopologySuite.IO.Converters;
public class DynamicDictionaryConverter : JsonConverter<DynamicDictionary>
{
public override DynamicDictionary Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartArray)
{
var array = JsonSerializer.Deserialize<object[]>(ref reader, options);
var arrayDict = new Dictionary<string, object>();
for (var i = 0; i < array.Length; i++)
arrayDict[i.ToString(CultureInfo.InvariantCulture)] = new DynamicValue(array[i]);
return DynamicDictionary.Create(arrayDict);
}
if (reader.TokenType != JsonTokenType.StartObject) throw new JsonException();
var dict = JsonSerializer.Deserialize<Dictionary<string, object>>(ref reader, options);
return DynamicDictionary.Create(dict);
}
public override void Write(Utf8JsonWriter writer, DynamicDictionary dictionary, JsonSerializerOptions options)
{
writer.WriteStartObject();
foreach (var kvp in dictionary.GetKeyValues())
{
if (kvp.Value == null) continue;
writer.WritePropertyName(kvp.Key);
JsonSerializer.Serialize(writer, kvp.Value?.Value, options);
}
writer.WriteEndObject();
}
}
internal class ExceptionConverter : JsonConverter<Exception>
{
private static List<Dictionary<string, object>> FlattenExceptions(Exception e)
{
var maxExceptions = 20;
var exceptions = new List<Dictionary<string, object>>(maxExceptions);
var depth = 0;
do
{
var o = ToDictionary(e, depth);
exceptions.Add(o);
depth++;
e = e.InnerException;
} while (depth < maxExceptions && e != null);
return exceptions;
}
private static Dictionary<string, object> ToDictionary(Exception e, int depth)
{
var o = new Dictionary<string, object>(10);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
var sc = new StreamingContext();
e.GetObjectData(si, sc);
var helpUrl = si.GetString("HelpURL");
var stackTrace = si.GetString("StackTraceString");
var remoteStackTrace = si.GetString("RemoteStackTraceString");
var remoteStackIndex = si.GetInt32("RemoteStackIndex");
var exceptionMethod = si.GetString("ExceptionMethod");
var hresult = si.GetInt32("HResult");
var source = si.GetString("Source");
var className = si.GetString("ClassName");
o.Add("Depth", depth);
o.Add("ClassName", className);
o.Add("Message", e.Message);
o.Add("Source", source);
o.Add("StackTraceString", stackTrace);
o.Add("RemoteStackTraceString", remoteStackTrace);
o.Add("RemoteStackIndex", remoteStackIndex);
o.Add("HResult", hresult);
o.Add("HelpURL", helpUrl);
WriteStructuredExceptionMethod(o, exceptionMethod);
return o;
}
private static void WriteStructuredExceptionMethod(Dictionary<string,object> o, string exceptionMethodString)
{
if (string.IsNullOrWhiteSpace(exceptionMethodString)) return;
var args = exceptionMethodString.Split('\0', '\n');
if (args.Length != 5) return;
var memberType = int.Parse(args[0], CultureInfo.InvariantCulture);
var name = args[1];
var assemblyName = args[2];
var className = args[3];
var signature = args[4];
var an = new AssemblyName(assemblyName);
var exceptionMethod = new Dictionary<string, object>(7)
{
{ "Name", name },
{ "AssemblyName", an.Name },
{ "AssemblyVersion", an.Version.ToString() },
{ "AssemblyCulture", an.CultureName },
{ "ClassName", className },
{ "Signature", signature },
{ "MemberType", memberType }
};
o.Add("ExceptionMethod", exceptionMethod);
}
public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
throw new NotSupportedException();
public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
{
if (value == null)
{
writer.WriteNullValue();
return;
}
var flattenedExceptions = FlattenExceptions(value);
writer.WriteStartArray();
for (var i = 0; i < flattenedExceptions.Count; i++)
{
var flattenedException = flattenedExceptions[i];
writer.WriteStartObject();
foreach (var kv in flattenedException)
{
writer.WritePropertyName(kv.Key);
JsonSerializer.Serialize(writer, kv.Value, options);
}
writer.WriteEndObject();
}
writer.WriteEndArray();
}
}
public class SystemTextJsonSerializer : IElasticsearchSerializer
{
private readonly Lazy<JsonSerializerOptions> _indented;
private readonly Lazy<JsonSerializerOptions> _none;
private IList<JsonConverter> BakedInConverters { get; } = new List<JsonConverter>
{
{new ExceptionConverter()},
{new DynamicDictionaryConverter()},
{new DateTimeConverter()}
};
public SystemTextJsonSerializer(JsonConverterFactory factory)
{
var indentedOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = true
};
indentedOptions.Converters.Add(factory);
foreach (var converter in BakedInConverters)
indentedOptions.Converters.Add(converter);
var noneIndentedOptions = new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false
};
noneIndentedOptions.Converters.Add(factory);
foreach (var converter in BakedInConverters)
noneIndentedOptions.Converters.Add(converter);
_indented = new Lazy<JsonSerializerOptions>(() => indentedOptions);
_none = new Lazy<JsonSerializerOptions>(() => noneIndentedOptions);
}
private static bool TryReturnDefault<T>(Stream stream, out T deserialize)
{
deserialize = default;
return stream == null || stream == Stream.Null || (stream.CanSeek && stream.Length == 0);
}
private static MemoryStream ToMemoryStream(Stream stream)
{
if (stream is MemoryStream m) return m;
var length = stream.CanSeek ? stream.Length : (long?) null;
var wrapped = length.HasValue ? new MemoryStream(new byte[length.Value]) : new MemoryStream();
stream.CopyTo(wrapped);
return wrapped;
}
private static ReadOnlySpan<byte> ToReadOnlySpan(Stream stream)
{
using var m = ToMemoryStream(stream);
if (m.TryGetBuffer(out var segment))
return segment;
var a = m.ToArray();
return new ReadOnlySpan<byte>(a).Slice(0, a.Length);
}
private JsonSerializerOptions GetFormatting(SerializationFormatting formatting) =>
formatting == SerializationFormatting.None ? _none.Value : _indented.Value;
public object Deserialize(Type type, Stream stream)
{
if (TryReturnDefault(stream, out object deserialize)) return deserialize;
var buffered = ToReadOnlySpan(stream);
return JsonSerializer.Deserialize(buffered, type, _none.Value);
}
public T Deserialize<T>(Stream stream)
{
if (TryReturnDefault(stream, out T deserialize)) return deserialize;
var buffered = ToReadOnlySpan(stream);
return JsonSerializer.Deserialize<T>(buffered, _none.Value);
}
public void Serialize<T>(T data, Stream stream, SerializationFormatting formatting = SerializationFormatting.None)
{
using var writer = new Utf8JsonWriter(stream);
if (data == null)
JsonSerializer.Serialize(writer, null, typeof(object), GetFormatting(formatting));
else
JsonSerializer.Serialize(writer, data, data.GetType(), GetFormatting(formatting));
}
public async Task SerializeAsync<T>(T data, Stream stream,
SerializationFormatting formatting = SerializationFormatting.None,
CancellationToken cancellationToken = default
)
{
if (data == null)
await JsonSerializer
.SerializeAsync(stream, null, typeof(object), GetFormatting(formatting), cancellationToken)
.ConfigureAwait(false);
else
await JsonSerializer
.SerializeAsync(stream, data, data.GetType(), GetFormatting(formatting), cancellationToken)
.ConfigureAwait(false);
}
public Task<object> DeserializeAsync(Type type, Stream stream, CancellationToken cancellationToken = default)
{
if (TryReturnDefault(stream, out object deserialize)) return Task.FromResult(deserialize);
return JsonSerializer.DeserializeAsync(stream, type, _none.Value, cancellationToken).AsTask();
}
public Task<T> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default)
{
if (TryReturnDefault(stream, out T deserialize)) return Task.FromResult(deserialize);
return JsonSerializer.DeserializeAsync<T>(stream, _none.Value, cancellationToken).AsTask();
}
}
namespace IsraelHiking.DataAccess
{
public class ElasticSearchGateway :
IInitializable,
IPointsOfInterestRepository,
IHighwaysRepository,
ISearchRepository,
IUserLayersRepository,
IImagesRepository,
IExternalSourcesRepository,
IShareUrlsRepository
{
private const int PAGE_SIZE = 10000;
private const string PROPERTIES = "properties";
private const string OSM_POIS_INDEX1 = "osm_names1";
private const string OSM_POIS_INDEX2 = "osm_names2";
private const string OSM_POIS_ALIAS = "osm_names";
private const string OSM_HIGHWAYS_INDEX1 = "osm_highways1";
private const string OSM_HIGHWAYS_INDEX2 = "osm_highways2";
private const string OSM_HIGHWAYS_ALIAS = "osm_highways";
private const string SHARES = "shares";
private const string CUSTOM_USER_LAYERS = "custom_user_layers";
private const string EXTERNAL_POIS = "external_pois";
private const string IMAGES = "images";
private const string REBUILD_LOG = "rebuild_log";
private const int NUMBER_OF_RESULTS = 10;
private readonly ILogger _logger;
private readonly ConfigurationData _options;
private IElasticClient _elasticClient;
public ElasticSearchGateway(IOptions<ConfigurationData> options, ILogger logger)
{
_options = options.Value;
_logger = logger;
}
public async Task Initialize()
{
var uri = _options.ElasticsearchServerAddress;
var pool = new SingleNodeConnectionPool(new Uri(uri));
var connectionString = new ConnectionSettings(
pool,
new HttpConnection(),
(b, c) => new SystemTextJsonSerializer(GeoJsonExtensions.GeoJsonWritableFactory))
.PrettyJson();
_elasticClient = new ElasticClient(connectionString);
await InitializeIndexWithAlias(OSM_POIS_INDEX1, OSM_POIS_INDEX2, OSM_POIS_ALIAS, CreatePointsOfInterestIndex);
await InitializeIndexWithAlias(OSM_HIGHWAYS_INDEX1, OSM_HIGHWAYS_INDEX2, OSM_HIGHWAYS_ALIAS, CreateHighwaysIndex);
if ((await _elasticClient.Indices.ExistsAsync(SHARES)).Exists == false)
{
await CreateSharesIndex();
}
if ((await _elasticClient.Indices.ExistsAsync(CUSTOM_USER_LAYERS)).Exists == false)
{
await _elasticClient.Indices.CreateAsync(CUSTOM_USER_LAYERS);
}
if ((await _elasticClient.Indices.ExistsAsync(IMAGES)).Exists == false)
{
await CreateImagesIndex();
}
_logger.LogInformation("Finished initialing elasticsearch with uri: " + uri);
}
private async Task InitializeIndexWithAlias(string index1, string index2, string alias, Func<string, Task> creatIndexAction)
{
if ((await _elasticClient.Indices.ExistsAsync(index1)).Exists == false &&
(await _elasticClient.Indices.ExistsAsync(index2)).Exists == false)
{
await creatIndexAction(index1);
await _elasticClient.Indices.BulkAliasAsync(a => a.Add(add => add.Alias(alias).Index(index1)));
_logger.LogInformation($"Initialing elasticsearch alias: {alias}, no indices existed");
return;
}
if ((await _elasticClient.Indices.ExistsAsync(index1)).Exists &&
(await _elasticClient.GetIndicesPointingToAliasAsync(alias)).Contains(index1))
{
return;
}
if ((await _elasticClient.Indices.ExistsAsync(index2)).Exists &&
(await _elasticClient.GetIndicesPointingToAliasAsync(alias)).Contains(index2))
{
return;
}
if ((await _elasticClient.Indices.ExistsAsync(index1)).Exists)
{
await _elasticClient.Indices.BulkAliasAsync(a => a.Add(add => add.Alias(alias).Index(index1)));
_logger.LogWarning($"Initialing elasticsearch alias: {alias}, for index {index1}, index existed without alias");
return;
}
if ((await _elasticClient.Indices.ExistsAsync(index2)).Exists)
{
await _elasticClient.Indices.BulkAliasAsync(a => a.Add(add => add.Alias(alias).Index(index2)));
_logger.LogWarning($"Initialing elasticsearch alias: {alias}, for index {index2}, index existed without alias");
}
}
private List<T> GetAllItemsByScrolling<T>(ISearchResponse<T> response) where T: class
{
var list = new List<T>();
list.AddRange(response.Documents.ToList());
var results = _elasticClient.Scroll<T>("10s", response.ScrollId);
list.AddRange(results.Documents.ToList());
while (results.Documents.Any())
{
results = _elasticClient.Scroll<T>("10s", results.ScrollId);
list.AddRange(results.Documents.ToList());
}
_elasticClient.ClearScroll(new ClearScrollRequest(response.ScrollId));
return list;
}
private QueryContainer FeatureNameSearchQueryWithFactor(QueryContainerDescriptor<IFeature> q, string searchTerm, string language)
{
return q.FunctionScore(
fs => fs.Query(
qi => FeatureNameSearchQuery(qi, searchTerm, language)
).Functions(fn => fn.FieldValueFactor(f => f.Field($"{PROPERTIES}.{FeatureAttributes.POI_SEARCH_FACTOR}")))
);
}
private QueryContainer FeatureNameSearchQuery(QueryContainerDescriptor<IFeature> q, string searchTerm, string language)
{
return q.DisMax(
dm => dm.Queries(
dmq => dmq.Match(
mm => mm.Query(searchTerm)
.Field($"{PROPERTIES}.{FeatureAttributes.POI_NAMES}.{Languages.ALL}")
.Fuzziness(Fuzziness.Auto)
),
dmq => dmq.Match(
m => m.Query(searchTerm)
.Boost(1.2)
.Field($"{PROPERTIES}.{FeatureAttributes.POI_NAMES}.{language}")
.Fuzziness(Fuzziness.Auto)
)
)
);
}
public async Task<List<IFeature>> Search(string searchTerm, string language)
{
if (string.IsNullOrWhiteSpace(searchTerm))
{
return new List<IFeature>();
}
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_POIS_ALIAS)
.Size(NUMBER_OF_RESULTS)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q => FeatureNameSearchQueryWithFactor(q, searchTerm, language))
);
return response.Documents.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
public async Task<List<IFeature>> SearchExact(string searchTerm, string language)
{
if (string.IsNullOrWhiteSpace(searchTerm))
{
return new List<IFeature>();
}
var response = await _elasticClient.SearchAsync<IFeature>(s =>
s.Index(OSM_POIS_ALIAS)
.Size(100)
.Query(q =>
q.MatchPhrase(mm =>
mm.Query(searchTerm)
.Field($"{PROPERTIES}.{FeatureAttributes.NAME}")
)
||
q.MatchPhrase(mm =>
mm.Query(searchTerm)
.Field($"{PROPERTIES}.{FeatureAttributes.NAME}:{language}")
)
||
q.MatchPhrase(mm =>
mm.Query(searchTerm)
.Field($"{PROPERTIES}.{FeatureAttributes.POI_NAMES}.{Languages.ALL}")
)
)
);
return response.Documents
.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED))
.Where(f => f.GetTitles().Contains(searchTerm))
.ToList();
}
public async Task<List<IFeature>> SearchPlaces(string place, string language)
{
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_POIS_ALIAS)
.Size(5)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(q => FeatureNameSearchQuery(q, place, language)
&& q.Term(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_CONTAINER}").Value(true))
)
);
return response.Documents.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
public async Task<List<IFeature>> SearchByLocation(Coordinate northEast, Coordinate southWest, string searchTerm, string language)
{
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_POIS_ALIAS)
.Size(NUMBER_OF_RESULTS)
.TrackScores()
.Sort(f => f.Descending("_score"))
.Query(
q => FeatureNameSearchQueryWithFactor(q, searchTerm, language) &&
q.GeoBoundingBox(b => ConvertToGeoBoundingBox(b, northEast, southWest))
)
);
return response.Documents.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
public async Task<List<IFeature>> GetContainers(Coordinate coordinate)
{
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_POIS_ALIAS)
.Size(100)
.Query(q =>
q.GeoShape(g =>
g.Shape(sh => sh.Point(ConvertCoordinate(coordinate)))
.Field(f => f.Geometry)
.Relation(GeoShapeRelation.Contains))
&& q.Term(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_CONTAINER}").Value(true)))
);
return response.Documents.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
private (string currentIndex, string newIndex) GetIndicesStatus(string index1, string index2, string alias)
{
var currentIndex = index1;
var newIndex = index2;
if (_elasticClient.GetIndicesPointingToAlias(alias).Contains(index2))
{
currentIndex = index2;
newIndex = index1;
}
return (currentIndex, newIndex);
}
private async Task SwitchIndices(string currentIndex, string newIndex, string alias)
{
await _elasticClient.Indices.BulkAliasAsync(a => a
.Remove(i => i.Alias(alias).Index(currentIndex))
.Add(i => i.Alias(alias).Index(newIndex))
);
await _elasticClient.Indices.DeleteAsync(currentIndex);
}
public async Task UpdateHighwaysZeroDownTime(List<IFeature> highways)
{
var (currentIndex, newIndex) = GetIndicesStatus(OSM_HIGHWAYS_INDEX1, OSM_HIGHWAYS_INDEX2, OSM_HIGHWAYS_ALIAS);
await CreateHighwaysIndex(newIndex);
await UpdateUsingPaging(highways, newIndex);
await SwitchIndices(currentIndex, newIndex, OSM_HIGHWAYS_ALIAS);
}
public async Task StorePointsOfInterestDataToSecondaryIndex(List<IFeature> pointsOfInterest)
{
var (_, newIndex) = GetIndicesStatus(OSM_POIS_INDEX1, OSM_POIS_INDEX2, OSM_POIS_ALIAS);
await CreatePointsOfInterestIndex(newIndex);
await UpdateUsingPaging(pointsOfInterest, newIndex);
}
public async Task SwitchPointsOfInterestIndices()
{
var (currentIndex, newIndex) = GetIndicesStatus(OSM_POIS_INDEX1, OSM_POIS_INDEX2, OSM_POIS_ALIAS);
await SwitchIndices(currentIndex, newIndex, OSM_POIS_ALIAS);
}
public Task UpdateHighwaysData(List<IFeature> features)
{
return UpdateData(features, OSM_HIGHWAYS_ALIAS);
}
public Task UpdatePointsOfInterestData(List<IFeature> features)
{
return UpdateData(features, OSM_POIS_ALIAS);
}
private async Task UpdateData(List<IFeature> features, string alias)
{
var result = await _elasticClient.BulkAsync(bulk =>
{
foreach (var feature in features)
{
bulk.Index<IFeature>(i => i.Index(alias).Document(feature).Id(feature.GetId()));
}
return bulk;
});
if (result.IsValid == false)
{
result.ItemsWithErrors.ToList().ForEach(i => _logger.LogError($"Inserting {i.Id} failed with error: {i.Error?.Reason ?? string.Empty} caused by: {i.Error?.CausedBy?.Reason ?? string.Empty}"));
}
}
public async Task<List<IFeature>> GetHighways(Coordinate northEast, Coordinate southWest)
{
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_HIGHWAYS_ALIAS)
.Size(5000)
.Query(
q => q.GeoShape(g =>
g.Shape(sh => sh.Envelope(ConvertCoordinate(new Coordinate(southWest.X, northEast.Y)), ConvertCoordinate(new Coordinate(northEast.X, southWest.Y))))
.Field(f => f.Geometry)
.Relation(GeoShapeRelation.Intersects)
)
)
);
return response.Documents.ToList();
}
private GeoCoordinate ConvertCoordinate(Coordinate coordinate)
{
return new GeoCoordinate(coordinate.Y, coordinate.X);
}
public async Task<List<IFeature>> GetPointsOfInterest(Coordinate northEast, Coordinate southWest, string[] categories, string language)
{
var languages = language == Languages.ALL ? Languages.Array : new[] { language };
languages = languages.Concat(new[] { Languages.ALL }).ToArray();
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(OSM_POIS_ALIAS)
.Size(10000).Query(
q => q.GeoBoundingBox(
b => ConvertToGeoBoundingBox(b, northEast, southWest)
) &&
q.Terms(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_CATEGORY}").Terms(categories.Select(c => c.ToLower()).ToArray())) &&
q.Terms(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_LANGUAGE}").Terms(languages))
)
);
return response.Documents.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
public async Task<List<IFeature>> GetAllPointsOfInterest(bool withDeleted)
{
await _elasticClient.Indices.RefreshAsync(OSM_POIS_ALIAS);
var categories = Categories.Points.Concat(Categories.Routes).Select(c => c.ToLower()).ToArray();
var response = await _elasticClient.SearchAsync<IFeature>(s => s.Index(OSM_POIS_ALIAS)
.Size(10000)
.Scroll("10s")
.Query(q => q.Terms(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_CATEGORY}").Terms(categories))
));
var list = GetAllItemsByScrolling(response);
if (withDeleted == false)
{
list = list.Where(f => !f.Attributes.Exists(FeatureAttributes.POI_DELETED)).ToList();
}
return list;
}
private GeoBoundingBoxQueryDescriptor<IFeature> ConvertToGeoBoundingBox(GeoBoundingBoxQueryDescriptor<IFeature> b,
Coordinate northEast, Coordinate southWest)
{
return b.BoundingBox(
bb => bb.TopLeft(new GeoCoordinate(northEast.Y, southWest.X))
.BottomRight(new GeoCoordinate(southWest.Y, northEast.X))
).Field($"{PROPERTIES}.{FeatureAttributes.POI_GEOLOCATION}");
}
public async Task<List<IFeature>> GetPointsOfInterestUpdates(DateTime lastModifiedDate, DateTime modifiedUntil)
{
var categories = Categories.Points.Concat(Categories.Routes).Select(c => c.ToLower()).ToArray();
var response = await _elasticClient.SearchAsync<IFeature>(s => s.Index(OSM_POIS_ALIAS)
.Size(10000)
.Scroll("10s")
.Query(q => q.DateRange(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_LAST_MODIFIED}").GreaterThan(lastModifiedDate).LessThanOrEquals(modifiedUntil))
&& q.Terms(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_CATEGORY}").Terms(categories))
));
return GetAllItemsByScrolling(response);
}
public async Task<IFeature> GetPointOfInterestById(string id, string source)
{
var fullId = GeoJsonExtensions.GetId(source, id);
var response = await _elasticClient.GetAsync<IFeature>(fullId, r => r.Index(OSM_POIS_ALIAS));
return response.Source;
}
public Task DeletePointOfInterestById(string id, string source)
{
var fullId = GeoJsonExtensions.GetId(source, id);
return _elasticClient.DeleteAsync<IFeature>(fullId, d => d.Index(OSM_POIS_ALIAS));
}
public async Task<List<IFeature>> GetExternalPoisBySource(string source)
{
var response = await _elasticClient.SearchAsync<IFeature>(
s => s.Index(EXTERNAL_POIS)
.Size(10000)
.Scroll("10s")
.Query(q =>
q.Term(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_SOURCE}").Value(source.ToLower()))
)
);
var features = GetAllItemsByScrolling(response);
_logger.LogInformation($"Got {features.Count} features for source {source}");
return features;
}
public async Task<IFeature> GetExternalPoiById(string id, string source)
{
var response = await _elasticClient.GetAsync<IFeature>(GeoJsonExtensions.GetId(source, id), r => r.Index(EXTERNAL_POIS));
return response.Source;
}
public async Task AddExternalPois(List<IFeature> features)
{
if ((await _elasticClient.Indices.ExistsAsync(EXTERNAL_POIS)).Exists == false)
{
await CreateExternalPoisIndex();
}
await UpdateData(features, EXTERNAL_POIS);
}
public Task DeleteExternalPoisBySource(string source)
{
return _elasticClient.DeleteByQueryAsync<IFeature>(d =>
d.Index(EXTERNAL_POIS)
.Query(q =>
q.Term(t => t.Field($"{PROPERTIES}.{FeatureAttributes.POI_SOURCE}").Value(source.ToLower()))
)
);
}
private Task CreateHighwaysIndex(string highwaysIndexName)
{
return _elasticClient.Indices.CreateAsync(highwaysIndexName,
c => c.Map<IFeature>(m =>
m.Dynamic(false)
.Properties(ps =>
ps.GeoShape(g =>
g.Name(f => f.Geometry)
)
)
)
);
}
private Task CreatePointsOfInterestIndex(string poisIndexName)
{
return _elasticClient.Indices.CreateAsync(poisIndexName,
c => c.Map<IFeature>(m =>
m.Properties(ps =>
ps.Object<IAttributesTable>(o => o
.Name(PROPERTIES)
.Properties(p => p.GeoPoint(s => s.Name(FeatureAttributes.POI_GEOLOCATION)))
.Properties(p => p.Keyword(s => s.Name(FeatureAttributes.ID)))
.Properties(p => p.Date(s => s.Name(FeatureAttributes.POI_LAST_MODIFIED)))
).GeoShape(g =>
g.Name(f => f.Geometry)
)
)
).Settings(s => s.Setting("index.mapping.total_fields.limit", 10000))
);
}
private Task CreateExternalPoisIndex()
{
return _elasticClient.Indices.CreateAsync(EXTERNAL_POIS,
c => c.Map<IFeature>(m =>
m.Properties(fp =>
fp.Object<IAttributesTable>(a => a
.Name(PROPERTIES)
.Properties(p => p.Keyword(s => s.Name(FeatureAttributes.ID)))
)
)
).Settings(s => s.Setting("index.mapping.total_fields.limit", 10000))
);
}
private Task CreateImagesIndex()
{
return _elasticClient.Indices.CreateAsync(IMAGES, c =>
c.Map<ImageItem>(m =>
m.Properties(p =>
p.Keyword(k => k.Name(ii => ii.Hash))
.Keyword(s => s.Name(n => n.ImageUrls))
.Binary(a => a.Name(i => i.Thumbnail))
)
)
);
}
private Task CreateSharesIndex()
{
return _elasticClient.Indices.CreateAsync(SHARES,
c => c.Map<ShareUrl>(m => m.AutoMap<ShareUrl>())
);
}
private async Task UpdateUsingPaging(List<IFeature> features, string alias)
{
_logger.LogInformation($"Starting indexing {features.Count} records");
var smallCacheList = new List<IFeature>(PAGE_SIZE);
int total = 0;
foreach (var feature in features)
{
smallCacheList.Add(feature);
if (smallCacheList.Count < PAGE_SIZE)
{
continue;
}
total += smallCacheList.Count;
await UpdateData(smallCacheList, alias);
_logger.LogDebug($"Indexed {total} records of {features.Count}");
smallCacheList.Clear();
}
await UpdateData(smallCacheList, alias);
_logger.LogInformation($"Finished indexing {features.Count} records");
}
public Task AddUrl(ShareUrl shareUrl)
{
return _elasticClient.IndexAsync(shareUrl, r => r.Index(SHARES).Id(shareUrl.Id));
}
public async Task<ShareUrl> GetUrlById(string id)
{
var response = await _elasticClient.GetAsync<ShareUrl>(id, r => r.Index(SHARES));
return response.Source;
}
public async Task<DateTime> GetUrlTimestampById(string id)
{
var response = await _elasticClient.GetAsync<ShareUrl>(id, r => r.Index(SHARES).SourceIncludes(e => e.LastModifiedDate, e=> e.CreationDate));
if (response.Source == null) {
return DateTime.MinValue;
}
response.Source.FixModifiedDate();
return response.Source.LastModifiedDate;
}
public async Task<List<ShareUrl>> GetUrlsByUser(string osmUserId)
{
var response = await _elasticClient.SearchAsync<ShareUrl>(s => s.Index(SHARES)
.Size(5000)
.Query(q => q.Term(t => t.OsmUserId, osmUserId))
.Source(src => src
.IncludeAll()
.Excludes(e => e.Fields(p => p.DataContainer))
)
);
return response.Documents.ToList();
}
public Task Delete(ShareUrl shareUrl)
{
return _elasticClient.DeleteAsync<ShareUrl>(shareUrl.Id, d => d.Index(SHARES));
}
public Task Update(ShareUrl shareUrl)
{
return AddUrl(shareUrl);
}
public async Task<List<MapLayerData>> GetUserLayers(string osmUserId)
{
var response = await _elasticClient.SearchAsync<MapLayerData>(s => s.Index(CUSTOM_USER_LAYERS).Size(1000)
.Query(q => q.Term(t => t.OsmUserId, osmUserId)));
var layers = response.Documents.ToList();
return response.Hits.Select((h, i) =>
{
layers[i].Id = h.Id;
return layers[i];
}).ToList();
}
public async Task<MapLayerData> GetUserLayerById(string id)
{
var response = await _elasticClient.GetAsync<MapLayerData>(id, r => r.Index(CUSTOM_USER_LAYERS));
response.Source.Id = id;
return response.Source;
}
public async Task<MapLayerData> AddUserLayer(MapLayerData layerData)
{
var response = await _elasticClient.IndexAsync(layerData, r => r.Index(CUSTOM_USER_LAYERS));
layerData.Id = response.Id;
return layerData;
}
public Task UpdateUserLayer(MapLayerData layerData)
{
return _elasticClient.IndexAsync(layerData, r => r.Index(CUSTOM_USER_LAYERS).Id(layerData.Id));
}
public Task DeleteUserLayer(MapLayerData layerData)
{
return _elasticClient.DeleteAsync<MapLayerData>(layerData.Id, d => d.Index(CUSTOM_USER_LAYERS));
}
public async Task<ImageItem> GetImageByUrl(string url)
{
var response = await _elasticClient.SearchAsync<ImageItem>(s =>
s.Index(IMAGES)
.Query(q => q.Match(m => m.Field(i => i.ImageUrls).Query(url)))
);
return response.Documents.FirstOrDefault();
}
public async Task<ImageItem> GetImageByHash(string hash)
{
var response = await _elasticClient.GetAsync<ImageItem>(hash, r => r.Index(IMAGES));
return response.Source;
}
public async Task<List<string>> GetAllUrls()
{
await _elasticClient.Indices.RefreshAsync(IMAGES);
var response = await _elasticClient.SearchAsync<ImageItem>(
s => s.Index(IMAGES)
.Size(10000)
.Scroll("10s")
.Source(sf => sf
.Includes(i => i.Fields(f => f.ImageUrls, f => f.Hash))
).Query(q => q.MatchAll())
);
var list = GetAllItemsByScrolling(response);
return list.SelectMany(i => i.ImageUrls ?? new List<String>()).ToList();
}
public Task StoreImage(ImageItem imageItem)
{
return _elasticClient.IndexAsync(imageItem, r => r.Index(IMAGES).Id(imageItem.Hash));
}
public async Task DeleteImageByUrl(string url)
{
var imageItem = await GetImageByUrl(url);
if (imageItem != null)
{
await _elasticClient.DeleteAsync<IFeature>(imageItem.Hash, d => d.Index(IMAGES));
}
}
public Task StoreRebuildContext(RebuildContext context)
{
return _elasticClient.IndexAsync(context, r => r.Index(REBUILD_LOG).Id(context.StartTime.ToString("o")));
}
public async Task<DateTime> GetLastSuccessfulRebuildTime()
{
const string maxDate = "max_date";
var response = await _elasticClient.SearchAsync<RebuildContext>(s => s.Index(REBUILD_LOG)
.Size(1)
.Query(q => q.Term(t => t.Field(r => r.Succeeded).Value(true)))
.Aggregations(a => a.Max(maxDate, m => m.Field(r => r.StartTime)))
);
return DateTime.TryParse(response.Aggregations.Max(maxDate).ValueAsString, out var date)
? date
: DateTime.MinValue;
}
}
}