Skip to content

Commit dbdafae

Browse files
committed
Serialize sorts as array instead of dictionary
1 parent cfe580f commit dbdafae

File tree

11 files changed

+187
-122
lines changed

11 files changed

+187
-122
lines changed

src/Nest/DSL/SearchDescriptor.cs

+19-19
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public interface ISearchRequest : IQueryPath<SearchRequestParameters>
4141
IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }
4242

4343
[JsonProperty(PropertyName = "sort")]
44-
[JsonConverter(typeof (DictionaryKeysAreNotPropertyNamesJsonConverter))]
45-
IDictionary<PropertyPathMarker, ISort> Sort { get; set; }
44+
[JsonConverter(typeof(SortCollectionConverter))]
45+
IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
4646

4747
[JsonProperty(PropertyName = "facets")]
4848
[JsonConverter(typeof (DictionaryKeysAreNotPropertyNamesJsonConverter))]
@@ -140,7 +140,7 @@ public partial class SearchRequest : QueryPathBase<SearchRequestParameters>, ISe
140140
public IList<PropertyPathMarker> Fields { get; set; }
141141
public IDictionary<string, IScriptFilter> ScriptFields { get; set; }
142142
public ISourceFilter Source { get; set; }
143-
public IDictionary<PropertyPathMarker, ISort> Sort { get; set; }
143+
public IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
144144
public IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }
145145
public IFilterContainer Filter { get; set; }
146146
public IQueryContainer Query { get; set; }
@@ -200,7 +200,7 @@ protected override void UpdatePathInfo(IConnectionSettingsValues settings, Elast
200200
public bool? TrackScores { get; set; }
201201
public double? MinScore { get; set; }
202202
public IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }
203-
public IDictionary<PropertyPathMarker, ISort> Sort { get; set; }
203+
public IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
204204
public IDictionary<PropertyPathMarker, IFacetContainer> Facets { get; set; }
205205
public IDictionary<string, ISuggestBucket> Suggest { get; set; }
206206
public IHighlightRequest Highlight { get; set; }
@@ -290,7 +290,7 @@ string ISearchRequest.Routing
290290

291291
IDictionary<IndexNameMarker, double> ISearchRequest.IndicesBoost { get; set; }
292292

293-
IDictionary<PropertyPathMarker, ISort> ISearchRequest.Sort { get; set; }
293+
IList<KeyValuePair<PropertyPathMarker, ISort>> ISearchRequest.Sort { get; set; }
294294

295295
IDictionary<PropertyPathMarker, IFacetContainer> ISearchRequest.Facets { get; set; }
296296

@@ -568,9 +568,9 @@ public SearchDescriptor<T> ScriptFields(
568568
/// </summary>
569569
public SearchDescriptor<T> SortAscending(Expression<Func<T, object>> objectPath)
570570
{
571-
if (Self.Sort == null) Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
571+
if (Self.Sort == null) Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
572572

573-
Self.Sort.Add(objectPath, new Sort() { Order = SortOrder.Ascending});
573+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(objectPath, new Sort() { Order = SortOrder.Ascending}));
574574
return this;
575575
}
576576

@@ -584,9 +584,9 @@ public SearchDescriptor<T> SortAscending(Expression<Func<T, object>> objectPath)
584584
/// </summary>
585585
public SearchDescriptor<T> SortDescending(Expression<Func<T, object>> objectPath)
586586
{
587-
if (Self.Sort == null) Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
587+
if (Self.Sort == null) Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
588588

589-
Self.Sort.Add(objectPath, new Sort() { Order = SortOrder.Descending});
589+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(objectPath, new Sort() { Order = SortOrder.Descending }));
590590
return this;
591591
}
592592

@@ -600,8 +600,8 @@ public SearchDescriptor<T> SortDescending(Expression<Func<T, object>> objectPath
600600
/// </summary>
601601
public SearchDescriptor<T> SortAscending(string field)
602602
{
603-
if (Self.Sort == null) Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
604-
Self.Sort.Add(field, new Sort() { Order = SortOrder.Ascending });
603+
if (Self.Sort == null) Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
604+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(field, new Sort() { Order = SortOrder.Ascending }));
605605
return this;
606606
}
607607

@@ -616,9 +616,9 @@ public SearchDescriptor<T> SortAscending(string field)
616616
public SearchDescriptor<T> SortDescending(string field)
617617
{
618618
if (Self.Sort == null)
619-
Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
619+
Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
620620

621-
Self.Sort.Add(field, new Sort() { Order = SortOrder.Descending});
621+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(field, new Sort() { Order = SortOrder.Descending}));
622622
return this;
623623
}
624624

@@ -629,11 +629,11 @@ public SearchDescriptor<T> SortDescending(string field)
629629
public SearchDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector)
630630
{
631631
if (Self.Sort == null)
632-
Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
632+
Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
633633

634634
sortSelector.ThrowIfNull("sortSelector");
635635
var descriptor = sortSelector(new SortFieldDescriptor<T>());
636-
Self.Sort.Add(descriptor.Field, descriptor);
636+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(descriptor.Field, descriptor));
637637
return this;
638638
}
639639

@@ -644,11 +644,11 @@ public SearchDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSel
644644
public SearchDescriptor<T> SortGeoDistance(Func<SortGeoDistanceDescriptor<T>, IGeoDistanceSort> sortSelector)
645645
{
646646
if (Self.Sort == null)
647-
Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
647+
Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
648648

649649
sortSelector.ThrowIfNull("sortSelector");
650650
var descriptor = sortSelector(new SortGeoDistanceDescriptor<T>());
651-
Self.Sort.Add("_geo_distance", descriptor);
651+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>("_geo_distance", descriptor));
652652
return this;
653653
}
654654

@@ -659,11 +659,11 @@ public SearchDescriptor<T> SortGeoDistance(Func<SortGeoDistanceDescriptor<T>, IG
659659
public SearchDescriptor<T> SortScript(Func<SortScriptDescriptor<T>, IScriptSort> sortSelector)
660660
{
661661
if (Self.Sort == null)
662-
Self.Sort = new Dictionary<PropertyPathMarker, ISort>();
662+
Self.Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>();
663663

664664
sortSelector.ThrowIfNull("sortSelector");
665665
var descriptor = sortSelector(new SortScriptDescriptor<T>());
666-
Self.Sort.Add("_script", descriptor);
666+
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>("_script", descriptor));
667667
return this;
668668
}
669669

src/Nest/Nest.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@
791791
<Compile Include="Resolvers\Converters\FieldNameQueryConverter.cs" />
792792
<Compile Include="Resolvers\Converters\IndexSettingsResponseConverter.cs" />
793793
<Compile Include="Resolvers\Converters\SimilaritySettingsConverter.cs" />
794+
<Compile Include="Resolvers\Converters\SortCollectionConverter.cs" />
794795
<Compile Include="Resolvers\Converters\SuggestResponseConverter.cs" />
795796
<Compile Include="Resolvers\Converters\PropertyPathMarkerConverter.cs" />
796797
<Compile Include="Resolvers\Converters\DynamicMappingOptionConverter.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest.Resolvers.Converters
8+
{
9+
public class SortCollectionConverter : JsonConverter
10+
{
11+
public override bool CanConvert(Type objectType)
12+
{
13+
return typeof(IList<KeyValuePair<PropertyPathMarker, ISort>>).IsAssignableFrom(objectType);
14+
}
15+
16+
public override bool CanRead
17+
{
18+
get { return false; }
19+
}
20+
21+
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
22+
{
23+
return new InvalidOperationException();
24+
}
25+
26+
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
27+
{
28+
writer.WriteStartArray();
29+
var sortItems = value as IList<KeyValuePair<PropertyPathMarker, ISort>>;
30+
foreach (var item in sortItems)
31+
{
32+
writer.WriteStartObject();
33+
var contract = serializer.ContractResolver as SettingsContractResolver;
34+
var fieldName = contract.Infer.PropertyPath(item.Key);
35+
writer.WritePropertyName(fieldName);
36+
serializer.Serialize(writer, item.Value);
37+
writer.WriteEndObject();
38+
}
39+
writer.WriteEndArray();
40+
}
41+
}
42+
}

src/Tests/Nest.Tests.Unit/ObjectInitializer/MoreLikeThis/MoreLikeThisBody.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
{
1+
{
22
"from": 0,
33
"size": 20,
44
"explain": true,
55
"track_scores": true,
6-
"sort": {
7-
"field": {
8-
"missing": "_first",
9-
"order": "asc"
6+
"sort": [
7+
{
8+
"field": {
9+
"missing": "_first",
10+
"order": "asc"
11+
}
1012
}
11-
},
13+
],
1214
"filter": {
1315
"bool": {
1416
"must": [

src/Tests/Nest.Tests.Unit/ObjectInitializer/MoreLikeThis/MoreLikeThisRequestTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public MoreLikeThisRequestTests()
4242
}),
4343
TrackScores = true,
4444
Explain = true,
45-
Sort = new Dictionary<PropertyPathMarker, ISort>()
45+
Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
4646
{
47-
{ "field", new Sort { Order = SortOrder.Ascending, Missing = "_first"}}
47+
new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
4848
}
4949
};
5050
var request = new MoreLikeThisRequest("some-index", "the-type","document-id-21")

src/Tests/Nest.Tests.Unit/ObjectInitializer/Search/SearchBody.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{
1+
{
22
"from": 0,
33
"size": 20,
44
"explain": true,
@@ -7,12 +7,14 @@
77
"indices_boost": {
88
"nest_test_data": 2.3
99
},
10-
"sort": {
11-
"field": {
12-
"missing": "_first",
13-
"order": "asc"
10+
"sort": [
11+
{
12+
"field": {
13+
"missing": "_first",
14+
"order": "asc"
15+
}
1416
}
15-
},
17+
],
1618
"facets": {
1719
"name": {
1820
"terms": {

src/Tests/Nest.Tests.Unit/ObjectInitializer/Search/SearchRequestTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public SearchRequestTests()
3939
{
4040
{ Infer.Index<ElasticsearchProject>(), 2.3 }
4141
},
42-
Sort = new Dictionary<PropertyPathMarker, ISort>()
42+
Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
4343
{
44-
{ "field", new Sort { Order = SortOrder.Ascending, Missing = "_first"}}
44+
new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
4545
},
4646
Facets = new Dictionary<PropertyPathMarker, IFacetContainer>()
4747
{

src/Tests/Nest.Tests.Unit/Search/InitializerSyntax/InitializerExample.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ public void FullExample_InitializerSyntax_Search()
6969
{"multiplier", 4}
7070
}
7171
}),
72-
Sort = new Dictionary<PropertyPathMarker, ISort>()
72+
Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
7373
{
74-
{ "field", new Sort { Order = SortOrder.Ascending, Missing = "_first"}}
74+
new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
7575
},
7676
Source = new SourceFilter
7777
{

src/Tests/Nest.Tests.Unit/Search/SearchOptions/SearchOptionTests.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ public void TestSort()
218218
.SortAscending(e => e.LOC.Suffix("sort"));
219219
var json = TestElasticClient.Serialize(s);
220220
var expected = @"{ from: 0, size: 10,
221-
sort: {
222-
""loc.sort"": { order: ""asc"" }
223-
},
221+
sort: [
222+
{""loc.sort"": { order: ""asc"" }}
223+
],
224224
fields: [""id"", ""name""]
225225
}";
226226
Assert.True(json.JsonEquals(expected), json);
@@ -236,10 +236,10 @@ public void TestSortDescending()
236236
.SortDescending(e => e.Name.Suffix("sort"));
237237
var json = TestElasticClient.Serialize(s);
238238
var expected = @"{ from: 0, size: 10,
239-
sort: {
240-
""loc.sort"": { order: ""asc"" },
241-
""name.sort"": { order: ""desc"" }
242-
},
239+
sort: [
240+
{""loc.sort"": { order: ""asc"" }},
241+
{""name.sort"": { order: ""desc"" }}
242+
],
243243
fields: [""id"", ""name""]
244244
}";
245245
Assert.True(json.JsonEquals(expected), json);
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
{
1+
{
22
"from": 0,
33
"size": 10,
4-
"sort": {
5-
"_script": {
6-
"type": "number",
7-
"script": "(doc['_id'].stringValue + salt).hashCode()",
8-
"params": {
9-
"salt": "some_random_string"
10-
},
11-
"order": "asc"
4+
"sort": [
5+
{
6+
"_script": {
7+
"type": "number",
8+
"script": "(doc['_id'].stringValue + salt).hashCode()",
9+
"params": {
10+
"salt": "some_random_string"
11+
},
12+
"order": "asc"
13+
}
1214
}
13-
}
14-
}
15+
]
16+
}

0 commit comments

Comments
 (0)