Ravendb尝试查询子对象导致“无法索引未索引字段”

时间:2018-09-27 09:39:00

标签: ravendb

我认为该类将根据索引进行自我解释:

public class CalendarMatchIndex : AbstractIndexCreationTask<CalendarMatch>
    {

        public CalendarMatchIndex()
        {
            Map = matches => from match in matches
                select new
                {
                     match.CalendarId,
                     match.MatchDate,
                     match.CommunityId,
                     CategoryId = match.ImportData.CategoryId,
                     TeamTypeId = match.ImportData.TeamTypeId,
                     TeamSheetDeadline =match.ImportData.TeamSheetDeadline,
                     ActivityId =match.ImportData.ActivityId,

                };
        }
    }
}

查询:

var query = session.Query<CalendarMatch, CalendarMatchIndex>()
.Where(x => x.ImportData.CategoryId == input.CategoryId);

导致“ importdata.categoryid未索引错误”。我如何查询此字段,而不必求助于使用单独的结果类并且不必存储所有字段?

编辑:添加Json:

{
    "CalendarId": "7ui824avw496",

    "ImportData": {
        "ActivityType": "Tournament",
        "ActivityId": "aqhfl52xbff137",
        "LinkedMatchId": "bykdzj5j11kagf"
    },
    "CommunityId": null
}

3 个答案:

答案 0 :(得分:1)

如果您过去不想创建结果类(ravendb 2.5),则可以在地图上使用以下语法:

Map = matches => from match in matches
                select new
                {
                     match.CalendarId,
                     match.MatchDate,
                     match.CommunityId,
                     ImportData_CategoryId = match.ImportData.CategoryId,
                     ImportData_TeamTypeId = match.ImportData.TeamTypeId,
                     ImportData_TeamSheetDeadline = match.ImportData.TeamSheetDeadline,
                     ImportData_ActivityId = match.ImportData.ActivityId,

                };

使用此地图,您的查询应该可以正常工作

var query = session.Query<CalendarMatch, CalendarMatchIndex>()
    .Where(x => x.ImportData.CategoryId == input.CategoryId);

答案 1 :(得分:0)

查看索引定义,将ImportData.CategoryId索引为CategoryId字段,这意味着查询应如下所示:

var query = session.Query<CalendarMatch, CalendarMatchIndex>()
.Where(x => x.CategoryId == input.CategoryId);

请参阅x.ImportData.CategoryId-> x.CategoryId的查询更改。

答案 2 :(得分:0)

创建一个结构与索引结果相对应的结果类:

public class CalendarMatchResult
{
    public string CalendarId { get; set; }
    public DateTime? MatchDate { get; set; }
    public string CommunityId { get; set; }
    public string CategoryId { get; set; }
    public string TeamTypeId { get; set; }
    public DateTime? TeamSheetDeadline { get; set; }
    public string ActivityId { get; set; }
}

然后,您可以像这样查询索引:

var query = session.Query<CalendarMatchResult, CalendarMatchIndex>()
    .Where(x => x.CategoryId == input.CategoryId);

除非您需要直接为索引检索它们,否则不需要存储这些字段。对于筛选,不存储的字段就很好。