RavenDB 2.5 ignores OrderByDecending

73 views
Skip to first unread message

Mouhong Lin

unread,
Dec 20, 2013, 1:07:20 PM12/20/13
to rav...@googlegroups.com
Hi,

I'm using build #2750, both server and client. I found that the OrderByDecending call in the Linq API is totally ignored.
But if I use LuceneQuery<T>().AddOrder(), then it will work:

// - NOT Work: This can NOT retrieve ordered result
//var query = session.Query<TestItem, TestItemIndex>()
//                            .OrderByDescending(x => x.Weight);

// - NOT Work
//var query = session.Advanced.LuceneQuery<TestItem, TestItemIndex>()
//                                           .OrderByDescending(x => x.Weight);

// - Work: This can retrieve ordered result
var query = session.Advanced.LuceneQuery<TestItem, TestItemIndex>()
                                           .AddOrder("Weight", true);


See my full gist here (I have comments in the code): https://gist.github.com/mouhong/8058789

Is it a bug?

Khalid Abuhakmeh

unread,
Dec 20, 2013, 2:03:20 PM12/20/13
to rav...@googlegroups.com
You need to "Store" any values you are sorting by. Sorting happens in Lucene. If you aren't storing it it doesn't know what to do and probably defaults to the default sorting.

Mouhong Lin

unread,
Dec 20, 2013, 2:09:46 PM12/20/13
to rav...@googlegroups.com

but you see, if i use LuceneQuery.AddOrder, it will work. Sorting also works in raven studio. So i think it's the client api's problem

--
You received this message because you are subscribed to a topic in the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/2pZp21HtXnA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Kijana Woodard

unread,
Dec 20, 2013, 3:34:14 PM12/20/13
to rav...@googlegroups.com
Interesting. OrderByDescending was my first pull request for raven.

The (outdated) version of the code on my machine looks like this:

public void OrderByDescending(params string[] fields)
{
fields = fields.Select(MakeFieldSortDescending).ToArray();
OrderBy(fields);
}

protected string MakeFieldSortDescending(string field)
{
if (string.IsNullOrWhiteSpace(field) || field.StartsWith("+") || field.StartsWith("-"))
{
return field;
}

return "-" + field;
}

Not sure what could go wrong other than something started prepending + earlier in the pipeline.




--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Kijana Woodard

unread,
Dec 20, 2013, 3:36:59 PM12/20/13
to rav...@googlegroups.com
Oh. I think I see the problem:

public IDocumentQuery<T> OrderByDescending<TValue>(params Expression<Func<T, TValue>>[] propertySelectors)
{
...
            OrderBy(orderByfields);
...

Not sure if submitted with that bug or if it's a regression. I may be able to fix this weekend if no one gets there first.

Kijana Woodard

unread,
Dec 20, 2013, 3:37:38 PM12/20/13
to rav...@googlegroups.com
Around line 773 in DocumentQuery.cs.

Kijana Woodard

unread,
Dec 20, 2013, 11:03:41 PM12/20/13
to rav...@googlegroups.com
Interesting exercise. I'll skip to the end.

@Mouhong - I made the test pass....by simple removing the FieldIndexing.NotAnalyzed call in the index.

@Oren: 
Is that a bug?
Is DocumentQuery being used anymore for sorting? RavenQueryableExtensions seems to use System.
Linq.Queryable directly.


@Mouhong - Your test was missing a "WaitForIndexes" step. Using RavenTestBase (ravendb.tests.helpers), you can wait like this: 




Mouhong Lin

unread,
Dec 20, 2013, 11:22:50 PM12/20/13
to rav...@googlegroups.com

inline

2013-12-21 下午12:03于 "Kijana Woodard" <kijana....@gmail.com>写道:
>
> Interesting exercise. I'll skip to the end.
>
> @Mouhong - I made the test pass....by simple removing the FieldIndexing.NotAnalyzed call in the index.

I found this, but i don't want to do this, cos that index is huge, i don't want to take a hour or so to reindex it :D
Now i fixed it using luceneQuery.AddOrder() as i mentioned. But i think there might be some bug in the OrderBy apis which accept linq expression.


>
> @Oren: 
> Is that a bug?
> Is DocumentQuery being used anymore for sorting? RavenQueryableExtensions seems to use System.
> Linq.Queryable directly.
>
> fwiw: https://github.com/ayende/ravendb/pull/503
>
> @Mouhong - Your test was missing a "WaitForIndexes" step. Using RavenTestBase (ravendb.tests.helpers), you can wait like this: 
> https://github.com/kijanawoodard/ravendb/blob/edffeff73a9a5342bc0b49bdcb1b8fb2a48b35ce/Raven.Tests/MailingList/Mouhong.cs#L45
>

> Yea, thanks.

Oren Eini (Ayende Rahien)

unread,
Dec 22, 2013, 4:28:26 AM12/22/13
to ravendb
I think that this is a bug, yes.
RavenQueryableExtensions doesn't actually _run_ anything. It is being used as a place holder, that is all.
Merged your pull request, thanks.

Oren Eini
CEO
Hibernating Rhinos
Office:    +972-4-674-7811
Fax:       +972-153-4622-7811



Ken Dale

unread,
Dec 27, 2013, 12:28:52 PM12/27/13
to rav...@googlegroups.com
What's the earliest version this fix is included in?

Kijana Woodard

unread,
Dec 27, 2013, 12:47:36 PM12/27/13
to rav...@googlegroups.com
I don't think the merge actually fixes the underlying issue. The problem is explicitly stating that the index is NotAnalyzed. Remove that and you should be ok.

Mouhong Lin

unread,
Dec 28, 2013, 7:01:00 AM12/28/13
to rav...@googlegroups.com

but why with NotAnalyzed, it can work in raven studio but can't work in .net api?
btw: NotAnalyzed means it's not full text searchable, but it can be sorted? Am i missing something? Not quite familiar with lucene

Kijana Woodard

unread,
Dec 28, 2013, 10:02:06 AM12/28/13
to rav...@googlegroups.com

It was brought up in another thread that NotAnalyzed turns off the range feature. Not sure why it works in the studio though.

Chris Marisic

unread,
Jan 2, 2014, 10:29:21 AM1/2/14
to rav...@googlegroups.com
In general you never need to use NotAnalzyed. The only compelling reason i know of to use NotAnalyzed is if you store Guids with hyphens as strings, or if you are storing base64 encoded data such as a cryptographic hash and use it for look ups (such as matching passwords to the persisted hashed password value with exact certainty)
Reply all
Reply to author
Forward
0 new messages