Skip to content

Fix/sorting #807

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

Merged
merged 2 commits into from
Jul 18, 2014
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
38 changes: 28 additions & 10 deletions src/Nest/DSL/Search/SortFieldDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,36 @@ public enum SortOrder
Descending
}

[JsonConverter(typeof(StringEnumConverter))]
public enum SortMode
{
[EnumMember(Value = "min")]
Min,
[EnumMember(Value = "max")]
Max,
[EnumMember(Value = "sum")]
Sum,
[EnumMember(Value = "avg")]
Average
}

[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
public interface ISort
{

[JsonProperty("missing")]
string Missing { get; set; }

[JsonProperty("order")]
SortOrder? Order { get; set; }

[JsonProperty("mode")]
SortMode? Mode { get; set; }
}

public interface IFieldSort : ISort
{
PropertyPathMarker Field { get; set; }

[JsonProperty("mode")]
ScoreMode? Mode { get; set; }

[JsonProperty("nested_filter")]
FilterContainer NestedFilter { get; set; }

Expand All @@ -51,7 +63,7 @@ public class Sort : IFieldSort
public PropertyPathMarker Field { get; set; }
public string Missing { get; set; }
public SortOrder? Order { get; set; }
public ScoreMode? Mode { get; set; }
public SortMode? Mode { get; set; }
public FilterContainer NestedFilter { get; set; }
public PropertyPathMarker NestedPath { get; set; }
public bool? IgnoreUnmappedFields { get; set; }
Expand All @@ -67,7 +79,7 @@ public class SortFieldDescriptor<T> : IFieldSort where T : class

SortOrder? ISort.Order { get; set; }

ScoreMode? IFieldSort.Mode { get; set; }
SortMode? ISort.Mode { get; set; }

FilterContainer IFieldSort.NestedFilter { get; set; }

Expand Down Expand Up @@ -129,27 +141,33 @@ public virtual SortFieldDescriptor<T> Order(SortOrder order)
return this;
}

public virtual SortFieldDescriptor<T> Mode(SortMode mode)
{
Self.Mode = mode;
return this;
}

public virtual SortFieldDescriptor<T> NestedMin()
{
Self.Mode = ScoreMode.Min;
Self.Mode = SortMode.Min;
return this;
}

public virtual SortFieldDescriptor<T> NestedMax()
{
Self.Mode = ScoreMode.Max;
Self.Mode = SortMode.Max;
return this;
}

public virtual SortFieldDescriptor<T> NestedSum()
{
Self.Mode = ScoreMode.Sum;
Self.Mode = SortMode.Sum;
return this;
}

public virtual SortFieldDescriptor<T> NestedAvg()
{
Self.Mode = ScoreMode.Average;
Self.Mode = SortMode.Average;
return this;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Nest/DSL/Search/SortGeoDistanceDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class GeoDistanceSort : IGeoDistanceSort
{
public string Missing { get; set; }
public SortOrder? Order { get; set; }
public SortMode? Mode { get; set; }
public PropertyPathMarker Field { get; set; }
public string PinLocation { get; set; }
public GeoUnit? GeoUnit { get; set; }
Expand All @@ -30,6 +31,7 @@ object ICustomJson.GetCustomJson()
{
{ this.Field, this.PinLocation },
{ "missing", this.Missing },
{ "mode", this.Mode },
{ "order", this.Order },
{ "unit", this.GeoUnit }
};
Expand All @@ -46,6 +48,8 @@ public class SortGeoDistanceDescriptor<T> : IGeoDistanceSort where T : class

SortOrder? ISort.Order { get; set; }

SortMode? ISort.Mode { get; set; }

string IGeoDistanceSort.PinLocation { get; set; }

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

public SortGeoDistanceDescriptor<T> Mode(SortMode mode)
{
Self.Mode = mode;
return this;
}

object ICustomJson.GetCustomJson()
{
return new Dictionary<object, object>
{
{ Self.Field, Self.PinLocation },
{ "missing", Self.Missing },
{ "mode", Self.Mode},
{ "order", Self.Order },
{ "unit", Self.GeoUnit }
};
Expand Down
9 changes: 9 additions & 0 deletions src/Nest/DSL/Search/SortScriptDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ScriptSort : IScriptSort
{
public string Missing { get; set; }
public SortOrder? Order { get; set; }
public SortMode? Mode { get; set; }
public string Type { get; set; }
public string Script { get; set; }
public Dictionary<string, object> Params { get; set; }
Expand All @@ -35,6 +36,8 @@ public class SortScriptDescriptor<T> : IScriptSort

SortOrder? ISort.Order { get; set; }

SortMode? ISort.Mode { get; set; }

string IScriptSort.Type { get; set; }

string IScriptSort.Script { get; set; }
Expand Down Expand Up @@ -97,5 +100,11 @@ public SortScriptDescriptor<T> Order(SortOrder order)
Self.Order = order;
return this;
}

public SortScriptDescriptor<T> Mode(SortMode mode)
{
Self.Mode = mode;
return this;
}
}
}
38 changes: 19 additions & 19 deletions src/Nest/DSL/SearchDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public interface ISearchRequest : IQueryPath<SearchRequestParameters>
IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }

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

[JsonProperty(PropertyName = "facets")]
[JsonConverter(typeof (DictionaryKeysAreNotPropertyNamesJsonConverter))]
Expand Down Expand Up @@ -140,7 +140,7 @@ public partial class SearchRequest : QueryPathBase<SearchRequestParameters>, ISe
public IList<PropertyPathMarker> Fields { get; set; }
public IDictionary<string, IScriptFilter> ScriptFields { get; set; }
public ISourceFilter Source { get; set; }
public IDictionary<PropertyPathMarker, ISort> Sort { get; set; }
public IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
public IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }
public IFilterContainer Filter { get; set; }
public IQueryContainer Query { get; set; }
Expand Down Expand Up @@ -200,7 +200,7 @@ protected override void UpdatePathInfo(IConnectionSettingsValues settings, Elast
public bool? TrackScores { get; set; }
public double? MinScore { get; set; }
public IDictionary<IndexNameMarker, double> IndicesBoost { get; set; }
public IDictionary<PropertyPathMarker, ISort> Sort { get; set; }
public IList<KeyValuePair<PropertyPathMarker, ISort>> Sort { get; set; }
public IDictionary<PropertyPathMarker, IFacetContainer> Facets { get; set; }
public IDictionary<string, ISuggestBucket> Suggest { get; set; }
public IHighlightRequest Highlight { get; set; }
Expand Down Expand Up @@ -290,7 +290,7 @@ string ISearchRequest.Routing

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

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

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

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

Self.Sort.Add(objectPath, new Sort() { Order = SortOrder.Ascending});
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(objectPath, new Sort() { Order = SortOrder.Ascending}));
return this;
}

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

Self.Sort.Add(objectPath, new Sort() { Order = SortOrder.Descending});
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(objectPath, new Sort() { Order = SortOrder.Descending }));
return this;
}

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

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

Self.Sort.Add(field, new Sort() { Order = SortOrder.Descending});
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(field, new Sort() { Order = SortOrder.Descending}));
return this;
}

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

sortSelector.ThrowIfNull("sortSelector");
var descriptor = sortSelector(new SortFieldDescriptor<T>());
Self.Sort.Add(descriptor.Field, descriptor);
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>(descriptor.Field, descriptor));
return this;
}

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

sortSelector.ThrowIfNull("sortSelector");
var descriptor = sortSelector(new SortGeoDistanceDescriptor<T>());
Self.Sort.Add("_geo_distance", descriptor);
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>("_geo_distance", descriptor));
return this;
}

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

sortSelector.ThrowIfNull("sortSelector");
var descriptor = sortSelector(new SortScriptDescriptor<T>());
Self.Sort.Add("_script", descriptor);
Self.Sort.Add(new KeyValuePair<PropertyPathMarker, ISort>("_script", descriptor));
return this;
}

Expand Down
1 change: 1 addition & 0 deletions src/Nest/Nest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@
<Compile Include="Resolvers\Converters\FieldNameQueryConverter.cs" />
<Compile Include="Resolvers\Converters\IndexSettingsResponseConverter.cs" />
<Compile Include="Resolvers\Converters\SimilaritySettingsConverter.cs" />
<Compile Include="Resolvers\Converters\SortCollectionConverter.cs" />
<Compile Include="Resolvers\Converters\SuggestResponseConverter.cs" />
<Compile Include="Resolvers\Converters\PropertyPathMarkerConverter.cs" />
<Compile Include="Resolvers\Converters\DynamicMappingOptionConverter.cs" />
Expand Down
42 changes: 42 additions & 0 deletions src/Nest/Resolvers/Converters/SortCollectionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nest.Resolvers.Converters
{
public class SortCollectionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(IList<KeyValuePair<PropertyPathMarker, ISort>>).IsAssignableFrom(objectType);
}

public override bool CanRead
{
get { return false; }
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return new InvalidOperationException();
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteStartArray();
var sortItems = value as IList<KeyValuePair<PropertyPathMarker, ISort>>;
foreach (var item in sortItems)
{
writer.WriteStartObject();
var contract = serializer.ContractResolver as SettingsContractResolver;
var fieldName = contract.Infer.PropertyPath(item.Key);
writer.WritePropertyName(fieldName);
serializer.Serialize(writer, item.Value);
writer.WriteEndObject();
}
writer.WriteEndArray();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
{
"from": 0,
"size": 20,
"explain": true,
"track_scores": true,
"sort": {
"field": {
"missing": "_first",
"order": "asc"
"sort": [
{
"field": {
"missing": "_first",
"order": "asc"
}
}
},
],
"filter": {
"bool": {
"must": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public MoreLikeThisRequestTests()
}),
TrackScores = true,
Explain = true,
Sort = new Dictionary<PropertyPathMarker, ISort>()
Sort = new List<KeyValuePair<PropertyPathMarker, ISort>>()
{
{ "field", new Sort { Order = SortOrder.Ascending, Missing = "_first"}}
new KeyValuePair<PropertyPathMarker, ISort>("field", new Sort { Order = SortOrder.Ascending, Missing = "_first"})
}
};
var request = new MoreLikeThisRequest("some-index", "the-type","document-id-21")
Expand Down
Loading