Index Distinct (with dates) no errors but no result

276 views
Skip to first unread message

round crisis

unread,
Aug 31, 2011, 1:32:33 PM8/31/11
to rav...@googlegroups.com


I have a slightly complex index that I need over a Raven db document

given this document definition like:

public class PriceDocument

{

public Guid PriceId { get; set; }

public decimal AskPrice { get; set; }

public decimal BidPrice { get; set; }

public DateTime PricingDate { get; set; }

public string Source { get; set; }

public int Version { get; set; }

public string Id { get; set; }

}

 need to get all the products for a given productId( I can do that when I query) that are unique by PricingDate and Source (need to select the latest Version)

so given the following dataset

var priceDocument = new PriceDocument { Price = 1m, Id = productId + "/1", PricingDate =  new DateTime(2011, 4, 1, 8,0,0), PriceId = productId, Source = "Bloomberg", Version = 1 };
var priceDocument1 = new PriceDocument { Price = 1m, Id = productId + "/2", PricingDate = new DateTime(2011, 4, 1,9,0,0), PriceId= productId, Source = "Bloomberg", Version = 2 };

I should get as a result priceDocument1 , since its latest

So far I have an index defined like this:

public PriceDocuments_ByDateBySource()

{

Map = docs => from priceDocument in docs

 select new { PricingDate = priceDocument.PricingDate, PricingSource = priceDocument.Source, PriceId = priceDocument.PriceId, ProductVersion = priceDocument.Version };

Reduce = results => from result in results

                   group result by

                    new

                    {

                    result.PricingDate,

                    result.Source,

result.Version

                    }

                   into price

                   select new

                   {

                    PricingDate = price.Key.PricingDate,

                    PricingSource = price.Key.Source,

ProductVersion = price.Max(p=> price.Key.Version)

                   };

}


This however returns no errors but also no results

What am I missing ?

Thanks


Itamar Syn-Hershko

unread,
Aug 31, 2011, 5:52:59 PM8/31/11
to rav...@googlegroups.com
Try using this in your reduce, see if it helps:

                    select new

                    {

PriceId = null, 

                     PricingDate = price.Key.PricingDate,

                     PricingSource = price.Key.Source,

ProductVersion = price.Max(p=> price.Key.Version)

                    };

Ayende Rahien

unread,
Sep 1, 2011, 1:10:21 AM9/1/11
to rav...@googlegroups.com
Also, see my reply in StackOverflow

On Wed, Aug 31, 2011 at 8:32 PM, round crisis <round...@gmail.com> wrote:

round crisis

unread,
Sep 1, 2011, 5:15:37 AM9/1/11
to rav...@googlegroups.com
tried that and it doesnt compile , then I tried

  select new

                    {

PriceId = price.Select(x=> x.PriceId),

                     PricingDate = price.Key.PricingDate,

                     PricingSource = price.Key.Source,

ProductVersion = price.Max(p=> price.Key.Version)

                    };

and it compiles but again the result is 0

Ayende Rahien

unread,
Sep 1, 2011, 5:17:41 AM9/1/11
to rav...@googlegroups.com
Please show the full text, a failing test case would be ideal

round crisis

unread,
Sep 1, 2011, 5:21:41 AM9/1/11
to rav...@googlegroups.com
thanks for your answer there @ayende

basically I cant do a query as far as I can see because I dont only need the latest date, I need a unique document per day per source

so if I had
DocumentId     price    source  date                                   version
1                     0.85      Bla      2011-09-01:08:00:000          1
2                     0.95      Bla      2011-09-01:09:00:000          2
3                     1.05      Foo      2011-09-01:08:00:000          3
4                     0.85      Foo      2011-09-02:08:00:000          1

it should return documents 2, 3 and 4 . 
2 and 3 because althou the are on the same date they have a different source
and 4 becuase is on a different date

Sorry i dont think I  explained myself properly
 

round crisis

unread,
Sep 1, 2011, 5:25:15 AM9/1/11
to rav...@googlegroups.com
here is the code I m working with at the moment https://gist.github.com/1185789

I could post the test  project if it makes things easier

Ayende Rahien

unread,
Sep 1, 2011, 5:28:13 AM9/1/11
to rav...@googlegroups.com
You need something like:

public PriceDocuments_ByDateBySource()
{
  Map = docs => from priceDocument in docs
          select new
          {
            PricingDate = new DateTime(priceDocument.PricingDate.Year, priceDocument.PricingDate.Month, priceDocument.PricingDate.Day), 
            PricingSource = priceDocument.Source, 
            PriceId = priceDocument.PriceId, 
            ProductVersion = priceDocument.Version
          };
  Reduce = results => from result in results
                      group result by new
                        {
                          result.PricingDate,
                          result.Source,
                        }
                      into price
                      let lastPrice = price.OrderByDescending( p=> p.ProductVersion ).First()
                      select new
                      {
                        lastPrice.PricdeId
                        lastPrice.PricingDay, 
                        lastPrice.PricingSource,
                        lastPrice.ProductVersion
                      };
}

Your previous index definition made no sense, you were selecting different things all over the place.

round crisis

unread,
Sep 1, 2011, 7:41:43 AM9/1/11
to rav...@googlegroups.com
Thanks for your answer, and yes I can see how it made no sense ( trying to figure by try and error sometimes givens me bad results :( )
Anyway, I tried that and is still not returning the anything 

I organized it into a series of unit tests so its easier to check (cant attach (new gist https://gist.github.com/1186006 )  you need to have raven and xunit in your references)

I ve started considering alternative options, but I would really like to know whats wrong

 

Ayende Rahien

unread,
Sep 1, 2011, 8:16:47 AM9/1/11
to rav...@googlegroups.com
The main problem is that you have different names for the fields in the map output than the fields in the Reduce.

I fixed it here


This is how it should look:
public class PriceDocuments_ByDateBySource : AbstractIndexCreationTask<PriceDocument>
{
public PriceDocuments_ByDateBySource()
{
Map = docs => from priceDocument in docs
             select new
             {
              PricingDate =
              new DateTime(priceDocument.PricingDate.Year, priceDocument.PricingDate.Month,
                          priceDocument.PricingDate.Day),
              priceDocument.Source,
              priceDocument.PriceId,
              priceDocument.AskPrice,
              priceDocument.BidPrice,
priceDocument.Version
             };
Reduce = results => from result in results
                   group result by new
                   {
                    result.PricingDate,
                    result.Source,
                   }
                   into price
                   let lastPrice = price.OrderByDescending(p => p.Version).First()
                   select new
                   {
                    lastPrice.PriceId,
                    lastPrice.PricingDate,
                    lastPrice.AskPrice,
                    lastPrice.BidPrice,
                    lastPrice.Source,
                    lastPrice.Version
                   };
}
}


The output of Map and Reduce _HAS_ to be the same. And you had PricingSource and Source and PricingVersion and Version.
You were trying to group by Source, which was never in the Map output (it was PricingSource).

round crisis

unread,
Sep 1, 2011, 10:06:14 AM9/1/11
to rav...@googlegroups.com
That worked! :D
Didnt realise, thanks for all the help :D

Cheers
Reply all
Reply to author
Forward
Message has been deleted
0 new messages