Skip to content

Commit 36d1a50

Browse files
committed

File tree

9 files changed

+432
-39
lines changed

9 files changed

+432
-39
lines changed

src/Nest/CommonAbstractions/LazyDocument/LazyDocument.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Nest
77
public interface ILazyDocument
88
{
99
/// <summary>
10-
///
10+
///
1111
/// </summary>
1212
/// <typeparam name="T"></typeparam>
1313
/// <returns></returns>
@@ -21,7 +21,7 @@ public class LazyDocument : ILazyDocument
2121
public T As<T>() where T : class
2222
{
2323
var jToken = this._Value;
24-
return jToken != null ? jToken.ToObject<T>() : null;
24+
return jToken?.ToObject<T>();
2525
}
2626
}
27-
}
27+
}

src/Nest/CommonAbstractions/LazyDocument/LazyDocumentJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal class LazyDocumentJsonConverter : JsonConverter
99
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
1010
{
1111
var d = value as LazyDocument;
12-
if (d == null || d._Value == null)
12+
if (d?._Value == null)
1313
return;
1414
writer.WriteToken(d._Value.CreateReader());
1515
}
@@ -25,4 +25,4 @@ public override bool CanConvert(Type objectType)
2525
return true;
2626
}
2727
}
28-
}
28+
}

src/Nest/CommonAbstractions/SerializationBehavior/StatefulDeserialization/ConcreteTypeConverter.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static void PopulateHit(JsonSerializer serializer, JsonReader reader, ob
9292

9393
private static JObject CreateIntermediateJObject(JsonReader reader)
9494
{
95-
JObject jObject = JObject.Load(reader);
95+
var jObject = JObject.Load(reader);
9696
return jObject;
9797
}
9898

@@ -112,26 +112,29 @@ internal static Type GetConcreteTypeUsingSelector<T>(
112112
var baseType = realConcreteConverter._baseType;
113113
var selector = realConcreteConverter._concreteTypeSelector;
114114

115-
//Hit<dynamic> hitDynamic = new Hit<dynamic>();
116115
dynamic d = jObject;
117116
var fields = jObject["fields"];
118117
var fieldsDictionary = fields?.ToObject<IDictionary<string, object>>();
119118
var fieldValues = new FieldValues(settings.Inferrer, fieldsDictionary);
120-
var hitDynamic = new Hit<dynamic>();
121-
//favor manual mapping over doing Populate twice.
122-
hitDynamic.Fields = fieldValues;
123-
hitDynamic.Source = d._source;
124-
hitDynamic.Index = d._index;
125-
hitDynamic.Score = d._score != null ? (double)d._score : 0;
126-
hitDynamic.Type = d._type;
127-
hitDynamic.Version = d._version;
128-
hitDynamic.Id = d._id;
129-
hitDynamic.Parent = d._parent;
130-
hitDynamic.Routing = d._routing;
131-
hitDynamic.Sorts = d.sort;
132-
hitDynamic._Highlight = d.highlight is Dictionary<string, List<string>> ? d.highlight : null;
133-
hitDynamic.Explanation = d._explanation is Explanation ? d._explanation : null;
134-
object o = d._source ?? DynamicResponse.Create(fieldsDictionary) ?? new object {};
119+
var hitDynamic = new Hit<dynamic>
120+
{
121+
//favor manual mapping over doing Populate twice.
122+
Fields = fieldValues,
123+
Source = d._source,
124+
Index = d._index,
125+
Score = d._score != null ? (double)d._score : 0,
126+
Type = d._type,
127+
Version = d._version,
128+
Id = d._id,
129+
Parent = d._parent,
130+
Routing = d._routing,
131+
Sorts = d.sort,
132+
_Highlight = d.highlight is Dictionary<string, List<string>> ? d.highlight : null,
133+
Explanation = d._explanation is Explanation ? d._explanation : null,
134+
Nested = d._nested
135+
};
136+
137+
object o = d._source ?? DynamicResponse.Create(fieldsDictionary) ?? new object();
135138
var concreteType = selector(o, hitDynamic);
136139
return concreteType;
137140
}

src/Nest/Nest.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,7 @@
11431143
<Compile Include="Search\Search\Hits\HitsMetaData.cs" />
11441144
<Compile Include="Search\Search\Hits\InnerHitsMetaData.cs" />
11451145
<Compile Include="Search\Search\Hits\InnerHitsResult.cs" />
1146+
<Compile Include="Search\Search\Hits\NestedIdentity.cs" />
11461147
<Compile Include="Search\Search\InnerHits\GlobalInnerHit.cs" />
11471148
<Compile Include="Search\Search\InnerHits\InnerHits.cs" />
11481149
<Compile Include="Search\Search\InnerHits\InnerHitsContainer.cs" />

src/Nest/Search/Search/Hits/Hit.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,50 @@ public interface IHit<out T> where T : class
2929
public class Hit<T> : IHit<T>
3030
where T : class
3131
{
32-
[JsonProperty(PropertyName = "fields")]
32+
[JsonProperty("fields")]
3333
public FieldValues Fields { get; internal set; }
3434

35-
[JsonProperty(PropertyName = "_source")]
35+
[JsonProperty("_source")]
3636
public T Source { get; internal set; }
3737

38-
[JsonProperty(PropertyName = "_index")]
38+
[JsonProperty("_index")]
3939
public string Index { get; internal set; }
4040

41-
[JsonProperty(PropertyName = "inner_hits")]
41+
[JsonProperty("inner_hits")]
4242
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
4343
public IDictionary<string, InnerHitsResult> InnerHits { get; internal set; }
4444

45-
// TODO make this nullable for 5.0
46-
[JsonProperty(PropertyName = "_score")]
45+
[JsonProperty("_score")]
4746
public double Score { get; set; }
4847

49-
[JsonProperty(PropertyName = "_type")]
48+
[JsonProperty("_type")]
5049
public string Type { get; internal set; }
5150

52-
[JsonProperty(PropertyName = "_version")]
51+
[JsonProperty("_version")]
5352
public long? Version { get; internal set; }
5453

55-
[JsonProperty(PropertyName = "_id")]
54+
[JsonProperty("_id")]
5655
public string Id { get; internal set; }
5756

58-
[JsonProperty(PropertyName = "_parent")]
57+
[JsonProperty("_nested")]
58+
public NestedIdentity Nested { get; internal set; }
59+
60+
[JsonProperty("_parent")]
5961
public string Parent { get; internal set; }
6062

61-
[JsonProperty(PropertyName = "_routing")]
63+
[JsonProperty("_routing")]
6264
public string Routing { get; internal set; }
6365

64-
[JsonProperty(PropertyName = "_timestamp")]
66+
[JsonProperty("_timestamp")]
6567
public long? Timestamp { get; internal set; }
6668

67-
[JsonProperty(PropertyName = "_ttl")]
69+
[JsonProperty("_ttl")]
6870
public long? Ttl { get; internal set; }
6971

70-
[JsonProperty(PropertyName = "sort")]
72+
[JsonProperty("sort")]
7173
public IEnumerable<object> Sorts { get; internal set; }
7274

73-
[JsonProperty(PropertyName = "highlight")]
75+
[JsonProperty("highlight")]
7476
[JsonConverter(typeof(VerbatimDictionaryKeysJsonConverter))]
7577
internal Dictionary<string, List<string>> _Highlight { get; set; }
7678

@@ -92,10 +94,10 @@ public HighlightFieldDictionary Highlights
9294
}
9395
}
9496

95-
[JsonProperty(PropertyName = "_explanation")]
97+
[JsonProperty("_explanation")]
9698
public Explanation Explanation { get; internal set; }
9799

98-
[JsonProperty(PropertyName = "matched_queries")]
100+
[JsonProperty("matched_queries")]
99101
public IEnumerable<string> MatchedQueries { get; internal set; }
100102
}
101103
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Nest
4+
{
5+
[JsonObject]
6+
public class NestedIdentity
7+
{
8+
[JsonProperty("field")]
9+
public Field Field { get; internal set; }
10+
11+
[JsonProperty("offset")]
12+
public int Offset { get; internal set; }
13+
14+
[JsonProperty("_nested")]
15+
public NestedIdentity Nested { get; internal set; }
16+
}
17+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using Nest;
4+
using Tests.Framework;
5+
using Tests.Framework.Integration;
6+
using Tests.Framework.MockData;
7+
using Xunit;
8+
9+
namespace Tests.Reproduce
10+
{
11+
public class GithubIssue2323 : IClusterFixture<ReadOnlyCluster>
12+
{
13+
private readonly ReadOnlyCluster _cluster;
14+
15+
public GithubIssue2323(ReadOnlyCluster cluster)
16+
{
17+
_cluster = cluster;
18+
}
19+
20+
[I]
21+
public void NestedInnerHitsShouldIncludedNestedProperty()
22+
{
23+
var client = _cluster.Client;
24+
var response = client.Search<Project>(s => s
25+
.Query(q => q
26+
.Nested(n => n
27+
.Path(p => p.Tags)
28+
.Query(nq => nq
29+
.MatchAll()
30+
)
31+
.InnerHits(i => i
32+
.Source(false)
33+
)
34+
)
35+
)
36+
);
37+
38+
response.ShouldBeValid();
39+
40+
var innerHits = response.Hits.Select(h => h.InnerHits).ToList();
41+
42+
innerHits.Should().NotBeNullOrEmpty();
43+
44+
var innerHit = innerHits.First();
45+
innerHit.Should().ContainKey("tags");
46+
var hitMetadata = innerHit["tags"].Hits.Hits.First();
47+
48+
hitMetadata.Nested.Should().NotBeNull();
49+
hitMetadata.Nested.Field.Should().Be(Field.Create("tags"));
50+
hitMetadata.Nested.Offset.Should().BeGreaterOrEqualTo(0);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)