Skip to content

Commit 26b7e80

Browse files
committed
Merge pull request #530 from Grastveit/sort
Some additional sort-options
2 parents 8de7b63 + 512e6c1 commit 26b7e80

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

Diff for: src/Nest/DSL/SortDescriptor.cs

+28
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class SortDescriptor<T> where T : class
1919
[JsonProperty("order")]
2020
internal string _Order { get; set; }
2121

22+
[JsonProperty("mode")]
23+
internal string _Mode { get; set; }
24+
2225
[JsonProperty("nested_filter")]
2326
internal BaseFilter _NestedFilter { get; set; }
2427

@@ -70,6 +73,31 @@ public virtual SortDescriptor<T> Descending()
7073
this._Order = "desc";
7174
return this;
7275
}
76+
77+
public virtual SortDescriptor<T> NestedMin()
78+
{
79+
this._Mode = "min";
80+
return this;
81+
}
82+
83+
public virtual SortDescriptor<T> NestedMax()
84+
{
85+
this._Mode = "max";
86+
return this;
87+
}
88+
89+
public virtual SortDescriptor<T> NestedSum()
90+
{
91+
this._Mode = "sum";
92+
return this;
93+
}
94+
95+
public virtual SortDescriptor<T> NestedAvg()
96+
{
97+
this._Mode = "avg";
98+
return this;
99+
}
100+
73101
public virtual SortDescriptor<T> NestedFilter(Func<FilterDescriptor<T>, BaseFilter> filterSelector)
74102
{
75103
filterSelector.ThrowIfNull("filterSelector");

Diff for: src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class ElasticPropertyAttribute : Attribute, IElasticPropertyAttribute
2121
public string Analyzer { get; set; }
2222
public string IndexAnalyzer { get; set; }
2323
public string SearchAnalyzer { get; set; }
24+
public string SortAnalyzer { get; set; }
2425
public string NullValue { get; set; }
2526
public string Similarity { get; set; }
2627

Diff for: src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface IElasticPropertyAttribute
1616
string Analyzer { get; set; }
1717
string IndexAnalyzer { get; set; }
1818
string SearchAnalyzer { get; set; }
19+
string SortAnalyzer { get; set; }
1920
string NullValue { get; set; }
2021

2122
bool OmitNorms { get; set; }

Diff for: src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,17 @@ public void VisitBaseAttribute(IElasticPropertyAttribute att) {
131131
this._jsonWriter.WritePropertyName("type");
132132
this._jsonWriter.WriteValue(this._type);
133133
}
134-
this._jsonWriter.WritePropertyName("index");
135-
this._jsonWriter.WriteValue(Enum.GetName(typeof (FieldIndexOption), FieldIndexOption.not_analyzed));
134+
if (att.SortAnalyzer.IsNullOrEmpty())
135+
{
136+
this._jsonWriter.WritePropertyName("index");
137+
this._jsonWriter.WriteValue(Enum.GetName(typeof(FieldIndexOption), FieldIndexOption.not_analyzed));
138+
}
139+
else
140+
{
141+
this._jsonWriter.WritePropertyName("index_analyzer");
142+
this._jsonWriter.WriteValue(att.SortAnalyzer);
143+
}
144+
136145
this._jsonWriter.WriteEnd();
137146
this._jsonWriter.WriteEnd();
138147
}

Diff for: src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,22 @@ public void IPProperty()
200200
);
201201
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
202202
}
203+
204+
public class Foo
205+
{
206+
public int Id { get; set; }
207+
[ElasticProperty(AddSortField = true, SortAnalyzer = "simple")]
208+
public string Name { get; set; }
209+
}
210+
211+
[Test]
212+
public void SortAnalyzeryReadFromAttribute()
213+
{
214+
var result = _client.Map<Foo>(m => m.MapFromAttributes());
215+
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
216+
217+
}
218+
203219
[Test]
204220
public void GeoPointProperty()
205221
{
@@ -215,6 +231,7 @@ public void GeoPointProperty()
215231
);
216232
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
217233
}
234+
218235
[Test]
219236
public void GeoShapeProperty()
220237
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"foo": {
3+
"properties": {
4+
"id": {
5+
"type": "integer"
6+
},
7+
"name": {
8+
"type": "multi_field",
9+
"fields": {
10+
"name": {
11+
"type": "string"
12+
},
13+
"sort": {
14+
"type": "string",
15+
"index_analyzer": "simple"
16+
}
17+
}
18+
}
19+
}
20+
}
21+
}

Diff for: src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs

+26
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,32 @@ public void TestSortOnSortField()
6161
Assert.True(json.JsonEquals(expected), json);
6262
}
6363

64+
[Test]
65+
public void TestSortOnNestedField()
66+
{
67+
var s = new SearchDescriptor<ElasticsearchProject>()
68+
.From(0)
69+
.Size(10)
70+
.Sort(sort => sort
71+
.OnField(e => e.Contributors.Suffix("age")) // Sort projects by oldest contributor
72+
.NestedMax()
73+
.Descending()
74+
);
75+
var json = TestElasticClient.Serialize(s);
76+
var expected = @"
77+
{
78+
from: 0,
79+
size: 10,
80+
sort: {
81+
""contributors.age"": {
82+
""order"": ""desc"",
83+
""mode"": ""max""
84+
}
85+
}
86+
}";
87+
Assert.True(json.JsonEquals(expected), json);
88+
}
89+
6490
[Test]
6591
public void TestSortAscending()
6692
{

0 commit comments

Comments
 (0)