diff --git a/src/Nest/DSL/Search/SortScriptDescriptor.cs b/src/Nest/DSL/Search/SortScriptDescriptor.cs index ac3d2be216b..1f343332bff 100644 --- a/src/Nest/DSL/Search/SortScriptDescriptor.cs +++ b/src/Nest/DSL/Search/SortScriptDescriptor.cs @@ -5,85 +5,97 @@ namespace Nest.DSL.Descriptors { - public interface IScriptSort : ISort - { - [JsonProperty(PropertyName = "type")] - string Type { get; set; } - - [JsonProperty(PropertyName = "script")] - string Script { get; set; } - - [JsonProperty(PropertyName = "params")] - [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] - Dictionary Params { get; set; } - - [JsonProperty(PropertyName = "lang")] - string Language { get; set; } - } - - public class ScriptSort : SortBase, IScriptSort - { - public string Type { get; set; } - public string Script { get; set; } - public Dictionary Params { get; set; } - public string Language { get; set; } - } - - public class SortScriptDescriptor : SortDescriptorBase>, IScriptSort where T : class - { - public IScriptSort Self { get { return this; } } - - string IScriptSort.Type { get; set; } - - string IScriptSort.Script { get; set; } - - string IScriptSort.Language { get; set; } - - Dictionary IScriptSort.Params { get; set; } - - public SortScriptDescriptor Script(string script) - { - script.ThrowIfNull("script"); - Self.Script = script; - return this; - } - - public SortScriptDescriptor Params(Func, FluentDictionary> paramDictionary) - { - paramDictionary.ThrowIfNull("paramDictionary"); - Self.Params = paramDictionary(new FluentDictionary()); - return this; - } - - public virtual SortScriptDescriptor MissingLast() - { - Self.Missing = "_last"; - return this; - } - public virtual SortScriptDescriptor MissingFirst() - { - Self.Missing = "_first"; - return this; - } - public virtual SortScriptDescriptor Type(string type) - { - Self.Type = type; - return this; - } - - /// - /// Value to sort on when the orginal value for the field is missing - /// - public virtual SortScriptDescriptor MissingValue(string value) - { - Self.Missing = value; - return this; - } - - public SortScriptDescriptor Language(string language) - { - Self.Language = language; - return this; - } - } + public interface IScriptSort : ISort + { + [JsonProperty(PropertyName = "type")] + string Type { get; set; } + + [JsonProperty(PropertyName = "script")] + string Script { get; set; } + + [JsonProperty(PropertyName = "params")] + [JsonConverter(typeof(DictionaryKeysAreNotPropertyNamesJsonConverter))] + Dictionary Params { get; set; } + + [JsonProperty(PropertyName = "lang")] + string Language { get; set; } + + [JsonProperty(PropertyName = "file")] + string File { get; set; } + } + + public class ScriptSort : SortBase, IScriptSort + { + public string Type { get; set; } + public string Script { get; set; } + public Dictionary Params { get; set; } + public string Language { get; set; } + public string File { get; set; } + } + + public class SortScriptDescriptor : SortDescriptorBase>, IScriptSort where T : class + { + public IScriptSort Self { get { return this; } } + + string IScriptSort.Type { get; set; } + + string IScriptSort.Script { get; set; } + + string IScriptSort.Language { get; set; } + + string IScriptSort.File { get; set; } + + Dictionary IScriptSort.Params { get; set; } + + public SortScriptDescriptor Script(string script) + { + script.ThrowIfNull("script"); + Self.Script = script; + return this; + } + + public SortScriptDescriptor Params(Func, FluentDictionary> paramDictionary) + { + paramDictionary.ThrowIfNull("paramDictionary"); + Self.Params = paramDictionary(new FluentDictionary()); + return this; + } + + public virtual SortScriptDescriptor MissingLast() + { + Self.Missing = "_last"; + return this; + } + public virtual SortScriptDescriptor MissingFirst() + { + Self.Missing = "_first"; + return this; + } + public virtual SortScriptDescriptor Type(string type) + { + Self.Type = type; + return this; + } + + /// + /// Value to sort on when the orginal value for the field is missing + /// + public virtual SortScriptDescriptor MissingValue(string value) + { + Self.Missing = value; + return this; + } + + public SortScriptDescriptor Language(string language) + { + Self.Language = language; + return this; + } + + public SortScriptDescriptor File(string file) + { + Self.File = file; + return this; + } + } } diff --git a/src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs b/src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs index c50f1dc0ed9..e5b75308f44 100644 --- a/src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs +++ b/src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs @@ -307,6 +307,47 @@ public void TestSortScript() Assert.True(json.JsonEquals(expected), json); } + [Test] + public void TestSortScriptFile() + { + var s = new SearchDescriptor() + .From(0) + .Size(10) + .SortScript(sort => sort + .MissingLast() + .Descending() + .Mode(SortMode.Average) + .File("SortScript") + .Params(p => p + .Add("factor", 1.1) + ) + .Language("native") + .Type("number") + ); + var json = TestElasticClient.Serialize(s); + var expected = @" + { + from: 0, + size: 10, + sort: [ + { + _script: { + type: ""number"", + params: { + factor: 1.1 + }, + missing: ""_last"", + order: ""desc"", + mode: ""avg"", + lang: ""native"", + file: ""SortScript"", + } + } + ] + }"; + Assert.True(json.JsonEquals(expected), json); + } + [Test] public void TestNestedFilter() {