Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly mapping returned custom fields from search results to their … #134

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions src/Epinova.ElasticSearch.Core/Engine/SearchEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -207,22 +207,7 @@ private static SearchHit Map(Hit hit)
break;
}

if(IsArrayValue(unmappedField))
{
searchHit.CustomProperties[property.Name] = unmappedField.Children().Cast<JValue>().Select(v => v.Value).ToArray();
continue;
}

if(IsDictionaryValue(unmappedField))
{
searchHit.CustomProperties[property.Name] = JObject.FromObject(unmappedField).ToObject<IDictionary<string, object>>();
continue;
}

if(unmappedField is JValue value)
{
searchHit.CustomProperties[property.Name] = value.Value;
}
searchHit.CustomProperties[property.Name] = unmappedField.ToObject(property.Type);
}

return searchHit;
Expand All @@ -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<JProperty>().Any();
}
}

public RawResults<TRoot> GetRawResults<TRoot>(RequestBase query, string language, string indexName = null)
Expand Down
4 changes: 2 additions & 2 deletions tests/Core.Tests/Engine/SearchEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>));
Assert.True(Factory.ArrayEquals(arr2, result.Hits.First().CustomProperties["Array2"] as IEnumerable<object>));
Assert.True(Factory.ArrayEquals(arr1, result.Hits.First().CustomProperties["Array1"] as IEnumerable<long>));
Assert.True(Factory.ArrayEquals(arr2, result.Hits.First().CustomProperties["Array2"] as IEnumerable<string>));
}

[Fact]
Expand Down