Skip to content
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
28 changes: 28 additions & 0 deletions src/Nest/DSL/SortDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class SortDescriptor<T> where T : class
[JsonProperty("order")]
internal string _Order { get; set; }

[JsonProperty("mode")]
internal string _Mode { get; set; }

[JsonProperty("nested_filter")]
internal BaseFilter _NestedFilter { get; set; }

Expand Down Expand Up @@ -70,6 +73,31 @@ public virtual SortDescriptor<T> Descending()
this._Order = "desc";
return this;
}

public virtual SortDescriptor<T> NestedMin()
{
this._Mode = "min";
return this;
}

public virtual SortDescriptor<T> NestedMax()
{
this._Mode = "max";
return this;
}

public virtual SortDescriptor<T> NestedSum()
{
this._Mode = "sum";
return this;
}

public virtual SortDescriptor<T> NestedAvg()
{
this._Mode = "avg";
return this;
}

public virtual SortDescriptor<T> NestedFilter(Func<FilterDescriptor<T>, BaseFilter> filterSelector)
{
filterSelector.ThrowIfNull("filterSelector");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ElasticPropertyAttribute : Attribute, IElasticPropertyAttribute
public string Analyzer { get; set; }
public string IndexAnalyzer { get; set; }
public string SearchAnalyzer { get; set; }
public string SortAnalyzer { get; set; }
public string NullValue { get; set; }
public string Similarity { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public interface IElasticPropertyAttribute
string Analyzer { get; set; }
string IndexAnalyzer { get; set; }
string SearchAnalyzer { get; set; }
string SortAnalyzer { get; set; }
string NullValue { get; set; }

bool OmitNorms { get; set; }
Expand Down
13 changes: 11 additions & 2 deletions src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,17 @@ public void VisitBaseAttribute(IElasticPropertyAttribute att) {
this._jsonWriter.WritePropertyName("type");
this._jsonWriter.WriteValue(this._type);
}
this._jsonWriter.WritePropertyName("index");
this._jsonWriter.WriteValue(Enum.GetName(typeof (FieldIndexOption), FieldIndexOption.not_analyzed));
if (att.SortAnalyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("index");
this._jsonWriter.WriteValue(Enum.GetName(typeof(FieldIndexOption), FieldIndexOption.not_analyzed));
}
else
{
this._jsonWriter.WritePropertyName("index_analyzer");
this._jsonWriter.WriteValue(att.SortAnalyzer);
}

this._jsonWriter.WriteEnd();
this._jsonWriter.WriteEnd();
}
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,22 @@ public void IPProperty()
);
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
}

public class Foo
{
public int Id { get; set; }
[ElasticProperty(AddSortField = true, SortAnalyzer = "simple")]
public string Name { get; set; }
}

[Test]
public void SortAnalyzeryReadFromAttribute()
{
var result = _client.Map<Foo>(m => m.MapFromAttributes());
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());

}

[Test]
public void GeoPointProperty()
{
Expand All @@ -215,6 +231,7 @@ public void GeoPointProperty()
);
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
}

[Test]
public void GeoShapeProperty()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"foo": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "multi_field",
"fields": {
"name": {
"type": "string"
},
"sort": {
"type": "string",
"index_analyzer": "simple"
}
}
}
}
}
}
26 changes: 26 additions & 0 deletions src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,32 @@ public void TestSortOnSortField()
Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void TestSortOnNestedField()
{
var s = new SearchDescriptor<ElasticsearchProject>()
.From(0)
.Size(10)
.Sort(sort => sort
.OnField(e => e.Contributors.Suffix("age")) // Sort projects by oldest contributor
.NestedMax()
.Descending()
);
var json = TestElasticClient.Serialize(s);
var expected = @"
{
from: 0,
size: 10,
sort: {
""contributors.age"": {
""order"": ""desc"",
""mode"": ""max""
}
}
}";
Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void TestSortAscending()
{
Expand Down