Iterate over searchResponse

I'd like to iterate over my searchResponse so I can use the value from an id field in every found document in order to do a new search where this id matches another one in another index.

So first I'm doing this

    var groupSearch = await EsClient().SearchAsync<GroupDto>(s => s
        .Query(q => q
            .Match(m => m
                .Field(f => f.GroupId)
                .Field(f => f.GroupName))
        ));

and then I'd like to do something like this

    SimpleUnitDto unit;
    GroupDto group = (GroupDto)groupSearch;
    group.Units = new List<SimpleUnitDto>();

foreach (Guid groupId in groupSearch.Hits.Select(g => g.Source.GroupId))
{
    var unitSearch = await EsClient().SearchAsync<SimpleUnitDto>(s => s
        .Index("units")
        .Query(q => q
            .MultiMatch(m => m
                .Fields(f => f
                    .Field(ff => ff.UnitId)
                    .Field(ff => ff.UnitName))
            ) && +q
            .Term(t => t
                .Field("groupid")
                .Value(groupId)
        ));

    unit = (SimpleUnitDto)unitSearch;
    group.Units.Add(unit);
    group = groupDict[groupId];
}

return groupDict.Values

This is not a working example but it's how i think it should work.
Could anyone assist?

Today I'm trying out MultiSearchAsync like this

var multiSearch = await EsClient().MultiSearchAsync("*", m => m
    .Search<GroupDto>(s => s
        .AllIndices()
        .Query(q => q
            .Term(t => t
                .Field(f => f.GroupId)
                .Field(f => f.GroupName)
            ) && +q
            .Term(t => t
                .Field("_index")
                .Value("groups")
            )))
    .Search<SimpleUnitDto>(s => s
        .AllIndices()
        .Query(q => q
            .Term(t => t
                .Field(f => f.UnitId)
                .Field(f => f.UnitName)
            ) && +q
            .Term(t => t
                .Field("_index")
                .Value("units")
            )
    )));

Which could be cool if it worked but I still can't figure out a better solution than something like a foreach to iterate over the search response. Unfortunately I can't find much relevant documentation for the multi search api for .net / nest.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.