2014-02-06 28 views
1

我正在使用RavenDB批量加載一些文檔。有沒有辦法讓文件加載到數據庫中?RavenDB在BulkInsertOperations之後獲取文檔數

對於插入操作我做:

BulkInsertOperation _bulk = docStore.BulkInsert(null, 
      new BulkInsertOptions{ CheckForUpdates = true}); 

foreach(MyDocument myDoc in docCollection) 
    _bulk.Store(myDoc); 

_bulk.Dispose(); 

,並在之後我撥打以下:

session.Query<MyDocument>().Count(); 

,但我總是得到一個數小於我在烏鴉看到計數工作室。

回答

1

默認情況下,您正在執行的查詢將限制爲相當數量的結果,這是RavenDB承諾在默認情況下爲安全並且不會回傳數百萬條記錄的一部分。

爲了獲取yoru數據庫中特定類型的文檔的數量,您需要一個特殊的map-reduce索引,它的工作是跟蹤每個文檔類型的計數。因爲這種類型的索引直接處理文檔元數據,所以在Raven Studio中定義這個更容易,而不是用代碼創建它。

該索引的來源是this question,但我會在這裏複製:

// Index Name: Raven/DocumentCollections 

// Map Query 
from doc in docs 
let Name = doc["@metadata"]["Raven-Entity-Name"] 
where Name != null 
select new { Name , Count = 1} 

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

然後訪問它在你的代碼,你需要模仿雙方創建匿名類型的結構類在Map和Reduce查詢:

0:

public class Collection 
{ 
    public string Name { get; set; } 
    public int Count { get; set; } 
} 

然後,Ayende在回答中注意到,previously linked question,您可以從索引像這樣得到的結果

但請記住,索引是異步更新的,因此在批量插入一堆文檔後,索引可能過時。您可以通過在.Query(...)之後加上.Customize(x => x.WaitForNonStaleResults())來強制等待。

Raven Studio實際上從索引Raven/DocumentsByEntityName獲取此數據,該數據存在於每個數據庫中,通過避開普通查詢並獲取索引上的元數據。你可以模仿這樣的:

QueryResult result = docStore.DatabaseCommands.Query("Raven/DocumentsByEntityName", 
    new Raven.Abstractions.Data.IndexQuery 
    { 
     Query = "Tag:MyDocument", 
     PageSize = 0 
    }, 
    includes: null, 
    metadataOnly: true); 

var totalDocsOfType = result.TotalResults; 

這QueryResult中包含了很多有用的數據:

{ 
    Results: [ ], 
    Includes: [ ], 
    IsStale: false, 
    IndexTimestamp: "2013-11-08T15:51:25.6463491Z", 
    TotalResults: 3, 
    SkippedResults: 0, 
    IndexName: "Raven/DocumentsByEntityName", 
    IndexEtag: "01000000-0000-0040-0000-00000000000B", 
    ResultEtag: "BA222B85-627A-FABE-DC7C-3CBC968124DE", 
    Highlightings: { }, 
    NonAuthoritativeInformation: false, 
    LastQueryTime: "2014-02-06T18:12:56.1990451Z", 
    DurationMilliseconds: 1 
} 

很多是你在任何查詢相同的數據,如果你要求的統計,像這樣的:

RavenQueryStatistics stats; 

Session.Query<Course>() 
    .Statistics(out stats) 
    // Rest of query 
+0

但是,那麼如何才能烏鴉工作室報告文件沒有索引正確計數? – mll5

+0

Raven Studio在內部使用Raven/DocumentsByEntityName索引上的統計信息,而不是基於標準LINQ的查詢語義。我會更新我的答案。 –