Commit 9cdf4fd3 authored by Marc Gravell's avatar Marc Gravell Committed by GitHub

Merge pull request #646 from rajeshsubhankar/feature/NRediSearch-Sorting

Feature- NRediSearch Sorting Functionality
parents 1a541b7f 0717e3c7
...@@ -161,6 +161,16 @@ public Paging(int offset, int count) ...@@ -161,6 +161,16 @@ public Paging(int offset, int count)
/// </summary> /// </summary>
public byte[] Payload { get; set; } public byte[] Payload { get; set; }
/// <summary>
/// Set the query parameter to sort by
/// </summary>
public string SortBy { get; set; }
/// <summary>
/// Set the query parameter to sort by ASC by default
/// </summary>
public bool SortAscending {get; set;} = true;
/// <summary> /// <summary>
/// Create a new index /// Create a new index
/// </summary> /// </summary>
...@@ -205,6 +215,13 @@ internal void SerializeRedisArgs(List<object> args) ...@@ -205,6 +215,13 @@ internal void SerializeRedisArgs(List<object> args)
args.AddRange(_fields); args.AddRange(_fields);
} }
if(SortBy != null)
{
args.Add("SORTBY".Literal());
args.Add(SortBy);
args.Add((SortAscending ? "ASC" : "DESC").Literal());
}
if (Payload != null) if (Payload != null)
{ {
args.Add("PAYLOAD".Literal()); args.Add("PAYLOAD".Literal());
...@@ -260,5 +277,18 @@ public Query LimitFields(params string[] fields) ...@@ -260,5 +277,18 @@ public Query LimitFields(params string[] fields)
this._fields = fields; this._fields = fields;
return this; return this;
} }
/// <summary>
/// Set the query to be sorted by a sortable field defined in the schema
/// </summary>
/// <param name="field">the sorting field's name</param>
/// <param name="ascending">if set to true, the sorting order is ascending, else descending</param>
/// <returns>the query object itself</returns>
public Query SetSortBy(string field, bool ascending = true)
{
SortBy = field;
SortAscending = ascending;
return this;
}
} }
} }
...@@ -22,11 +22,13 @@ public class Field ...@@ -22,11 +22,13 @@ public class Field
{ {
public String Name { get; } public String Name { get; }
public FieldType Type { get; } public FieldType Type { get; }
public bool Sortable {get;}
internal Field(string name, FieldType type) internal Field(string name, FieldType type, bool sortable)
{ {
Name = name; Name = name;
Type = type; Type = type;
Sortable = sortable;
} }
internal virtual void SerializeRedisArgs(List<object> args) internal virtual void SerializeRedisArgs(List<object> args)
...@@ -43,12 +45,17 @@ object GetForRedis(FieldType type) ...@@ -43,12 +45,17 @@ object GetForRedis(FieldType type)
} }
args.Add(Name); args.Add(Name);
args.Add(GetForRedis(Type)); args.Add(GetForRedis(Type));
if(Sortable){args.Add("SORTABLE");}
} }
} }
public class TextField : Field public class TextField : Field
{ {
public double Weight { get; } public double Weight { get; }
internal TextField(string name, double weight = 1.0) : base(name, FieldType.FullText) internal TextField(string name, double weight = 1.0) : base(name, FieldType.FullText, false)
{
Weight = weight;
}
internal TextField(string name, bool sortable, double weight = 1.0) : base(name, FieldType.FullText, sortable)
{ {
Weight = weight; Weight = weight;
} }
...@@ -77,6 +84,18 @@ public Schema AddTextField(string name, double weight = 1.0) ...@@ -77,6 +84,18 @@ public Schema AddTextField(string name, double weight = 1.0)
return this; return this;
} }
/// <summary>
/// Add a text field that can be sorted on
/// </summary>
/// <param name="name">the field's name</param>
/// <param name="weight">its weight, a positive floating point number</param>
/// <returns>the schema object</returns>
public Schema AddSortableTextField(string name, double weight = 1.0)
{
Fields.Add(new TextField(name, true, weight));
return this;
}
/// <summary> /// <summary>
/// Add a numeric field to the schema /// Add a numeric field to the schema
/// </summary> /// </summary>
...@@ -84,7 +103,7 @@ public Schema AddTextField(string name, double weight = 1.0) ...@@ -84,7 +103,7 @@ public Schema AddTextField(string name, double weight = 1.0)
/// <returns>the schema object</returns> /// <returns>the schema object</returns>
public Schema AddGeoField(string name) public Schema AddGeoField(string name)
{ {
Fields.Add(new Field(name, FieldType.Geo)); Fields.Add(new Field(name, FieldType.Geo, false));
return this; return this;
} }
...@@ -95,7 +114,18 @@ public Schema AddGeoField(string name) ...@@ -95,7 +114,18 @@ public Schema AddGeoField(string name)
/// <returns>the schema object</returns> /// <returns>the schema object</returns>
public Schema AddNumericField(string name) public Schema AddNumericField(string name)
{ {
Fields.Add(new Field(name, FieldType.Numeric)); Fields.Add(new Field(name, FieldType.Numeric, false));
return this;
}
/// <summary>
/// Add a numeric field that can be sorted on
/// </summary>
/// <param name="name">the field's name</param>
/// <returns>the schema object</returns>
public Schema AddSortableNumericField(string name)
{
Fields.Add(new Field(name, FieldType.Numeric, true));
return this; return this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment