diff --git a/src/Nest/DSL/SortDescriptor.cs b/src/Nest/DSL/SortDescriptor.cs index 663e0ac58b3..06946b9493f 100644 --- a/src/Nest/DSL/SortDescriptor.cs +++ b/src/Nest/DSL/SortDescriptor.cs @@ -19,6 +19,9 @@ public class SortDescriptor 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; } @@ -70,6 +73,31 @@ public virtual SortDescriptor Descending() this._Order = "desc"; return this; } + + public virtual SortDescriptor NestedMin() + { + this._Mode = "min"; + return this; + } + + public virtual SortDescriptor NestedMax() + { + this._Mode = "max"; + return this; + } + + public virtual SortDescriptor NestedSum() + { + this._Mode = "sum"; + return this; + } + + public virtual SortDescriptor NestedAvg() + { + this._Mode = "avg"; + return this; + } + public virtual SortDescriptor NestedFilter(Func, BaseFilter> filterSelector) { filterSelector.ThrowIfNull("filterSelector"); diff --git a/src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs b/src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs index 42bc6297542..42ce5ad160c 100644 --- a/src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs +++ b/src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs @@ -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; } diff --git a/src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs b/src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs index 836368772e8..0cff2759418 100644 --- a/src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs +++ b/src/Nest/Domain/Mapping/Attributes/IElasticPropertyAttribute.cs @@ -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; } diff --git a/src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs b/src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs index c08d7eeba9e..c5d805ac182 100644 --- a/src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs +++ b/src/Nest/Resolvers/Writers/WritePropertiesFromAttributeVisitor.cs @@ -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(); } diff --git a/src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs b/src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs index 1835f641c67..de7ff2dd534 100644 --- a/src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs +++ b/src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs @@ -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(m => m.MapFromAttributes()); + this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod()); + + } + [Test] public void GeoPointProperty() { @@ -215,6 +231,7 @@ public void GeoPointProperty() ); this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod()); } + [Test] public void GeoShapeProperty() { diff --git a/src/Tests/Nest.Tests.Unit/Core/Map/Properties/SortAnalyzeryReadFromAttribute.json b/src/Tests/Nest.Tests.Unit/Core/Map/Properties/SortAnalyzeryReadFromAttribute.json new file mode 100644 index 00000000000..74227dab5a4 --- /dev/null +++ b/src/Tests/Nest.Tests.Unit/Core/Map/Properties/SortAnalyzeryReadFromAttribute.json @@ -0,0 +1,21 @@ +{ + "foo": { + "properties": { + "id": { + "type": "integer" + }, + "name": { + "type": "multi_field", + "fields": { + "name": { + "type": "string" + }, + "sort": { + "type": "string", + "index_analyzer": "simple" + } + } + } + } + } +} \ No newline at end of file diff --git a/src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs b/src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs index 076fd7ee388..26ab993c176 100644 --- a/src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs +++ b/src/Tests/Nest.Tests.Unit/Search/Sort/SortTests.cs @@ -61,6 +61,32 @@ public void TestSortOnSortField() Assert.True(json.JsonEquals(expected), json); } + [Test] + public void TestSortOnNestedField() + { + var s = new SearchDescriptor() + .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() {