2011-08-31 5 views
4

내가 같이이 문서에 정의 주어진 RavenDB 문서RavenDB 지수는 별개의

이상 필요 약간 복잡한 인덱스가 감소

012 : PricingDate (최신) 다음과 같은 데이터를 제공 Source

그래서 의해 고유 이 (내가 쿼리 할 때 그렇게 할 수 있습니다)

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 = 1 
}; 

그 결과 최신 제품인 priceDocument1을 사용해야합니다.

가 지금까지 인덱스는 다음과 같이 정의 :

Map = docs => 
    from priceDocument in docs 
    select new { 
     PricingDate = priceDocument.PricingDate, 
     PricingSource = priceDocument.Source, 
     Price = priceDocument.Price, 
     PriceId = priceDocument.PriceId 
    }; 

Reduce = results => 
    from result in results 
    group result by new { result.PricingDate, result.Source } into price 
    select new { 
     PricingDate = price.Max(p => price.Key.PricingDate), 
     PricingSource = price.Key.Source, 
    }; 

그러나 런타임에 작업을 나던, 나는 AbstractIndexingExecuter||8||Failed to index documents for index (my index name)

나는 별도의 프로젝트에이 샘플을 다시 한

과납니다

Cannot implicitly convert type 'System.DateTimeOffset' to 'int' changed the DateTime? to DateTime

그리고 운 : 나는 오류가 있음을 나는 까마귀 스튜디오에서 통계에 것을 볼 수 있습니다.

는 나는 이제 인덱스는 다음과 같습니다 증가에 의존 할 수있는 I를 사용하여 날짜를 사용하는 전환 :
Map = docs => 
    from priceDocument in docs 
    select new { 
     PricingDate = priceDocument.PricingDate, 
     PricingSource = priceDocument.Source, 
     ProductId = priceDocument.ProductId, 
     ProductVersion = priceDocument.Version 
    }; 

Reduce = results => 
    from result in results 
    group result by new { 
     result.PriceId, 
     result.PricingDate, 
     result.Source, 
     result.Version 
    } into price 
    select new { 
     PricingDate = price.Key.PricingDate, 
     PricingSource = price.Key, 
     ProductVersion = price.Max(p=> price.Key.Version) 
    }; 

지금 그 오류가 발생하지 않습니다, 그러나 그것은 또한 어떤 결과를 제공하지 않습니다.

PricingDate = price.Max(p => (DateTimeOffset)price.Key.PricingDate) 

을하지만, 당신이 원하는 아무튼 :

답변

5

PricingDate = price.Max(p => price.Key.PricingDate) 

에서, 인덱스의 감소 부분이 줄을 변경 시도 지도 감축 지수가 필요합니다. 그냥 사용하여 해당를 얻을 수 있습니다 :

session.Query<PriceDocument>() 
     .Where(x=>x.ProduceId == prodId) 
     .OrderByDescending(x=>x.PriceDate) 
     .FirstOrDefault(); 

이유 당신의지도/작동하지 않는 인덱스를 절감하면 &이 기능을 감소지도에 대해 서로 다른 출력을 가지고있다.

+0

안녕하세요. 답변을 주셔서 감사합니다. 가격이 PriceDate와 PricingSource에서 고유해야합니다 (예 : 2 행을 포함 할 수 있습니다. 소스가 다른 한 동일한 날짜). 버전 (또는 날짜)이 가장 높은 버전을 선택하십시오. – roundcrisis

+0

그러나 전체 답변은 아니지만 @ayende는 까마귀 토론 그룹 https://groups.google에서 질문에 답변했습니다. co.kr/d/topic/ravendb/B9HCmi32YWw/토론 및 솔루션은 https://gist.github.com/1186051 여기에 있습니다. – roundcrisis

3

이 아마해야

PricingDate = price.Max(p => (int)price.Key.PricingDate) 
+0

감사합니다. 결과를 오류가 없지만 결과가 없습니다. – roundcrisis