Question

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.

Was it helpful?

Solution

Your code works for me as you've written it (you have one extra underscore in the index name but I dont think that will affect anything) Please compare how this unit test is different to what you're doing as it passes for me:

[TestMethod]
public void TestMethod()
{
    using (var session = _store.OpenSession())
    {
        session.Store(new Level{ Number = 2828});
        session.Store(new Level { Number = 8583 });
        session.Store(new Level { Number = 3751 });
        session.Store(new Level{ Number = 1});
        session.SaveChanges();
        RavenQueryStatistics statistics;
        var query = session.Query<Level, Level_ByNumber>().Customize(c => c.WaitForNonStaleResults()).Statistics(out statistics).OrderBy(x => x.Number);

        var results = query.ToList();

        Assert.AreEqual(1, results[0].Number);
        Assert.AreEqual(2828, results[1].Number);
        Assert.AreEqual(3751, results[2].Number);
        Assert.AreEqual(8583, results[3].Number);//all pass
    }
}

OTHER TIPS

I believe your problem is that you are analyzing the Number field. Don't do that.

Index(n => n.Number, FieldIndexing.Analyzed);

Remove that and you should be ok.

If that doesn't work, try storing your fielding using Store, but I don't think you need that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top