1

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.

Community
  • 1
  • 1
Ciel
  • 4,290
  • 8
  • 51
  • 110
  • in your second query (where you are using the `Level__ByNumber ` index you need to call `.OrderBy(r => r.Number)` after Customize – wal Feb 20 '14 at 22:12
  • I have attempted it this way, as well. It has no impact. – Ciel Feb 20 '14 at 22:13
  • are you registering your index when you start ravendb? eg `IndexCreation.CreateIndexes(typeof(Users_ByUsername).Assembly, _store)` ? – wal Feb 20 '14 at 22:16
  • Yes, I am. The indexes show up fine in the Raven Management console. I can query them and everything exactly as I would expect. – Ciel Feb 20 '14 at 22:17

2 Answers2

1

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
    }
}
wal
  • 17,409
  • 8
  • 74
  • 109
  • They do not differ at all, and I am still getting the wrong results. – Ciel Feb 20 '14 at 22:29
  • Well, correction; Mine does have a 'Search' clause possible. But even if I remove that, I get the same wrong results. – Ciel Feb 20 '14 at 22:30
  • Hey, after starting my entire project over from scratch and copying/pasting every line verbatim, it worked this time. There must have been some strange hangup in the project files somewhere. – Ciel Mar 05 '14 at 22:55
0

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.

Khalid Abuhakmeh
  • 10,709
  • 10
  • 52
  • 75
  • I have done both of those things, and it still does not work. – Ciel Feb 20 '14 at 21:57
  • I have updated my code to represent more _exactly_ what it looks like to me. I forgot that I was putting a search field on it, and that may alter the behavior. But I do not believe it should. – Ciel Feb 20 '14 at 22:04
  • To try and help make this all clearer, I have placed 100% of the relevant code in a pastie for you to observe. However, I do not believe any of the ancillary code has any impact on it. It is merely there to make the entire process extremely repeatable and short for multiple different pages to use: http://pastie.org/private/bkqujjxb0aljjgs6ltmvw – Ciel Feb 20 '14 at 22:12
  • 1
    Hmm, maybe it's just me, but I don't see where you actually use `OrderBy()` in the code you pasted. – Kiliman Feb 20 '14 at 23:29