creation of a map reduce in code and then querying using it

16 views
Skip to first unread message

greg

unread,
May 28, 2012, 2:25:19 PM5/28/12
to rav...@googlegroups.com
I have created an index in code as follows:-
 

    using Raven.Abstractions.Indexing;
    using Raven.Client.Indexes;

    public class CategoryProductCount : AbstractIndexCreationTask<Product, CategoryCount>
    {
        public CategoryProductCount()
        {
            Map = docs => from doc in docs
                          select new
                              {
                                  CategoryId = doc.CategoryId,
                                  Count = 1
                              };

            Reduce = results => from result in results
                                group result by result.CategoryId
                into g select new { CategoryId = g.Key, Count = g.Sum(x => x.Count) };

            Index(x => x.CategoryId, FieldIndexing.Analyzed);
        }
    }

    public class CategoryCount
    {
        public int CategoryId { get; set; }

        public int Count { get; set; }
    }

 

I wanted to test this out in code as follows:-

var results = _session.Query<Product>("CategoryProductCount").Where(x => x.CategoryId == "7").ToList();

I know there are 3 records with a CategoryId of 7 but I always get 1 result, doing something silly but cant see what, any help would be appreciated.

 

 

Itamar Syn-Hershko

unread,
May 28, 2012, 3:06:47 PM5/28/12
to rav...@googlegroups.com
Remove this line:

Index(x => x.CategoryId, FieldIndexing.Analyzed);

greg

unread,
May 28, 2012, 3:27:05 PM5/28/12
to rav...@googlegroups.com
Removed and still getting results = 1

Itamar Syn-Hershko

unread,
May 28, 2012, 3:29:13 PM5/28/12
to rav...@googlegroups.com
Then create a failing test and move from there...

greg

unread,
May 28, 2012, 3:45:33 PM5/28/12
to rav...@googlegroups.com
Will do, wasnt sure if it was a syntax error or not.

Oren Eini (Ayende Rahien)

unread,
May 29, 2012, 5:31:18 AM5/29/12
to rav...@googlegroups.com
doc.CategoryId is an integer?
Reply all
Reply to author
Forward
0 new messages