From dbd821dfc9a50bb0e74ca9c9b62d3335fa32a6e2 Mon Sep 17 00:00:00 2001 From: Ryan Duffing Date: Mon, 16 Aug 2021 09:10:31 -0400 Subject: [PATCH] Properly mapping returned custom fields from search results to their original type --- .../Engine/SearchEngine.cs | 31 ++----------------- tests/Core.Tests/Engine/SearchEngineTests.cs | 4 +-- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs b/src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs index 6a215f67..943f3141 100644 --- a/src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs +++ b/src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs @@ -195,7 +195,7 @@ private static SearchHit Map(Hit hit) foreach(CustomProperty property in customPropertiesForType) { - if(!hit.Source.UnmappedFields.ContainsKey(property.Name)) + if(!hit.Source.UnmappedFields.ContainsKey(property.Name) || property.Type == null) { continue; } @@ -207,22 +207,7 @@ private static SearchHit Map(Hit hit) break; } - if(IsArrayValue(unmappedField)) - { - searchHit.CustomProperties[property.Name] = unmappedField.Children().Cast().Select(v => v.Value).ToArray(); - continue; - } - - if(IsDictionaryValue(unmappedField)) - { - searchHit.CustomProperties[property.Name] = JObject.FromObject(unmappedField).ToObject>(); - continue; - } - - if(unmappedField is JValue value) - { - searchHit.CustomProperties[property.Name] = value.Value; - } + searchHit.CustomProperties[property.Name] = unmappedField.ToObject(property.Type); } return searchHit; @@ -233,18 +218,6 @@ bool IsValidCustomProperty() && hit.Source?.UnmappedFields != null && hit.Source.UnmappedFields.Any(u => customPropertiesForType.Any(c => c.Name == u.Key)); } - - static bool IsArrayValue(JToken field) - { - return field.Type == JTokenType.Array - && field.Children().Any(); - } - - bool IsDictionaryValue(JToken field) - { - return field.Type == JTokenType.Object - && field.Children().OfType().Any(); - } } public RawResults GetRawResults(RequestBase query, string language, string indexName = null) diff --git a/tests/Core.Tests/Engine/SearchEngineTests.cs b/tests/Core.Tests/Engine/SearchEngineTests.cs index 5a481923..aaff4949 100644 --- a/tests/Core.Tests/Engine/SearchEngineTests.cs +++ b/tests/Core.Tests/Engine/SearchEngineTests.cs @@ -142,8 +142,8 @@ public void Query_ReturnsCustomProperties() Assert.Equal(text, result.Hits.First().CustomProperties["Text"]); Assert.Equal(lng1, result.Hits.First().CustomProperties["Int"]); Assert.Equal(dec1, result.Hits.First().CustomProperties["Dec"]); - Assert.True(Factory.ArrayEquals(arr1, result.Hits.First().CustomProperties["Array1"] as IEnumerable)); - Assert.True(Factory.ArrayEquals(arr2, result.Hits.First().CustomProperties["Array2"] as IEnumerable)); + Assert.True(Factory.ArrayEquals(arr1, result.Hits.First().CustomProperties["Array1"] as IEnumerable)); + Assert.True(Factory.ArrayEquals(arr2, result.Hits.First().CustomProperties["Array2"] as IEnumerable)); } [Fact]