Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Supporting the ability to define sort order on the client if it is no…

…t specified on the server
  • Loading branch information...
commit 665b95088a7709b01cffdba5bf0b3f5aaff3507f 1 parent 270dbf6
@ayende ayende authored
View
21 Raven.Database/Extensions/IndexingExtensions.cs
@@ -13,6 +13,7 @@
using Raven.Abstractions.Data;
using Raven.Abstractions.Indexing;
using Raven.Database.Indexing.Sorting;
+using Raven.Database.Server;
using Constants = Raven.Abstractions.Data.Constants;
namespace Raven.Database.Extensions
@@ -117,16 +118,22 @@ public static Sort GetSort(this IndexQuery self, IndexDefinition indexDefinition
public static SortOptions? GetSortOption(this IndexDefinition self, string name)
{
SortOptions value;
- if (!self.SortOptions.TryGetValue(name, out value))
+ if (self.SortOptions.TryGetValue(name, out value))
+ return value;
+
+ if (name.EndsWith("_Range"))
{
- if (!name.EndsWith("_Range"))
- {
- return null;
- }
string nameWithoutRange = name.Substring(0, name.Length - "_Range".Length);
- if (!self.SortOptions.TryGetValue(nameWithoutRange, out value))
- return null;
+ if (self.SortOptions.TryGetValue(nameWithoutRange, out value))
+ return value;
}
+ if (CurrentOperationContext.Headers.Value == null)
+ return value;
+
+ var hint = CurrentOperationContext.Headers.Value["SortHint-" + name];
+ if (string.IsNullOrEmpty(hint))
+ return value;
+ Enum.TryParse(hint, true, out value);
return value;
}
}
View
45 Raven.Tests/MailingList/DynamicFieldSorting.cs
@@ -47,6 +47,12 @@ public class ProjectionItem
{
public string SongId { get; set; }
public NumericAttribute[] NumericAttributes { get; set; }
+
+ public override string ToString()
+ {
+ return string.Format("SongId: {0}, N1: {1}", SongId,
+ NumericAttributes.First(x => x.Name == "N1").Value);
+ }
}
public WithDynamicIndex()
@@ -72,7 +78,7 @@ select new
[Fact]
public void CanSortDynamicaly()
{
- using(var store = NewDocumentStore())
+ using (var store = NewDocumentStore())
{
new WithDynamicIndex().Execute(store);
using (var session = store.OpenSession())
@@ -108,6 +114,43 @@ public void CanSortDynamicaly()
}
}
+ [Fact]
+ public void CanSortDynamicaly_Desc()
+ {
+ using (var Store = NewDocumentStore())
+ {
+ new WithDynamicIndex().Execute(Store);
+ using (var session = Store.OpenSession())
+ {
+ session.Store(new DataSet
+ {
+ Items = Enumerable.Range(1, 50).Select(x =>
+ new Item
+ {
+ SongId = "songs/" + x,
+ NumericAttributes = new[]
+ {
+ new NumericAttribute("N1",x*0.99d),
+ new NumericAttribute("N4",x*0.01d),
+ }
+ }).ToList()
+ });
+ session.SaveChanges();
+ }
+
+ using (var s = Store.OpenSession())
+ {
+ var items = s.Advanced.LuceneQuery<WithDynamicIndex.ProjectionItem, WithDynamicIndex>()
+ .WaitForNonStaleResults()
+ .AddOrder("N1_Range", true, typeof(double))
+ .SelectFields<WithDynamicIndex.ProjectionItem>("SongId", "NumericAttributes")
+ .ToList();
+ Assert.Equal(50, items.Count);
+ Assert.Equal("songs/50", items.First().SongId);
+ }
+ }
+ }
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.