BMills
BMills

Reputation: 901

Ravendb trying to query child object results in "cannot index not indexed field"

I think the class will be self explanatory based on the index:

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,

                };
        }
    }
}

Query:

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

Results in the 'importdata.categoryid not indexed error'. How do i query this field without resorting to using a separate result class and having to store all fields?

edit: adding Json:

{
    "CalendarId": "7ui824avw496",

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

Upvotes: 0

Views: 146

Answers (3)

Embri
Embri

Reputation: 622

If you don't want to create a result class in the past (ravendb 2.5) i used this syntax on the map:

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,

                };

with this map your query should works:

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

Upvotes: 1

Andrej Krivulč&#237;k
Andrej Krivulč&#237;k

Reputation: 387

Create a result class with structure corresponding to the index results:

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; }
}

Then, you can query the index like this:

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

You don't need to store the fields unless you need to retrieve them directly for the index. For filtering, fields which are not stored are just fine.

Upvotes: 0

David Marko
David Marko

Reputation: 2519

Looking at your index definition, you index ImportData.CategoryId as CategoryId field and this means your query should looks like this:

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

See x.ImportData.CategoryId -> x.CategoryId change in my query.

Upvotes: 0

Related Questions