Skip to content
Closed
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
174 changes: 93 additions & 81 deletions src/Nest/DSL/Search/SortScriptDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object> 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<string, object> Params { get; set; }
public string Language { get; set; }
}

public class SortScriptDescriptor<T> : SortDescriptorBase<T, SortScriptDescriptor<T>>, 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<string, object> IScriptSort.Params { get; set; }

public SortScriptDescriptor<T> Script(string script)
{
script.ThrowIfNull("script");
Self.Script = script;
return this;
}

public SortScriptDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramDictionary)
{
paramDictionary.ThrowIfNull("paramDictionary");
Self.Params = paramDictionary(new FluentDictionary<string, object>());
return this;
}

public virtual SortScriptDescriptor<T> MissingLast()
{
Self.Missing = "_last";
return this;
}
public virtual SortScriptDescriptor<T> MissingFirst()
{
Self.Missing = "_first";
return this;
}
public virtual SortScriptDescriptor<T> Type(string type)
{
Self.Type = type;
return this;
}

/// <summary>
/// Value to sort on when the orginal value for the field is missing
/// </summary>
public virtual SortScriptDescriptor<T> MissingValue(string value)
{
Self.Missing = value;
return this;
}

public SortScriptDescriptor<T> 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<string, object> 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<string, object> Params { get; set; }
public string Language { get; set; }
public string File { get; set; }
}

public class SortScriptDescriptor<T> : SortDescriptorBase<T, SortScriptDescriptor<T>>, 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<string, object> IScriptSort.Params { get; set; }

public SortScriptDescriptor<T> Script(string script)
{
script.ThrowIfNull("script");
Self.Script = script;
return this;
}

public SortScriptDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramDictionary)
{
paramDictionary.ThrowIfNull("paramDictionary");
Self.Params = paramDictionary(new FluentDictionary<string, object>());
return this;
}

public virtual SortScriptDescriptor<T> MissingLast()
{
Self.Missing = "_last";
return this;
}
public virtual SortScriptDescriptor<T> MissingFirst()
{
Self.Missing = "_first";
return this;
}
public virtual SortScriptDescriptor<T> Type(string type)
{
Self.Type = type;
return this;
}

/// <summary>
/// Value to sort on when the orginal value for the field is missing
/// </summary>
public virtual SortScriptDescriptor<T> MissingValue(string value)
{
Self.Missing = value;
return this;
}

public SortScriptDescriptor<T> Language(string language)
{
Self.Language = language;
return this;
}

public SortScriptDescriptor<T> File(string file)
{
Self.File = file;
return this;
}
}
}
41 changes: 41 additions & 0 deletions src/Tests/Nest.Tests.Unit/Search/Sorting/SortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,47 @@ public void TestSortScript()
Assert.True(json.JsonEquals(expected), json);
}

[Test]
public void TestSortScriptFile()
{
var s = new SearchDescriptor<ElasticsearchProject>()
.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()
{
Expand Down