Skip to content

Commit

Permalink
fix: fix tsc read elasticsearch mapping (#631)
Browse files Browse the repository at this point in the history
* fix: fix tsc read elasticsearch mapping

* chore: fix bug

* fix: update
  • Loading branch information
Qinyouzeng authored Jun 16, 2023
1 parent 75a6124 commit e635a80
Showing 1 changed file with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,25 @@ await client.SearchAsync<TResult>(s => searchDescriptorFunc(s.Index(indexName)).

#region mapping

/// <summary>
/// 获取mapping
/// </summary>
/// <param name="caller"></param>
/// <param name="indexName"></param>
/// <param name="token"></param>
/// <returns></returns>
public static async Task<IEnumerable<MappingResponseDto>> GetMappingAsync(this ICaller caller, string indexName, CancellationToken token = default)
{
var path = $"/{indexName}/_mapping";
var result = await caller.GetAsync<object>(path, false, token);
var json = (JsonElement)result!;
if (!json.TryGetProperty(indexName, out JsonElement root) || !root.TryGetProperty("mappings", out JsonElement mapping))
JsonElement mapping = default;
bool findMapping = false;
foreach (var item in json.EnumerateObject())
{
return default!;
if (!findMapping && item.Value.TryGetProperty("mappings", out mapping))
{
findMapping = true;
break;
}
}

return GetRepProperties(mapping, default!)!;
if (findMapping)
return GetRepProperties(mapping, default!)!;
throw new UserFriendlyException($"can't find mapping for index: {indexName}");
}

private static IEnumerable<MappingResponseDto>? GetRepProperties(JsonElement node, string? parentName = default)
Expand Down

0 comments on commit e635a80

Please sign in to comment.