Skip to content

Commit 4076ddc

Browse files
committed
Merge branch 'fix/5.x-multitermvectors' into 5.x
2 parents 5de59b4 + 27b7533 commit 4076ddc

File tree

6 files changed

+118
-11
lines changed

6 files changed

+118
-11
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using Newtonsoft.Json;
5+
6+
namespace Nest
7+
{
8+
/// <summary>
9+
/// Json converter that deserializes into an <see cref="ReadOnlyCollection{TInterface}"/> or <see cref="IReadOnlyCollection{TInterface}"/>
10+
/// where <typeparamref name="TInterface"/> does not have a custom deserializer and should be deserialized
11+
/// into concrete types of <typeparamref name="TDocument"/>
12+
/// </summary>
13+
/// <remarks>
14+
/// <typeparamref name="TInterface"/> may not have a deserializer for valid reasons, for example, an interface may be implemented by two
15+
/// concrete types that need to be deserialized. In this case, a deserializer would not know which concrete type to deserialize to in
16+
/// a given context.
17+
/// </remarks>
18+
/// <typeparam name="TDocument">The concrete type to deserialize</typeparam>
19+
/// <typeparam name="TInterface">The interface for the deserialized readonly collection</typeparam>
20+
internal class ReadOnlyCollectionJsonConverter<TDocument, TInterface> : JsonConverter
21+
where TDocument : TInterface
22+
where TInterface : class
23+
{
24+
public override bool CanWrite => false;
25+
public override bool CanRead => true;
26+
27+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
28+
{
29+
throw new NotSupportedException();
30+
}
31+
32+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
33+
{
34+
if (reader.TokenType != JsonToken.StartArray)
35+
return EmptyReadOnly<TInterface>.Collection;
36+
37+
var list = new List<TInterface>();
38+
reader.Read();
39+
while (reader.TokenType != JsonToken.EndArray)
40+
{
41+
var item = serializer.Deserialize<TDocument>(reader);
42+
list.Add(item);
43+
reader.Read();
44+
}
45+
46+
return new ReadOnlyCollection<TInterface>(list);
47+
}
48+
49+
public override bool CanConvert(Type objectType) => true;
50+
}
51+
}

src/Nest/Document/Multiple/MultiTermVectors/MultiTermVectorsResponse.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ namespace Nest
55
{
66
public interface IMultiTermVectorsResponse : IResponse
77
{
8-
IReadOnlyCollection<TermVectorsResponse> Documents { get; }
8+
IReadOnlyCollection<ITermVectors> Documents { get; }
99
}
1010

1111
[JsonObject]
1212
public class MultiTermVectorsResponse : ResponseBase, IMultiTermVectorsResponse
1313
{
1414
[JsonProperty("docs")]
15-
public IReadOnlyCollection<TermVectorsResponse> Documents { get; internal set; } = EmptyReadOnly<TermVectorsResponse>.Collection;
15+
[JsonConverter(typeof(ReadOnlyCollectionJsonConverter<TermVectorsResult, ITermVectors>))]
16+
public IReadOnlyCollection<ITermVectors> Documents { get; internal set; } = EmptyReadOnly<ITermVectors>.Collection;
1617
}
1718
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using Newtonsoft.Json;
3+
4+
namespace Nest
5+
{
6+
public interface ITermVectors
7+
{
8+
string Index { get; }
9+
string Type { get; }
10+
string Id { get; }
11+
long Version { get; }
12+
bool Found { get; }
13+
long Took { get; }
14+
IReadOnlyDictionary<string, TermVector> TermVectors { get; }
15+
}
16+
17+
public class TermVectorsResult : ITermVectors
18+
{
19+
[JsonProperty("_index")]
20+
public string Index { get; internal set; }
21+
22+
[JsonProperty("_type")]
23+
public string Type { get; internal set; }
24+
25+
[JsonProperty("_id")]
26+
public string Id { get; internal set; }
27+
28+
[JsonProperty("_version")]
29+
public long Version { get; internal set; }
30+
31+
[JsonProperty("found")]
32+
public bool Found { get; internal set; }
33+
34+
[JsonProperty("took")]
35+
public long Took { get; internal set; }
36+
37+
[JsonProperty("term_vectors")]
38+
public IReadOnlyDictionary<string, TermVector> TermVectors { get; internal set; } = EmptyReadOnly<string, TermVector>.Dictionary;
39+
}
40+
}

src/Nest/Document/Single/TermVectors/TermVectorsResponse.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,8 @@
33

44
namespace Nest
55
{
6-
public interface ITermVectorsResponse : IResponse
6+
public interface ITermVectorsResponse : IResponse, ITermVectors
77
{
8-
string Index { get; }
9-
string Type { get; }
10-
string Id { get; }
11-
long Version { get; }
12-
bool Found { get; }
13-
long Took { get; }
14-
IReadOnlyDictionary<string, TermVector> TermVectors { get; }
158
}
169

1710
[JsonObject]
@@ -32,7 +25,7 @@ public class TermVectorsResponse : ResponseBase, ITermVectorsResponse
3225
[JsonProperty("found")]
3326
public bool Found { get; internal set; }
3427

35-
[JsonProperty(PropertyName = "took")]
28+
[JsonProperty("took")]
3629
public long Took { get; internal set; }
3730

3831
[JsonProperty("term_vectors")]

src/Nest/Nest.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@
471471
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\JsonConverterBase.cs" />
472472
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\KeyValueJsonConverter.cs" />
473473
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\ReadAsTypeJsonConverter.cs" />
474+
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\ReadOnlyCollectionJsonConverter.cs" />
474475
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\ReserializeJsonConverter.cs" />
475476
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\ServerErrorJsonConverter.cs" />
476477
<Compile Include="CommonAbstractions\SerializationBehavior\GenericJsonConverters\ReadSingleOrEnumerableJsonConverter.cs" />
@@ -610,6 +611,7 @@
610611
<Compile Include="Document\Single\Source\SourceRequest.cs" />
611612
<Compile Include="Document\Single\TermVectors\ElasticClient-TermVectors.cs" />
612613
<Compile Include="Document\Single\TermVectors\FieldStatistics.cs" />
614+
<Compile Include="Document\Single\TermVectors\TermVectors.cs" />
613615
<Compile Include="Document\Single\TermVectors\TermVector.cs" />
614616
<Compile Include="Document\Single\TermVectors\TermVectorFilter.cs" />
615617
<Compile Include="Document\Single\TermVectors\TermVectorsRequest.cs" />

src/Tests/Document/Single/TermVectors/TermVectorsApiTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Elasticsearch.Net;
3+
using FluentAssertions;
34
using Nest;
45
using Tests.Framework;
56
using Tests.Framework.Integration;
@@ -70,5 +71,24 @@ protected override LazyResponses ClientUsage() => Calls(
7071
MaximumWordLength = 200
7172
}
7273
};
74+
75+
protected override void ExpectResponse(ITermVectorsResponse response)
76+
{
77+
response.ShouldBeValid();
78+
79+
response.TermVectors.Should().NotBeEmpty();
80+
response.Found.Should().BeTrue();
81+
response.Version.Should().Be(1);
82+
response.Id.Should().NotBeNullOrEmpty();
83+
response.Index.Should().NotBeNullOrEmpty();
84+
response.Type.Should().NotBeNullOrEmpty();
85+
86+
foreach (var termVector in response.TermVectors)
87+
{
88+
termVector.Key.Should().NotBeNullOrEmpty();
89+
termVector.Value.FieldStatistics.Should().NotBeNull();
90+
termVector.Value.Terms.Should().NotBeEmpty();
91+
}
92+
}
7393
}
7494
}

0 commit comments

Comments
 (0)