2016-08-12 89 views
0
public class LedgerItem 
{ 
    public string Id { get; set; } 
    ... 
} 

public class Order 
{ 
    ... 
    public IList<OrderLine> OrderLines { get; private set; } 
    ... 
} 

public class OrderLine 
{ 
    ... 
    public List<string> LedgerItemIds { get; set; } 
    ... 
} 

我需要查找所有Order.OrderLines.LedgerItemIds中未由Id引用的所有LedgerItems。相關文檔中未引用的RavenDB查詢文檔

到目前爲止,我已經嘗試創建一個Multi-Map/Reduce索引來從LedgerItems和Orders中選擇所有的LedgerItems,然後通過LedgerItemId和Sum計數來歸類到組中。 然後我的計劃是通過Count == 1進行查詢,因爲這意味着它只在組中存在一次。

public class LedgerItemResult 
{ 
    public string Id { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public string ProTeriaCustomerId { get; set; } 
    public string Collection { get; set; } 
    public int Count { get; set; } 
} 

public class LedgerItemsFromLedgerItemsAndOrdersGroupById : AbstractMultiMapIndexCreationTask<LedgerItemResult> 
{ 
    public LedgerItemsFromLedgerItemsAndOrdersGroupById() 
    { 
     AddMap<LedgerItem>(
      ledgeritems => from ledgeritem in ledgeritems 
      where (decimal)ledgeritem.Amount < 0m 
      select new 
      { 
       Id = ledgeritem.Id, 
       CreatedDate = ledgeritem.TimeStamp, 
       ProTeriaCustomerId = ledgeritem.ProTeriaCustomerId, 
       Collection = MetadataFor(ledgeritem)["Raven-Entity-Name"].ToString(), 
       Count = 1 
      } 
     ); 

     AddMap<Order>(
      orders => from order in orders 
      from orderline in order.OrderLines 
      from ledgeritemid in orderline.LedgerItemIds 
      select new 
      { 
       Id = ledgeritemid, 
       CreatedDate = order.CreatedDateTime, 
       ProTeriaCustomerId = order.ProTeriaCustomerId, 
       Collection = MetadataFor(order)["Raven-Entity-Name"].ToString(), 
       Count = 1 
      } 
     ); 

     Reduce = results => 
      from result in results 
      group result by result.Id into g 
      select new LedgerItemResult 
      { 
       Id = g.First().Id, 
       CreatedDate = g.First().CreatedDate, 
       ProTeriaCustomerId = g.First().ProTeriaCustomerId, 
       Collection = g.First().Collection, 
       Count = g.Sum(x => x.Count) 
      }; 

     Stores.Add(x => x.Id, FieldStorage.Yes); 
     Stores.Add(x => x.CreatedDate, FieldStorage.Yes); 
     Stores.Add(x => x.ProTeriaCustomerId, FieldStorage.Yes); 
     Stores.Add(x => x.Collection, FieldStorage.Yes); 

     MaxIndexOutputsPerDocument = 250; 
    } 
} 

雖然這似乎沒有工作正常。我試着查詢Count == 1和Count == 2,並得到了太多的計數== 1的結果,我手動選中了一個返回的LedgerItemIds,看它是否被一個Order實際引用,它是,儘管它不應該。

可能有更簡單的方法來完成這個,但我仍然有點習慣用SQL來思考。

我希望找到一種方法來實現這一點,以便我可以在Management Studio中使用Lucene語法和代碼中的LiINQ查詢索引。

回答

0

原來我在正確的軌道上,但被騙了一段時間,因爲磁盤幾乎已滿。它可能跨越了「當磁盤空間太少時停止索引」 - 運行索引並且創建的索引不一致時的閾值。