1
1
using System ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Linq ;
4
+ using Elastic . Clients . Elasticsearch ;
5
+ using Elastic . Clients . Elasticsearch . IndexManagement ;
6
+ using Elastic . Clients . Elasticsearch . Mapping ;
4
7
using Exceptionless . DateTimeExtensions ;
5
8
using Foundatio . Parsers . ElasticQueries . Extensions ;
6
9
using Microsoft . Extensions . Logging ;
7
10
using Microsoft . Extensions . Logging . Abstractions ;
8
- using Nest ;
9
11
10
12
namespace Foundatio . Parsers . ElasticQueries ;
11
13
12
14
public class ElasticMappingResolver
13
15
{
14
- private ITypeMapping _serverMapping ;
15
- private readonly ITypeMapping _codeMapping ;
16
+ private TypeMapping _serverMapping ;
17
+ private readonly TypeMapping _codeMapping ;
16
18
private readonly Inferrer _inferrer ;
17
19
private readonly ConcurrentDictionary < string , FieldMapping > _mappingCache = new ( ) ;
18
20
private readonly ILogger _logger ;
19
21
20
22
public static ElasticMappingResolver NullInstance = new ( ( ) => null ) ;
21
23
22
- public ElasticMappingResolver ( Func < ITypeMapping > getMapping , Inferrer inferrer = null , ILogger logger = null )
24
+ public ElasticMappingResolver ( Func < TypeMapping > getMapping , Inferrer inferrer = null , ILogger logger = null )
23
25
{
24
26
GetServerMappingFunc = getMapping ;
25
27
_inferrer = inferrer ;
26
28
_logger = logger ?? NullLogger . Instance ;
27
29
}
28
30
29
- public ElasticMappingResolver ( ITypeMapping codeMapping , Inferrer inferrer , Func < ITypeMapping > getMapping , ILogger logger = null )
31
+ public ElasticMappingResolver ( TypeMapping codeMapping , Inferrer inferrer , Func < TypeMapping > getMapping , ILogger logger = null )
30
32
: this ( getMapping , inferrer , logger )
31
33
{
32
34
_codeMapping = codeMapping ;
@@ -53,7 +55,7 @@ public FieldMapping GetMapping(string field, bool followAlias = false)
53
55
if ( _mappingCache . TryGetValue ( field , out var mapping ) )
54
56
{
55
57
56
- if ( followAlias && mapping . Found && mapping . Property is IFieldAliasProperty fieldAlias )
58
+ if ( followAlias && mapping . Found && mapping . Property is FieldAliasProperty fieldAlias )
57
59
{
58
60
_logger . LogTrace ( "Cached alias mapping: {Field}={FieldPath}:{FieldType}" , field , mapping . FullPath , mapping . Property ? . Type ) ;
59
61
return GetMapping ( fieldAlias . Path . Name ) ;
@@ -136,19 +138,19 @@ public FieldMapping GetMapping(string field, bool followAlias = false)
136
138
_mappingCache . AddOrUpdate ( field , resolvedMapping , ( f , m ) => resolvedMapping ) ;
137
139
_logger . LogTrace ( "Resolved mapping: {Field}={FieldPath}:{FieldType}" , field , resolvedMapping . FullPath , resolvedMapping . Property ? . Type ) ;
138
140
139
- if ( followAlias && resolvedMapping . Property is IFieldAliasProperty fieldAlias )
141
+ if ( followAlias && resolvedMapping . Property is FieldAliasProperty fieldAlias )
140
142
return GetMapping ( fieldAlias . Path . Name ) ;
141
143
142
144
return resolvedMapping ;
143
145
}
144
146
145
- if ( fieldMapping is IObjectProperty objectProperty )
147
+ if ( fieldMapping is ObjectProperty objectProperty )
146
148
{
147
149
currentProperties = objectProperty . Properties ;
148
150
}
149
151
else
150
152
{
151
- if ( fieldMapping is ITextProperty textProperty )
153
+ if ( fieldMapping is TextProperty textProperty )
152
154
currentProperties = textProperty . Fields ;
153
155
else
154
156
break ;
@@ -235,7 +237,7 @@ public string GetNonAnalyzedFieldName(string field, string preferredSubField = n
235
237
236
238
var nonAnalyzedProperty = multiFieldProperty . Fields . OrderByDescending ( kvp => kvp . Key . Name == preferredSubField ) . FirstOrDefault ( kvp =>
237
239
{
238
- if ( kvp . Value is IKeywordProperty )
240
+ if ( kvp . Value is KeywordProperty )
239
241
return true ;
240
242
241
243
if ( ! IsPropertyAnalyzed ( kvp . Value ) )
@@ -265,7 +267,7 @@ public bool IsPropertyAnalyzed(string field)
265
267
266
268
public bool IsPropertyAnalyzed ( IProperty property )
267
269
{
268
- if ( property is ITextProperty textProperty )
270
+ if ( property is TextProperty textProperty )
269
271
return ! textProperty . Index . HasValue || textProperty . Index . Value ;
270
272
271
273
return false ;
@@ -276,39 +278,48 @@ public bool IsNestedPropertyType(string field)
276
278
if ( String . IsNullOrEmpty ( field ) )
277
279
return false ;
278
280
279
- return GetMappingProperty ( field , true ) is INestedProperty ;
281
+ return GetMappingProperty ( field , true ) is NestedProperty ;
280
282
}
281
283
282
284
public bool IsGeoPropertyType ( string field )
283
285
{
284
286
if ( String . IsNullOrEmpty ( field ) )
285
287
return false ;
286
288
287
- return GetMappingProperty ( field , true ) is IGeoPointProperty ;
289
+ return GetMappingProperty ( field , true ) is GeoPointProperty ;
288
290
}
289
291
290
292
public bool IsNumericPropertyType ( string field )
291
293
{
292
294
if ( String . IsNullOrEmpty ( field ) )
293
295
return false ;
294
296
295
- return GetMappingProperty ( field , true ) is INumberProperty ;
297
+ var property = GetMappingProperty ( field , true ) ;
298
+ return property is ByteNumberProperty
299
+ or DoubleNumberProperty
300
+ or FloatNumberProperty
301
+ or HalfFloatNumberProperty
302
+ or IntegerNumberProperty
303
+ or LongNumberProperty
304
+ or ScaledFloatNumberProperty
305
+ or ShortNumberProperty
306
+ or UnsignedLongNumberProperty ;
296
307
}
297
308
298
309
public bool IsBooleanPropertyType ( string field )
299
310
{
300
311
if ( String . IsNullOrEmpty ( field ) )
301
312
return false ;
302
313
303
- return GetMappingProperty ( field , true ) is IBooleanProperty ;
314
+ return GetMappingProperty ( field , true ) is BooleanProperty ;
304
315
}
305
316
306
317
public bool IsDatePropertyType ( string field )
307
318
{
308
319
if ( String . IsNullOrEmpty ( field ) )
309
320
return false ;
310
321
311
- return GetMappingProperty ( field , true ) is IDateProperty ;
322
+ return GetMappingProperty ( field , true ) is DateProperty ;
312
323
}
313
324
314
325
public FieldType GetFieldType ( string field )
@@ -334,7 +345,7 @@ public FieldType GetFieldType(string field)
334
345
"completion" => FieldType . Completion ,
335
346
"nested" => FieldType . Nested ,
336
347
"object" => FieldType . Object ,
337
- "murmur3" => FieldType . Murmur3Hash ,
348
+ "murmur3" => FieldType . Murmur3 ,
338
349
"token_count" => FieldType . TokenCount ,
339
350
"percolator" => FieldType . Percolator ,
340
351
"integer" => FieldType . Integer ,
@@ -355,12 +366,12 @@ public FieldType GetFieldType(string field)
355
366
} ;
356
367
}
357
368
358
- private IProperties MergeProperties ( IProperties codeProperties , IProperties serverProperties )
369
+ private Properties MergeProperties ( Properties codeProperties , Properties serverProperties )
359
370
{
360
371
if ( codeProperties == null && serverProperties == null )
361
372
return null ;
362
373
363
- IProperties mergedCodeProperties = null ;
374
+ Properties mergedCodeProperties = null ;
364
375
// resolve code mapping property expressions using inferrer
365
376
if ( codeProperties != null )
366
377
{
@@ -369,7 +380,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
369
380
foreach ( var kvp in codeProperties )
370
381
{
371
382
var propertyName = kvp . Key ;
372
- if ( _inferrer != null && ( String . IsNullOrEmpty ( kvp . Key . Name ) || kvp . Value is IFieldAliasProperty ) )
383
+ if ( _inferrer != null && ( String . IsNullOrEmpty ( kvp . Key . Name ) || kvp . Value is FieldAliasProperty ) )
373
384
propertyName = _inferrer . PropertyName ( kvp . Key ) ?? kvp . Key ;
374
385
375
386
mergedCodeProperties [ propertyName ] = kvp . Value ;
@@ -380,12 +391,12 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
380
391
// resolve field alias
381
392
foreach ( var kvp in codeProperties )
382
393
{
383
- if ( kvp . Value is not IFieldAliasProperty aliasProperty )
394
+ if ( kvp . Value is not FieldAliasProperty aliasProperty )
384
395
continue ;
385
396
386
397
mergedCodeProperties [ kvp . Key ] = new FieldAliasProperty
387
398
{
388
- LocalMetadata = aliasProperty . LocalMetadata ,
399
+ Meta = aliasProperty . Meta ,
389
400
Path = _inferrer ? . Field ( aliasProperty . Path ) ?? aliasProperty . Path ,
390
401
Name = aliasProperty . Name
391
402
} ;
@@ -397,7 +408,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
397
408
if ( mergedCodeProperties == null || serverProperties == null )
398
409
return mergedCodeProperties ?? serverProperties ;
399
410
400
- IProperties properties = new Properties ( ) ;
411
+ Properties properties = new Properties ( ) ;
401
412
foreach ( var serverProperty in serverProperties )
402
413
{
403
414
var merged = serverProperty . Value ;
@@ -406,12 +417,12 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
406
417
407
418
switch ( merged )
408
419
{
409
- case IObjectProperty objectProperty :
410
- var codeObjectProperty = codeProperty as IObjectProperty ;
420
+ case ObjectProperty objectProperty :
421
+ var codeObjectProperty = codeProperty as ObjectProperty ;
411
422
objectProperty . Properties = MergeProperties ( codeObjectProperty ? . Properties , objectProperty . Properties ) ;
412
423
break ;
413
- case ITextProperty textProperty :
414
- var codeTextProperty = codeProperty as ITextProperty ;
424
+ case TextProperty textProperty :
425
+ var codeTextProperty = codeProperty as TextProperty ;
415
426
textProperty . Fields = MergeProperties ( codeTextProperty ? . Fields , textProperty . Fields ) ;
416
427
break ;
417
428
}
@@ -430,7 +441,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
430
441
return properties ;
431
442
}
432
443
433
- private Func < ITypeMapping > GetServerMappingFunc { get ; set ; }
444
+ private Func < TypeMapping > GetServerMappingFunc { get ; set ; }
434
445
private DateTime ? _lastMappingUpdate = null ;
435
446
private bool GetServerMapping ( )
436
447
{
@@ -455,7 +466,7 @@ private bool GetServerMapping()
455
466
}
456
467
}
457
468
458
- public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , ITypeMapping > mappingBuilder , IElasticClient client , ILogger logger = null ) where T : class
469
+ public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , TypeMapping > mappingBuilder , ElasticsearchClient client , ILogger logger = null ) where T : class
459
470
{
460
471
logger ??= NullLogger . Instance ;
461
472
@@ -471,7 +482,7 @@ public static ElasticMappingResolver Create<T>(Func<TypeMappingDescriptor<T>, IT
471
482
} , logger ) ;
472
483
}
473
484
474
- public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , ITypeMapping > mappingBuilder , IElasticClient client , string index , ILogger logger = null ) where T : class
485
+ public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , TypeMapping > mappingBuilder , ElasticsearchClient client , string index , ILogger logger = null ) where T : class
475
486
{
476
487
logger ??= NullLogger . Instance ;
477
488
@@ -487,14 +498,14 @@ public static ElasticMappingResolver Create<T>(Func<TypeMappingDescriptor<T>, IT
487
498
} , logger ) ;
488
499
}
489
500
490
- public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , ITypeMapping > mappingBuilder , Inferrer inferrer , Func < ITypeMapping > getMapping , ILogger logger = null ) where T : class
501
+ public static ElasticMappingResolver Create < T > ( Func < TypeMappingDescriptor < T > , TypeMapping > mappingBuilder , Inferrer inferrer , Func < TypeMapping > getMapping , ILogger logger = null ) where T : class
491
502
{
492
503
var codeMapping = new TypeMappingDescriptor < T > ( ) ;
493
504
codeMapping = mappingBuilder ( codeMapping ) as TypeMappingDescriptor < T > ;
494
505
return new ElasticMappingResolver ( codeMapping , inferrer , getMapping , logger : logger ) ;
495
506
}
496
507
497
- public static ElasticMappingResolver Create < T > ( IElasticClient client , ILogger logger = null )
508
+ public static ElasticMappingResolver Create < T > ( ElasticsearchClient client , ILogger logger = null )
498
509
{
499
510
logger ??= NullLogger . Instance ;
500
511
@@ -510,7 +521,7 @@ public static ElasticMappingResolver Create<T>(IElasticClient client, ILogger lo
510
521
} , client . Infer , logger ) ;
511
522
}
512
523
513
- public static ElasticMappingResolver Create ( IElasticClient client , string index , ILogger logger = null )
524
+ public static ElasticMappingResolver Create ( ElasticsearchClient client , string index , ILogger logger = null )
514
525
{
515
526
logger ??= NullLogger . Instance ;
516
527
@@ -526,7 +537,7 @@ public static ElasticMappingResolver Create(IElasticClient client, string index,
526
537
} , client . Infer , logger ) ;
527
538
}
528
539
529
- public static ElasticMappingResolver Create ( Func < ITypeMapping > getMapping , Inferrer inferrer , ILogger logger = null )
540
+ public static ElasticMappingResolver Create ( Func < TypeMapping > getMapping , Inferrer inferrer , ILogger logger = null )
530
541
{
531
542
return new ElasticMappingResolver ( getMapping , inferrer , logger : logger ) ;
532
543
}
0 commit comments