|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
| 5 | +using System; |
5 | 6 | using System.Collections.Generic;
|
| 7 | +using System.Linq.Expressions; |
6 | 8 | using System.Text.Json.Serialization;
|
| 9 | +using Elastic.Clients.Elasticsearch.Mapping; |
7 | 10 |
|
8 | 11 | namespace Elastic.Clients.Elasticsearch.IndexManagement;
|
9 | 12 |
|
10 | 13 | public partial class GetFieldMappingResponse
|
11 | 14 | {
|
12 | 15 | [JsonIgnore]
|
13 | 16 | public IReadOnlyDictionary<IndexName, TypeFieldMappings> FieldMappings => BackingDictionary;
|
| 17 | + |
| 18 | + public IProperty? GetProperty(IndexName index, Field field) |
| 19 | + { |
| 20 | + if (index is null) |
| 21 | + throw new ArgumentNullException(nameof(index)); |
| 22 | + |
| 23 | + if (field is null) |
| 24 | + throw new ArgumentNullException(nameof(field)); |
| 25 | + |
| 26 | + var mappings = MappingsFor(index); |
| 27 | + |
| 28 | + if (mappings is null) |
| 29 | + return null; |
| 30 | + |
| 31 | + if (!mappings.TryGetValue(field, out var fieldMapping) || fieldMapping.Mapping is null) |
| 32 | + return null; |
| 33 | + |
| 34 | + return fieldMapping.Mapping.TryGetProperty(PropertyName.FromField(field), out var property) ? property : null; |
| 35 | + } |
| 36 | + |
| 37 | + public bool TryGetProperty(IndexName index, Field field, out IProperty property) |
| 38 | + { |
| 39 | + property = null; |
| 40 | + |
| 41 | + if (index is null) |
| 42 | + throw new ArgumentNullException(nameof(index)); |
| 43 | + |
| 44 | + if (field is null) |
| 45 | + throw new ArgumentNullException(nameof(field)); |
| 46 | + |
| 47 | + var mappings = MappingsFor(index); |
| 48 | + |
| 49 | + if (mappings is null) |
| 50 | + return false; |
| 51 | + |
| 52 | + if (!mappings.TryGetValue(field, out var fieldMapping) || fieldMapping.Mapping is null) |
| 53 | + return false; |
| 54 | + |
| 55 | + if (fieldMapping.Mapping.TryGetProperty(PropertyName.FromField(field), out var matched)) |
| 56 | + { |
| 57 | + property = matched; |
| 58 | + return true; |
| 59 | + } |
| 60 | + |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + public IProperty? PropertyFor<T>(Field field) => PropertyFor<T>(field, null); |
| 65 | + |
| 66 | + public IProperty? PropertyFor<T>(Field field, IndexName index) => |
| 67 | + GetProperty(index ?? Infer.Index<T>(), field); |
| 68 | + |
| 69 | + public IProperty? PropertyFor<T, TValue>(Expression<Func<T, TValue>> objectPath) |
| 70 | + where T : class => |
| 71 | + GetProperty(Infer.Index<T>(), Infer.Field(objectPath)); |
| 72 | + |
| 73 | + public IProperty? PropertyFor<T, TValue>(Expression<Func<T, TValue>> objectPath, IndexName index) |
| 74 | + where T : class => |
| 75 | + GetProperty(index, Infer.Field(objectPath)); |
| 76 | + |
| 77 | + public IProperty? PropertyFor<T>(Expression<Func<T, object>> objectPath) |
| 78 | + where T : class => |
| 79 | + GetProperty(Infer.Index<T>(), Infer.Field(objectPath)); |
| 80 | + |
| 81 | + public IProperty? PropertyFor<T>(Expression<Func<T, object>> objectPath, IndexName index) |
| 82 | + where T : class => |
| 83 | + GetProperty(index, Infer.Field(objectPath)); |
| 84 | + |
| 85 | + private IReadOnlyDictionary<Field, FieldMapping> MappingsFor(IndexName index) |
| 86 | + { |
| 87 | + if (!FieldMappings.TryGetValue(index, out var indexMapping) || indexMapping.Mappings == null) |
| 88 | + return null; |
| 89 | + |
| 90 | + return indexMapping.Mappings; |
| 91 | + } |
14 | 92 | }
|
0 commit comments