Skip to content

Commit 5d61ba8

Browse files
committed
Merge pull request #807 from elasticsearch/fix/sorting
Fix/sorting
2 parents fc512db + dbdafae commit 5d61ba8

File tree

14 files changed

+238
-129
lines changed

14 files changed

+238
-129
lines changed

Diff for: src/Nest/DSL/Search/SortFieldDescriptor.cs

+28-10
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,36 @@ public enum SortOrder
1818
Descending
1919
}
2020

21+
[JsonConverter(typeof(StringEnumConverter))]
22+
public enum SortMode
23+
{
24+
[EnumMember(Value = "min")]
25+
Min,
26+
[EnumMember(Value = "max")]
27+
Max,
28+
[EnumMember(Value = "sum")]
29+
Sum,
30+
[EnumMember(Value = "avg")]
31+
Average
32+
}
33+
2134
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
2235
public interface ISort
2336
{
24-
2537
[JsonProperty("missing")]
2638
string Missing { get; set; }
2739

2840
[JsonProperty("order")]
2941
SortOrder? Order { get; set; }
42+
43+
[JsonProperty("mode")]
44+
SortMode? Mode { get; set; }
3045
}
3146

3247
public interface IFieldSort : ISort
3348
{
3449
PropertyPathMarker Field { get; set; }
3550

36-
[JsonProperty("mode")]
37-
ScoreMode? Mode { get; set; }
38-
3951
[JsonProperty("nested_filter")]
4052
FilterContainer NestedFilter { get; set; }
4153

@@ -51,7 +63,7 @@ public class Sort : IFieldSort
5163
public PropertyPathMarker Field { get; set; }
5264
public string Missing { get; set; }
5365
public SortOrder? Order { get; set; }
54-
public ScoreMode? Mode { get; set; }
66+
public SortMode? Mode { get; set; }
5567
public FilterContainer NestedFilter { get; set; }
5668
public PropertyPathMarker NestedPath { get; set; }
5769
public bool? IgnoreUnmappedFields { get; set; }
@@ -67,7 +79,7 @@ public class SortFieldDescriptor<T> : IFieldSort where T : class
6779

6880
SortOrder? ISort.Order { get; set; }
6981

70-
ScoreMode? IFieldSort.Mode { get; set; }
82+
SortMode? ISort.Mode { get; set; }
7183

7284
FilterContainer IFieldSort.NestedFilter { get; set; }
7385

@@ -129,27 +141,33 @@ public virtual SortFieldDescriptor<T> Order(SortOrder order)
129141
return this;
130142
}
131143

144+
public virtual SortFieldDescriptor<T> Mode(SortMode mode)
145+
{
146+
Self.Mode = mode;
147+
return this;
148+
}
149+
132150
public virtual SortFieldDescriptor<T> NestedMin()
133151
{
134-
Self.Mode = ScoreMode.Min;
152+
Self.Mode = SortMode.Min;
135153
return this;
136154
}
137155

138156
public virtual SortFieldDescriptor<T> NestedMax()
139157
{
140-
Self.Mode = ScoreMode.Max;
158+
Self.Mode = SortMode.Max;
141159
return this;
142160
}
143161

144162
public virtual SortFieldDescriptor<T> NestedSum()
145163
{
146-
Self.Mode = ScoreMode.Sum;
164+
Self.Mode = SortMode.Sum;
147165
return this;
148166
}
149167

150168
public virtual SortFieldDescriptor<T> NestedAvg()
151169
{
152-
Self.Mode = ScoreMode.Average;
170+
Self.Mode = SortMode.Average;
153171
return this;
154172
}
155173

Diff for: src/Nest/DSL/Search/SortGeoDistanceDescriptor.cs

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GeoDistanceSort : IGeoDistanceSort
2020
{
2121
public string Missing { get; set; }
2222
public SortOrder? Order { get; set; }
23+
public SortMode? Mode { get; set; }
2324
public PropertyPathMarker Field { get; set; }
2425
public string PinLocation { get; set; }
2526
public GeoUnit? GeoUnit { get; set; }
@@ -30,6 +31,7 @@ object ICustomJson.GetCustomJson()
3031
{
3132
{ this.Field, this.PinLocation },
3233
{ "missing", this.Missing },
34+
{ "mode", this.Mode },
3335
{ "order", this.Order },
3436
{ "unit", this.GeoUnit }
3537
};
@@ -46,6 +48,8 @@ public class SortGeoDistanceDescriptor<T> : IGeoDistanceSort where T : class
4648

4749
SortOrder? ISort.Order { get; set; }
4850

51+
SortMode? ISort.Mode { get; set; }
52+
4953
string IGeoDistanceSort.PinLocation { get; set; }
5054

5155
GeoUnit? IGeoDistanceSort.GeoUnit { get; set; }
@@ -114,12 +118,19 @@ public SortGeoDistanceDescriptor<T> Order(SortOrder order)
114118
return this;
115119
}
116120

121+
public SortGeoDistanceDescriptor<T> Mode(SortMode mode)
122+
{
123+
Self.Mode = mode;
124+
return this;
125+
}
126+
117127
object ICustomJson.GetCustomJson()
118128
{
119129
return new Dictionary<object, object>
120130
{
121131
{ Self.Field, Self.PinLocation },
122132
{ "missing", Self.Missing },
133+
{ "mode", Self.Mode},
123134
{ "order", Self.Order },
124135
{ "unit", Self.GeoUnit }
125136
};

Diff for: src/Nest/DSL/Search/SortScriptDescriptor.cs

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class ScriptSort : IScriptSort
2222
{
2323
public string Missing { get; set; }
2424
public SortOrder? Order { get; set; }
25+
public SortMode? Mode { get; set; }
2526
public string Type { get; set; }
2627
public string Script { get; set; }
2728
public Dictionary<string, object> Params { get; set; }
@@ -35,6 +36,8 @@ public class SortScriptDescriptor<T> : IScriptSort
3536

3637
SortOrder? ISort.Order { get; set; }
3738

39+
SortMode? ISort.Mode { get; set; }
40+
3841
string IScriptSort.Type { get; set; }
3942

4043
string IScriptSort.Script { get; set; }
@@ -97,5 +100,11 @@ public SortScriptDescriptor<T> Order(SortOrder order)
97100
Self.Order = order;
98101
return this;
99102
}
103+
104+
public SortScriptDescriptor<T> Mode(SortMode mode)
105+
{
106+
Self.Mode = mode;
107+
return this;
108+
}
100109
}
101110
}

Diff for: 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

Diff for: 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" />
+42
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+
}

Diff for: 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": [

Diff for: 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")

0 commit comments

Comments
 (0)