Sorting on a decimal

110 views
Skip to first unread message

SPATEN

unread,
Apr 13, 2012, 3:02:32 PM4/13/12
to rav...@googlegroups.com
Hello,

I posted this Question which got me started, but now that I'm up and running, I noticed that if I sort by the Price field that the ordering is close, but not right. Is there something I'm missing in the Index that might be causing this?

Thank you,
Stephen


EXAMPLE

99.99
99.99
99.95
99.95
97.95
97.9
97.22
97.22
957.15
95.99
95.99
93.95
92.52
91.58
90.63
90.23
9.99
9.99
9.99
9.99
9.99
9.9
9.87
9.87
9.8
9.79
9.79




INDEX

        public Products_Index()
        {
            Map = products => from p in products
                              select new
                              { p.ItemNum,
                              p.BrandName,
                              p.ProductName,
                              p.Catalog,
                              p.UOM,
                              p.CasePack,
                              p.AveWeight,
                              p.CatalogId,
                              p.HasPicture,
                              p.INFO2,
                              p.IsOfflineSupplierItem,
                              p.IsRebateItem,
                              p.IsSpecialOrderItem,
                              p.IsSpecialPriceItem,
                              p.Price };

            Indexes.Add(x => x.INFO2, FieldIndexing.Analyzed);
        }


POCO

 

    [Serializable]
    public class Product
    {
        private string _ProductAttributes;

        public string AveWeight { get; set; }

        public string BrandName { get; set; }

        public string CasePack { get; set; }

        public string Catalog { get; set; }

        public decimal CatalogId { get; set; }

        public decimal CategoryId { get; set; }

        public bool HasPicture { get; set; }

        public string INFO2 { get; set; }

        public string Info { get; set; }

        public bool IsOfflineSupplierItem { get; set; }

        public bool IsRebateItem { get; set; }

        public bool IsSpecialOrderItem { get; set; }

        public bool IsSpecialPriceItem { get; set; }

        public bool IsTieredPricingItem { get; set; }

        public string ItemNum { get; set; }

        public string ManufactureName { get; set; }

        public string ManufactureNum { get; set; }

        public decimal OffineSupplierId { get; set; }

        public string PackageRemarks { get; set; }

        public decimal Price { get; set; }

        public decimal PriceGroupId { get; set; }

        /*[JsonIgnore]*/
        public string ProductAttributes
        {
            get
            {
                var sb = new StringBuilder();
                if (!string.IsNullOrEmpty(Url))
                {
                    sb.Append("P");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                if (IsSpecialOrderItem)
                {
                    sb.Append("SO");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                if (IsSpecialPriceItem)
                {
                    sb.Append("SP");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                if (IsRebateItem)
                {
                    sb.Append("R");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                if (IsTieredPricingItem)
                {
                    sb.Append("T");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                sb.Append("X");
                sb.Append("~");

                if (IsOfflineSupplierItem)
                {
                    sb.Append("O");
                    sb.Append("~");
                }
                else
                {
                    sb.Append("X");
                    sb.Append("~");
                }

                sb.Append("X");
                sb.Append("~");

                _ProductAttributes = sb.ToString();
                return _ProductAttributes.TrimEnd(new char[] { '~' });
            }
            set
            {
                _ProductAttributes = value;
            }
        }

        public decimal ProductId { get; set; }

        public string ProductName { get; set; }

        public int Quantity { get; set; }

        public string SupplierName { get; set; }

        public string UOM { get; set; }

        public string Upc { get; set; }

        public string Url { get; set; }
    }
}




alwin

unread,
Apr 13, 2012, 3:23:06 PM4/13/12
to ravendb
You need to specify the sort order.
http://ravendb.net/docs/client-api/querying/static-indexes/customizing-results-order

By default RavenDB always sorts by alphabetical order, even when you
have a decimal or other number property.

(IMO it's a bad default because it's different from sorting with SQL
or Linq to objects.)


On Apr 13, 9:02 pm, SPATEN <stephenpat...@gmail.com> wrote:
> Hello,
>
> I posted this Question<http://stackoverflow.com/questions/9961882/how-do-i-construct-my-rave...> which

SPATEN

unread,
Apr 13, 2012, 3:29:59 PM4/13/12
to rav...@googlegroups.com
Thank you! 

I had read that a while ago and really didn't know when it came into play.

Matt Warren

unread,
Apr 13, 2012, 6:25:11 PM4/13/12
to rav...@googlegroups.com
It's probably the default because lucene is designed for text searching and so it defaults to alphabetical/lexicography.

But maybe the RavenDB client API could help you out a bit more and derive the correct sort order from the type (int/long/short/string etc) of the field being indexed.

alwin

unread,
Apr 13, 2012, 9:10:44 PM4/13/12
to ravendb
Yes it would be great if numbers are sorted numerically by default.

I've mentioned it in the possible breaking changes list:
http://groups.google.com/group/ravendb/browse_thread/thread/b15e500dfde0eeac

On Apr 14, 12:25 am, Matt Warren <mattd...@gmail.com> wrote:
> It's probably the default because lucene is designed for text searching and
> so it defaults to alphabetical/lexicography.
>
> But maybe the RavenDB client API could help you out a bit more and derive
> the correct sort order from the type (int/long/short/string etc) of the
> field being indexed.
>
>
>
>
>
>
>
> On Friday, 13 April 2012 20:23:06 UTC+1, alwin wrote:
>
> > You need to specify the sort order.
>
> >http://ravendb.net/docs/client-api/querying/static-indexes/customizin...
> >http://ravendb.net/docs/client-api/querying/static-indexes/customizin...
> ...
>
> read more »

Oren Eini (Ayende Rahien)

unread,
Apr 14, 2012, 6:24:40 AM4/14/12
to rav...@googlegroups.com
The API actually DO figure it out. If you are using the strongly typed Linq.
If you are using the low level lucene API, or if you are building your own queries, we have no way to figure it out.

Stephen Patten

unread,
Apr 14, 2012, 1:09:35 PM4/14/12
to rav...@googlegroups.com
Thanks Oren.

I'm sticking with the low level stuff for the time as IT WORKS
perfectly for my needs. I must say that I like the various ways to
can tackle a problem with RavneDB's APIs, thanks for all the hard work
you've put into the product.

Stephen

Oren Eini (Ayende Rahien)

unread,
Apr 15, 2012, 1:47:29 AM4/15/12
to rav...@googlegroups.com
Stephen,
In that case, take a look at this overload for the OrderBy in the LuceneQuery:

void AddOrder(string fieldName, bool descending, Type fieldType);

This let you tell RavenDB what type you are using, so it can properly index it.
Reply all
Reply to author
Forward
0 new messages