为什么 RavenDB return 总结果计数错误?

Why does RavenDB return a wrong total result count?

如以下单元测试所示,我希望 stats.TotalResults 为 2,但实际为 3。这是为什么呢?

    [Test]
    public void RavenQueryStatisticsTotalResultsTest1()
    {
        using (var db = _documentStore.OpenSession())
        {
            db.Store(new Club { Name = "Foo1", Type = "Amateur" }); // --> Matches all conditions
            db.Store(new Club { Name = "Foo2", Type = "Professional" });
            db.Store(new Club { Name = "Foo3", Type = "Amateur" }); // --> Matches all conditions
            db.Store(new Club { Name = "Boo1", Type = "Amateur" });
            db.Store(new Club { Name = "Boo2", Type = "Professional" });
            db.SaveChanges();
        }

        WaitForIndexing(_documentStore);

        using (var db = _documentStore.OpenSession())
        {
            RavenQueryStatistics stats;
            var query = db.Query<Club>()
                    .Statistics(out stats)
                    .Where(club => club.Type == "Amateur")
                    .Intersect()
                    .Search(club => club.Name, "Foo*", escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard); 

            var clubs = query.ToList();

            Assert.AreEqual(2, clubs.Count);
            Assert.AreEqual(2, stats.TotalResults); // I expect 2 but was 3! Why is that?
        }
    }

使用多个 where 子句时需要 Intersect。要合并 WhereSearch,您必须将 SearchOptions.And 传递给 Search:

using (var db = _documentStore.OpenSession()) {
    RavenQueryStatistics stats;
    var query = db.Query<Club>()
        .Customize(_ => _.WaitForNonStaleResultsAsOfLastWrite())
            .Statistics(out stats)
            .Where(club => club.Type == "Amateur")
            .Search(
                club => club.Name,
                "Foo*", 
                escapeQueryOptions: EscapeQueryOptions.AllowPostfixWildcard, 
                options:SearchOptions.And);

    var clubs = query.ToList();

    Assert.AreEqual(2, clubs.Count);
    Assert.AreEqual(2, stats.TotalResults); 
}