Unable to obtain a correct Count / Statistics on a query over an index

39 views
Skip to first unread message

sam.bam...@gmail.com

unread,
Oct 20, 2014, 4:13:55 PM10/20/14
to rav...@googlegroups.com
I have 4 order documents each like  (with all having    "EmailAddress": "Sa...@RavenDB.com")

{
"UserCommentSaved": true,
 "IpAddress": "::1",
"LineItems": [
     {
      "City": "Test Event",
      "State": "",
      "Country": "United Kingdom",
      "Status": "Assigned",
      "Timestamp": "2014-09-05T14:18:24.1761563Z"
    },
   {
      "City": "Test Event",
      "State": "",
      "Country": "United Kingdom",
      "Status": "Assigned",
      "Timestamp": "2014-09-05T14:18:24.1761563Z"
    }
  ],
"BillingAddress": {
    "FirstName": "ccrgd",
    "LastName": "vccb",
    "Street1": "123 Line1",
    "Street2": "Line 2",
    "EmailAddress": "Sa...@RavenDB.com"
  },
}

 and OrdersIndex is 

  public class OrdersIndex : AbstractIndexCreationTask<Order>
    {
       public class ReduceResult
        {
      
            public string EmailAddress { get; set; }

            public List<LineItemHistoryChild> LineItems { set; get; }
        }


      public OrdersIndex()
        {
            Map = orders => from order in orders
                            from lineItemsItem in ((IEnumerable<LineItem>)order.LineItems).DefaultIfEmpty()
                            select new
                                {
                                           order.LineItems,
                                           order.BillingAddress.EmailAddress
                                 };
        }

}

                                             so in===

    var query = session.Query<OrdersIndex.ReduceResult, OrdersIndex>().Search(x => x.EmailAddress, "S...@RavenDB.com");
     orders=     query .Skip(3) .Take(3).ToList();

   ordersCount=orders.Count;
   queryCount=query.Count();

                                           I have the following results:===
ordersCount is 1

queryCount is 8
                                            But i expected 

ordersCount is 1

queryCount is 4 (coz there are only 4 docs in the db )


Same result when i use .Statistics(out stats) as stats.TotalResults also gives 8.

Is there a way to count the total number of matching orders in the system to give correct result?
Something like Session.Query<Order>().Count() works (but then gives the count of all the orders corrctly) but Query<OrdersIndex.ReduceResult, OrdersIndex>().Search(x => x.EmailAddress, "S...@RavenDB.com").Count() (done over the index) doesn't and its the count of orders that matches the search criteria I need






Kijana Woodard

unread,
Oct 20, 2014, 4:36:43 PM10/20/14
to rav...@googlegroups.com
You're counting "ReduceResult". Since you have two line items for each of 4 docs.....

Why are you indexing LineItems?

--
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.

Kijana Woodard

unread,
Oct 20, 2014, 4:37:38 PM10/20/14
to rav...@googlegroups.com
and more importantly, why do you have
"from lineItemsItem in ((IEnumerable<LineItem>)order.LineItems).DefaultIfEmpty()"?

Samuel Bamgboye

unread,
Oct 21, 2014, 8:51:09 AM10/21/14
to rav...@googlegroups.com
Thanks for replying. I wanted to maintain only one index while having another query using the same index over LineItems like this :   query = session.Query<OrdersIndex.ReduceResult, OrdersIndex>().Search(x => x.LineItems_Status, _foundLineItemStatus.ToString());

creating another index would have solved it but performance is another concern.

I had excluded the line in the index for the above query. its like this

  public OrdersIndex()
        {
            Map = orders => from order in orders
                            from lineItemsItem in ((IEnumerable<LineItem>)order.LineItems).DefaultIfEmpty()
                            select new
                                {
                                           order.LineItems,
                                           order.BillingAddress.EmailAddress,
                                          LineItems_Status = lineItemsItem.Status
                                 };
        }

The line from lineItemsItem in ((IEnumerable<LineItem>)order.LineItems).DefaultIfEmpty() is to enable me do  LineItems_Status = lineItemsItem.Status   because i want to search for a given status property of an object, the collection of which is a property in the document

Thanks
    var query = session.Query<OrdersIndex.ReduceResult, OrdersIndex>().Search(x => x.EmailAddress, "Sam@RavenDB.com");
     orders=     query .Skip(3) .Take(3).ToList();

   ordersCount=orders.Count;
   queryCount=query.Count();

                                           I have the following results:===
ordersCount is 1

queryCount is 8
                                            But i expected 

ordersCount is 1

queryCount is 4 (coz there are only 4 docs in the db )


Same result when i use .Statistics(out stats) as stats.TotalResults also gives 8.

Is there a way to count the total number of matching orders in the system to give correct result?
Something like Session.Query<Order>().Count() works (but then gives the count of all the orders corrctly) but Query<OrdersIndex.ReduceResult, OrdersIndex>().Search(x => x.EmailAddress, "Sam@RavenDB.com").Count() (done over the index) doesn't and its the count of orders that matches the search criteria I need






Samuel Bamgboye

unread,
Oct 21, 2014, 8:55:39 AM10/21/14
to rav...@googlegroups.com
public class OrdersIndex : AbstractIndexCreationTask<Order>
    { 
public class ReduceResult
        {
            public bool LineItems_Status { set; get; }

            public string EmailAddress { get; set; }

            public List<LineItemHistoryChild> LineItems { set; get; }
        }
 
public OrdersIndex()
        {
            Map = orders => from order in orders
                            from lineItemsItem in ((IEnumerable<LineItem>)order.LineItems).DefaultIfEmpty()
                            select new
                                {
                                           order.LineItems,
                                           order.BillingAddress.EmailAddress,
                                          LineItems_Status = lineItemsItem.Status
                                 };
        }

}

Oren Eini (Ayende Rahien)

unread,
Oct 21, 2014, 9:52:29 AM10/21/14
to ravendb
Use this, instead:

public OrdersIndex()
        {
            Map = orders => from order in orders
                            select new
                                {
                                           order.LineItems,
                                           order.BillingAddress.EmailAddress,
                                          LineItems_Status = order.LineItems.Select(x=>x.Status)
                                 };
        }

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 

Samuel Bamgboye

unread,
Oct 21, 2014, 1:01:40 PM10/21/14
to rav...@googlegroups.com
Thank you so much. It solved the issue and more!
Reply all
Reply to author
Forward
0 new messages