How can I do a case-insenstive orderby on queries?

39 views
Skip to first unread message

Andrew Stephens

unread,
Jul 29, 2014, 6:42:05 AM7/29/14
to rav...@googlegroups.com
I've got a query similar to this:-

   var results = session.Query<MyIndexType, MyIndexer>()
                    .OrderBy(o => o.ProductName)
                    //etc...

The results are coming back in a case-sensitive order, e.g. ABCabc. How can I get them in a case-insensitive order (AaBbCc)?
I've tried the .OrderBy() overload that takes an IComparer, passing in StringComparer.InvariantCultureIgnoreCase as the second param, but it makes no difference.

Does the index configuration play a part in this? FYI, the property I'm sorting on is configured in the index with the following options:-

            Indexes.Add(o => o.ProductName, FieldIndexing.NotAnalyzed);
            Stores.Add(o => o.ProductName, FieldStorage.Yes);

Chris Marisic

unread,
Jul 29, 2014, 9:26:36 AM7/29/14
to rav...@googlegroups.com
NotAnalyzed says DO NOT TOUCH THIS FIELD.

Analyzed says i plan to search on this field.

Analyzed can give odd results for sorting since it will split up tokens. Ultimately what i would do is this:

ProductNameNormalized { get { return ProductName.ToLower().RemoveSpecialCharacters(); } }

If you add this to an existing model you need to save every product document in the database. You could also do this inside the index to avoid needing to update every product with something like

from product in products
select
ProductNameNormalized  = ProductName.ToLower()


And you can store it and sort on it.

Oren Eini (Ayende Rahien)

unread,
Jul 30, 2014, 3:53:52 AM7/30/14
to ravendb
Or just mark as as default analyzed, which does case insensitive comparison.



Oren Eini

CEO


Mobile: + 972-52-548-6969

Office:  + 972-4-622-7811

Fax:      + 972-153-4622-7811





--
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.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages