Skip to main content
 首页 » 编程设计

c#之知道为什么这个 RavenDB 查询只返回 6 个结果吗

2024年02月13日8jiqing9006

我们在 Raven 中被这个查询难住了,无法弄清楚它到底出了什么问题。我们有一个相当大的索引。它还具有运行的转换,可根据包含标签数组的白名单文档过滤掉结果。

这是索引:

public class Entries_ByWhiteListSearchableFields 
        : AbstractIndexCreationTask<Entry, Entries_ByWhiteListSearchableFields.Result> 
    { 
        public class Result 
        { 
            public string Id { get; set; } 
            public IEnumerable<string> Search { get; set; } 
            public DateTimeOffset? LastPublishedAtUtc { get; set; } 
            public DateTimeOffset LastModifiedAtUtc { get; set; } 
            public DateTimeOffset DisplayAtUtc { get; set; } 
            public PublishStatus PublishStatus { get; set; } 
            public IEnumerable<string> BylineIds { get; set; } 
            public IEnumerable<string> TagIds { get; set; } 
            public IEnumerable<string> ReferenceIds { get; set; } 
            public string SourceStreamConfigId { get; set; } 
            public bool Deleted { get; set; } 
            public IEnumerable<string> FullyQualifiedTagIds { get; set; } 
            public DateTimeOffset? ExpireAtUtc { get; set; } 
        } 
 
        public Entries_ByWhiteListSearchableFields() 
        { 
            this.Map = entries => from entry in entries 
                                  let entrySection = entry.Tags.FirstOrDefault(tag => tag.Schema == "EntrySection") 
                                  where entrySection != null 
                                  select new 
                                  { 
                                      Id = entry.Id, 
                                      Search = entry.Tags 
                                          .Select(x => x.Label) 
                                          .Concat(new[] { entry.Headline }), 
 
                                      LastPublishedAtUtc = entry.LastPublishedAtUtc, 
                                      LastModifiedAtUtc = entry.LastModifiedAtUtc, 
                                      DisplayAtUtc = entry.LastPublishedAtUtc ?? entry.LastModifiedAtUtc, 
                                      PublishStatus = entry.PublishStatus, 
                                      BylineIds = entry.BylineIds, 
                                      TagIds = entry.Tags.Select(x => x.Id), 
                                      ReferenceIds = entry.References.Select(x => x.Id), 
                                      Deleted = entry.Deleted, 
                                      EntrySectionId = entrySection.Id, 
                                      FullyQualifiedTagIds = entry.Tags.Select(t => t.Schema + "." + t.Id), 
                                      ExpireAtUtc = entry.ExpireAtUtc 
                                  }; 
 
            this.TransformResults = 
                (database, entries) => from entry in entries 
                                       let whitelist = database.Load<WhiteList>("whitelistdocid") 
                                       where database.Load<Entry>(entry.Id) 
                                                     .Tags 
                                                     .Select(t => t.Schema + "." + t.Id) 
                                                     .Intersect( 
                                                        whitelist.WhiteList.Select(s => "EntrySection." + s)) 
                                                     .Any() 
                                       select entry; 
            this.Index(x => x.Search, FieldIndexing.Analyzed); 
        } 
    } 

我们已经运行了这个完全相同的索引,但没有转换,它运行得很好。

这个索引似乎也有效,但在一个区域,我们有这个查询:

query = this.session.Advanced.LuceneQuery<Entry, Entries_ByWhiteListSearchableFields>()     
.Statistics(out stats) 
.WhereEquals("Deleted", false) 
.OrderBy("-DisplayAtUtc") 
.Skip(queryModel.PageSize * (queryModel.Page - 1)) 
.Take(queryModel.PageSize) 
.Include("BylineIds") 
.Include("Tags,Id") 
.Include("References,Id") 
.Select(factory.GetEntryDashboardItemViewModel); 

此查询在没有转换的原始索引的情况下工作完美。但是,使用新索引,当我们请求 pageSize 为 100 时,它只会返回 6 个结果。当我们要求 50 时,它只返回几个。当我们要求 25 时,它返回 0。

但是,这一行:

.OrderBy("-DisplayAtUtc") 

似乎引起了问题。当我们注释掉这一行时,它起作用了,但是结果显然没有排序。当我们把它放回去时,同样的问题,只返回了几个结果。

我重命名了一些东西以使其更有意义。如果需要更多信息,请告诉我。

如有任何帮助或建议,我们将不胜感激。

请您参考如下方法:

我假设 TransformResults 中的 where。请注意,TR 发生在分页发生后。因此,您向它提供 100 个项目,TR 将它们过滤掉,但不会尝试获取任何其他结果来填充页面的其余部分。

一般来说,TR不应该做过滤。

您应该在查询中这样做。