Skip to content

Commit 0cccbbf

Browse files
committed
WIP - Upgrade to the new Elasticsearch client
1 parent dcb1b70 commit 0cccbbf

24 files changed

+234
-218
lines changed

src/Foundatio.Parsers.ElasticQueries/ElasticMappingResolver.cs

+45-34
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Linq;
4+
using Elastic.Clients.Elasticsearch;
5+
using Elastic.Clients.Elasticsearch.IndexManagement;
6+
using Elastic.Clients.Elasticsearch.Mapping;
47
using Exceptionless.DateTimeExtensions;
58
using Foundatio.Parsers.ElasticQueries.Extensions;
69
using Microsoft.Extensions.Logging;
710
using Microsoft.Extensions.Logging.Abstractions;
8-
using Nest;
911

1012
namespace Foundatio.Parsers.ElasticQueries;
1113

1214
public class ElasticMappingResolver
1315
{
14-
private ITypeMapping _serverMapping;
15-
private readonly ITypeMapping _codeMapping;
16+
private TypeMapping _serverMapping;
17+
private readonly TypeMapping _codeMapping;
1618
private readonly Inferrer _inferrer;
1719
private readonly ConcurrentDictionary<string, FieldMapping> _mappingCache = new();
1820
private readonly ILogger _logger;
1921

2022
public static ElasticMappingResolver NullInstance = new(() => null);
2123

22-
public ElasticMappingResolver(Func<ITypeMapping> getMapping, Inferrer inferrer = null, ILogger logger = null)
24+
public ElasticMappingResolver(Func<TypeMapping> getMapping, Inferrer inferrer = null, ILogger logger = null)
2325
{
2426
GetServerMappingFunc = getMapping;
2527
_inferrer = inferrer;
2628
_logger = logger ?? NullLogger.Instance;
2729
}
2830

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)
3032
: this(getMapping, inferrer, logger)
3133
{
3234
_codeMapping = codeMapping;
@@ -53,7 +55,7 @@ public FieldMapping GetMapping(string field, bool followAlias = false)
5355
if (_mappingCache.TryGetValue(field, out var mapping))
5456
{
5557

56-
if (followAlias && mapping.Found && mapping.Property is IFieldAliasProperty fieldAlias)
58+
if (followAlias && mapping.Found && mapping.Property is FieldAliasProperty fieldAlias)
5759
{
5860
_logger.LogTrace("Cached alias mapping: {Field}={FieldPath}:{FieldType}", field, mapping.FullPath, mapping.Property?.Type);
5961
return GetMapping(fieldAlias.Path.Name);
@@ -136,19 +138,19 @@ public FieldMapping GetMapping(string field, bool followAlias = false)
136138
_mappingCache.AddOrUpdate(field, resolvedMapping, (f, m) => resolvedMapping);
137139
_logger.LogTrace("Resolved mapping: {Field}={FieldPath}:{FieldType}", field, resolvedMapping.FullPath, resolvedMapping.Property?.Type);
138140

139-
if (followAlias && resolvedMapping.Property is IFieldAliasProperty fieldAlias)
141+
if (followAlias && resolvedMapping.Property is FieldAliasProperty fieldAlias)
140142
return GetMapping(fieldAlias.Path.Name);
141143

142144
return resolvedMapping;
143145
}
144146

145-
if (fieldMapping is IObjectProperty objectProperty)
147+
if (fieldMapping is ObjectProperty objectProperty)
146148
{
147149
currentProperties = objectProperty.Properties;
148150
}
149151
else
150152
{
151-
if (fieldMapping is ITextProperty textProperty)
153+
if (fieldMapping is TextProperty textProperty)
152154
currentProperties = textProperty.Fields;
153155
else
154156
break;
@@ -235,7 +237,7 @@ public string GetNonAnalyzedFieldName(string field, string preferredSubField = n
235237

236238
var nonAnalyzedProperty = multiFieldProperty.Fields.OrderByDescending(kvp => kvp.Key.Name == preferredSubField).FirstOrDefault(kvp =>
237239
{
238-
if (kvp.Value is IKeywordProperty)
240+
if (kvp.Value is KeywordProperty)
239241
return true;
240242

241243
if (!IsPropertyAnalyzed(kvp.Value))
@@ -265,7 +267,7 @@ public bool IsPropertyAnalyzed(string field)
265267

266268
public bool IsPropertyAnalyzed(IProperty property)
267269
{
268-
if (property is ITextProperty textProperty)
270+
if (property is TextProperty textProperty)
269271
return !textProperty.Index.HasValue || textProperty.Index.Value;
270272

271273
return false;
@@ -276,39 +278,48 @@ public bool IsNestedPropertyType(string field)
276278
if (String.IsNullOrEmpty(field))
277279
return false;
278280

279-
return GetMappingProperty(field, true) is INestedProperty;
281+
return GetMappingProperty(field, true) is NestedProperty;
280282
}
281283

282284
public bool IsGeoPropertyType(string field)
283285
{
284286
if (String.IsNullOrEmpty(field))
285287
return false;
286288

287-
return GetMappingProperty(field, true) is IGeoPointProperty;
289+
return GetMappingProperty(field, true) is GeoPointProperty;
288290
}
289291

290292
public bool IsNumericPropertyType(string field)
291293
{
292294
if (String.IsNullOrEmpty(field))
293295
return false;
294296

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;
296307
}
297308

298309
public bool IsBooleanPropertyType(string field)
299310
{
300311
if (String.IsNullOrEmpty(field))
301312
return false;
302313

303-
return GetMappingProperty(field, true) is IBooleanProperty;
314+
return GetMappingProperty(field, true) is BooleanProperty;
304315
}
305316

306317
public bool IsDatePropertyType(string field)
307318
{
308319
if (String.IsNullOrEmpty(field))
309320
return false;
310321

311-
return GetMappingProperty(field, true) is IDateProperty;
322+
return GetMappingProperty(field, true) is DateProperty;
312323
}
313324

314325
public FieldType GetFieldType(string field)
@@ -334,7 +345,7 @@ public FieldType GetFieldType(string field)
334345
"completion" => FieldType.Completion,
335346
"nested" => FieldType.Nested,
336347
"object" => FieldType.Object,
337-
"murmur3" => FieldType.Murmur3Hash,
348+
"murmur3" => FieldType.Murmur3,
338349
"token_count" => FieldType.TokenCount,
339350
"percolator" => FieldType.Percolator,
340351
"integer" => FieldType.Integer,
@@ -355,12 +366,12 @@ public FieldType GetFieldType(string field)
355366
};
356367
}
357368

358-
private IProperties MergeProperties(IProperties codeProperties, IProperties serverProperties)
369+
private Properties MergeProperties(Properties codeProperties, Properties serverProperties)
359370
{
360371
if (codeProperties == null && serverProperties == null)
361372
return null;
362373

363-
IProperties mergedCodeProperties = null;
374+
Properties mergedCodeProperties = null;
364375
// resolve code mapping property expressions using inferrer
365376
if (codeProperties != null)
366377
{
@@ -369,7 +380,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
369380
foreach (var kvp in codeProperties)
370381
{
371382
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))
373384
propertyName = _inferrer.PropertyName(kvp.Key) ?? kvp.Key;
374385

375386
mergedCodeProperties[propertyName] = kvp.Value;
@@ -380,12 +391,12 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
380391
// resolve field alias
381392
foreach (var kvp in codeProperties)
382393
{
383-
if (kvp.Value is not IFieldAliasProperty aliasProperty)
394+
if (kvp.Value is not FieldAliasProperty aliasProperty)
384395
continue;
385396

386397
mergedCodeProperties[kvp.Key] = new FieldAliasProperty
387398
{
388-
LocalMetadata = aliasProperty.LocalMetadata,
399+
Meta = aliasProperty.Meta,
389400
Path = _inferrer?.Field(aliasProperty.Path) ?? aliasProperty.Path,
390401
Name = aliasProperty.Name
391402
};
@@ -397,7 +408,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
397408
if (mergedCodeProperties == null || serverProperties == null)
398409
return mergedCodeProperties ?? serverProperties;
399410

400-
IProperties properties = new Properties();
411+
Properties properties = new Properties();
401412
foreach (var serverProperty in serverProperties)
402413
{
403414
var merged = serverProperty.Value;
@@ -406,12 +417,12 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
406417

407418
switch (merged)
408419
{
409-
case IObjectProperty objectProperty:
410-
var codeObjectProperty = codeProperty as IObjectProperty;
420+
case ObjectProperty objectProperty:
421+
var codeObjectProperty = codeProperty as ObjectProperty;
411422
objectProperty.Properties = MergeProperties(codeObjectProperty?.Properties, objectProperty.Properties);
412423
break;
413-
case ITextProperty textProperty:
414-
var codeTextProperty = codeProperty as ITextProperty;
424+
case TextProperty textProperty:
425+
var codeTextProperty = codeProperty as TextProperty;
415426
textProperty.Fields = MergeProperties(codeTextProperty?.Fields, textProperty.Fields);
416427
break;
417428
}
@@ -430,7 +441,7 @@ private IProperties MergeProperties(IProperties codeProperties, IProperties serv
430441
return properties;
431442
}
432443

433-
private Func<ITypeMapping> GetServerMappingFunc { get; set; }
444+
private Func<TypeMapping> GetServerMappingFunc { get; set; }
434445
private DateTime? _lastMappingUpdate = null;
435446
private bool GetServerMapping()
436447
{
@@ -455,7 +466,7 @@ private bool GetServerMapping()
455466
}
456467
}
457468

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
459470
{
460471
logger ??= NullLogger.Instance;
461472

@@ -471,7 +482,7 @@ public static ElasticMappingResolver Create<T>(Func<TypeMappingDescriptor<T>, IT
471482
}, logger);
472483
}
473484

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
475486
{
476487
logger ??= NullLogger.Instance;
477488

@@ -487,14 +498,14 @@ public static ElasticMappingResolver Create<T>(Func<TypeMappingDescriptor<T>, IT
487498
}, logger);
488499
}
489500

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
491502
{
492503
var codeMapping = new TypeMappingDescriptor<T>();
493504
codeMapping = mappingBuilder(codeMapping) as TypeMappingDescriptor<T>;
494505
return new ElasticMappingResolver(codeMapping, inferrer, getMapping, logger: logger);
495506
}
496507

497-
public static ElasticMappingResolver Create<T>(IElasticClient client, ILogger logger = null)
508+
public static ElasticMappingResolver Create<T>(ElasticsearchClient client, ILogger logger = null)
498509
{
499510
logger ??= NullLogger.Instance;
500511

@@ -510,7 +521,7 @@ public static ElasticMappingResolver Create<T>(IElasticClient client, ILogger lo
510521
}, client.Infer, logger);
511522
}
512523

513-
public static ElasticMappingResolver Create(IElasticClient client, string index, ILogger logger = null)
524+
public static ElasticMappingResolver Create(ElasticsearchClient client, string index, ILogger logger = null)
514525
{
515526
logger ??= NullLogger.Instance;
516527

@@ -526,7 +537,7 @@ public static ElasticMappingResolver Create(IElasticClient client, string index,
526537
}, client.Infer, logger);
527538
}
528539

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)
530541
{
531542
return new ElasticMappingResolver(getMapping, inferrer, logger: logger);
532543
}

src/Foundatio.Parsers.ElasticQueries/ElasticQueryParser.cs

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using Elastic.Clients.Elasticsearch;
6+
using Elastic.Clients.Elasticsearch.Aggregations;
7+
using Elastic.Clients.Elasticsearch.QueryDsl;
58
using Foundatio.Parsers.ElasticQueries.Extensions;
69
using Foundatio.Parsers.ElasticQueries.Visitors;
710
using Foundatio.Parsers.LuceneQueries;
811
using Foundatio.Parsers.LuceneQueries.Extensions;
912
using Foundatio.Parsers.LuceneQueries.Nodes;
1013
using Foundatio.Parsers.LuceneQueries.Visitors;
11-
using Nest;
1214
using Pegasus.Common;
1315

1416
namespace Foundatio.Parsers.ElasticQueries;
@@ -162,7 +164,7 @@ public async Task<QueryValidationResult> ValidateQueryAsync(string query, QueryV
162164
return context.GetValidationResult();
163165
}
164166

165-
public async Task<QueryContainer> BuildQueryAsync(string query, IElasticQueryVisitorContext context = null)
167+
public async Task<Query> BuildQueryAsync(string query, IElasticQueryVisitorContext context = null)
166168
{
167169
if (context == null)
168170
context = new ElasticQueryVisitorContext();
@@ -175,7 +177,7 @@ public async Task<QueryContainer> BuildQueryAsync(string query, IElasticQueryVis
175177
return await BuildQueryAsync(result, context).ConfigureAwait(false);
176178
}
177179

178-
public async Task<QueryContainer> BuildQueryAsync(IQueryNode query, IElasticQueryVisitorContext context = null)
180+
public async Task<Query> BuildQueryAsync(IQueryNode query, IElasticQueryVisitorContext context = null)
179181
{
180182
if (context == null)
181183
context = new ElasticQueryVisitorContext();
@@ -185,7 +187,7 @@ public async Task<QueryContainer> BuildQueryAsync(IQueryNode query, IElasticQuer
185187
{
186188
q = new BoolQuery
187189
{
188-
Filter = new QueryContainer[] { q }
190+
Filter = new Query[] { q }
189191
};
190192
}
191193

@@ -205,7 +207,7 @@ public async Task<QueryValidationResult> ValidateAggregationsAsync(string query,
205207
return context.GetValidationResult();
206208
}
207209

208-
public async Task<AggregationContainer> BuildAggregationsAsync(string aggregations, IElasticQueryVisitorContext context = null)
210+
public async Task<Aggregation> BuildAggregationsAsync(string aggregations, IElasticQueryVisitorContext context = null)
209211
{
210212
if (context == null)
211213
context = new ElasticQueryVisitorContext();
@@ -219,7 +221,7 @@ public async Task<AggregationContainer> BuildAggregationsAsync(string aggregatio
219221
}
220222

221223
#pragma warning disable IDE0060 // Remove unused parameter
222-
public async Task<AggregationContainer> BuildAggregationsAsync(IQueryNode aggregations, IElasticQueryVisitorContext context = null)
224+
public async Task<Aggregation> BuildAggregationsAsync(IQueryNode aggregations, IElasticQueryVisitorContext context = null)
223225
{
224226
if (aggregations == null)
225227
return null;
@@ -241,7 +243,7 @@ public async Task<QueryValidationResult> ValidateSortAsync(string query, QueryVa
241243
return context.GetValidationResult();
242244
}
243245

244-
public async Task<IEnumerable<IFieldSort>> BuildSortAsync(string sort, IElasticQueryVisitorContext context = null)
246+
public async Task<IEnumerable<FieldSort>> BuildSortAsync(string sort, IElasticQueryVisitorContext context = null)
245247
{
246248
if (context == null)
247249
context = new ElasticQueryVisitorContext();
@@ -254,7 +256,7 @@ public async Task<IEnumerable<IFieldSort>> BuildSortAsync(string sort, IElasticQ
254256
return await BuildSortAsync(result, context).ConfigureAwait(false);
255257
}
256258

257-
public Task<IEnumerable<IFieldSort>> BuildSortAsync(IQueryNode sort, IElasticQueryVisitorContext context = null)
259+
public Task<IEnumerable<FieldSort>> BuildSortAsync(IQueryNode sort, IElasticQueryVisitorContext context = null)
258260
{
259261
if (context == null)
260262
context = new ElasticQueryVisitorContext();

0 commit comments

Comments
 (0)