Index with Sort not sorting...

32 views
Skip to first unread message

Amanda Myer

unread,
Mar 3, 2012, 4:21:48 PM3/3/12
to rav...@googlegroups.com
I have the following static index defined.

public class DayOfMonths_AllDaysSortedByOrder : AbstractIndexCreationTask<DayOfMonth, DayOfMonth>
    {
        public DayOfMonths_AllDaysSortedByOrder()
        {
            Map = days => from day in days select new { day };

            Sort(x => x.Order, Raven.Abstractions.Indexing.SortOptions.Int);
        }
    }

However, it is not applying a sort.  It is returning the objects in the order of when they were last updated or created, which seems to be what it does when there is no sorting applied.

What have I done wrong?

Oren Eini (Ayende Rahien)

unread,
Mar 4, 2012, 5:55:40 AM3/4/12
to rav...@googlegroups.com
            Map = days => from day in days select new { day.Order };

You need to define the actual fields that you are using.

Amanda Myer

unread,
Mar 4, 2012, 7:50:40 AM3/4/12
to rav...@googlegroups.com
 Doesn't this method return only a list of the "Order" values?

I want to get all of the "days" docs  sorted by their "Order" property.

Do I misunderstand the syntax then?

Oren Eini (Ayende Rahien)

unread,
Mar 4, 2012, 8:05:38 AM3/4/12
to rav...@googlegroups.com
Amanda,
You misunderstood the effect of indexes.
Indexes merely control how you are searching, they don't (usually) control what you get back.
In this case, you define an index on the Order property and say that this is an int.

Note that for such a simple index, you don't actually need to have a static index.

Amanda Myer

unread,
Mar 4, 2012, 8:19:11 AM3/4/12
to rav...@googlegroups.com
Ayende,

I wasn't going to do such a simple index, but I have a need to be able to reset the data in my db back to an initial/original state.

I thought that the only way for me to remove all documents of a certain type in one database call was to use a command like this:

// Delete old days
                    session.Query<DayOfMonth, DayOfMonths_AllDaysSortedByOrder>().Customize(x => x.WaitForNonStaleResults()).Take(0).ToList();
                    session.Advanced.DatabaseCommands.DeleteByIndex("DayOfMonths/AllDaysSortedByOrder", new IndexQuery { }, false); 

So, I added the index merely to get all documents of type "DayOfMonth", but then I thought I may as well use it for getting all the documents for display in my application as well, so I thought to add a sort index on it.

If there is another way I can delete all documents of a specific type in one database call, I will gladly do that instead. =)

I am a total RavenDB newb, so thanks for your patient explanations!

Oren Eini (Ayende Rahien)

unread,
Mar 4, 2012, 8:22:39 AM3/4/12
to rav...@googlegroups.com
You can use the builtin index for that:

         session.Advanced.DatabaseCommands.DeleteByIndex("Raven/DocumentsByEntityName", new IndexQuery { Query = "Tag:DayOfMonths" }, false);  

Amanda Myer

unread,
Mar 4, 2012, 8:39:24 AM3/4/12
to rav...@googlegroups.com
Fantastic!  Thanks!
Reply all
Reply to author
Forward
0 new messages