How to do a simple map/reduce aggregation

48 views
Skip to first unread message

avin

unread,
Jul 5, 2012, 5:26:13 AM7/5/12
to rav...@googlegroups.com
hi,

   I have a document collection named Books, which looks like this.

{
   
"Name":"B",
   
"Price":4
}
{
   
"Name":"A",
   
"Price":10
}

How do I perform a simple aggregation on the prices in the list? i.e., I just wana get the whole total prices of all documents in the collection Books. I don't want to perform any grouping, just a simple scalar result. How can I achieve this using map reduce? Any help will be highly appreciated.
Regards,
Avin

Oren Eini (Ayende Rahien)

unread,
Jul 5, 2012, 5:36:42 AM7/5/12
to rav...@googlegroups.com
group result by "constant" into g

avin

unread,
Jul 5, 2012, 8:04:58 AM7/5/12
to rav...@googlegroups.com
Ok Oren,  here is my code,

============================================================

public class GetTotalAssetStorageSize : AbstractIndexCreationTask<Asset,GetTotalAssetStorageSize.Result>
    {
        /// <summary>
        /// The class that maps the fields in Assets document for doing a full text search.
        /// </summary>
        public class Result
        {
            /// <summary>
            /// Gets or sets the FileSize.
            /// </summary>
            public int FileSize { get; set; }
        }

        /// <summary>
        /// Initializes a new instance of the GetTotalAssetStorageSize class.
        /// </summary>
        public GetTotalAssetStorageSize()
        {
            Map = assets => from asset in assets
                            select new
                            {
                                Total = asset.FileSize,
                            };

            Reduce = results => from result in results
                                group result by 1 into g
                                select new
                                {
                                    Total = g.Sum(x=>x.FileSize)
                                };
                     
        }

    }
============================================================

for fetching the results, I use the following code,
var results = this.Session.Query<GetTotalAssetStorageSize.Result,GetTotalAssetStorageSize>()   
                            .ToArray();

Do I miss something?  Please  help..............

Regards,
Avin

Kijana Woodard

unread,
Jul 5, 2012, 8:13:57 AM7/5/12
to rav...@googlegroups.com

Your result property is FileSize, but your reduce property is Total. Change Total to FileSize in your map and reduce.

avin

unread,
Jul 5, 2012, 8:32:59 AM7/5/12
to rav...@googlegroups.com
thx  Kijana, it worked.
Reply all
Reply to author
Forward
0 new messages