I am attempting to instruct RavenDB to sort a specific index by a number field, the class looks like this;
class Level {
public int Number {
get;
set;
}
public string Id {
get;
set;
}
// other properties not listed
}
So I created 15,000 documents with a simple loop, with the number field set incrementally from 1 to 15000. This works fine so far.
Now when I query it, I want it to by - default, sort these in order of the number. So my first attempt is to just use the standard OrderBy
.
var query = RavenSession
.Query<Level>()
.Customize(c => c.WaitForNonStaleResults())
.OrderBy(r => r.Number)
.ToList();
This certainly did not work. I ended up with a list that resembles..
[levels/1]
[levels/3751]
[levels/8583]
[levels/2828]
This does not even make sense to me. So I tried specifying an index.
public class Level__ByNumber : AbstractIndexCreationTask<Models.Level> {
public Level__ByNumber() {
Map = results => from result in results
select new {
Id = result.Id,
Number = result.Number
};
Sort(n => n.Number, SortOptions.Int);
}
}
Which is then called exactly as I assume an index is called...
RavenQueryStatistics statistics;
var query = RavenSession
.Query<Level, Level_ByNumber>()
.Customize(c => c.WaitForNonStaleResults())
.Statistics(out statistics);
if(searchable != null) {
query = query.Search(n => n.Number, searchable);
}
var results = query.ToList();
And still no luck; The list is scrambled, in strange sequence. I have also tried SortOptions.Short
and SortOptions.String
with no avail. What am I doing wrong? How do I get this index to just return the objects in the expected order?
I have thoroughly read the documentation on this, and other stack overflow questions such as This and the suggested information does not work.