How to use MultiSearchResponse of MultiSearchAsync in Client Elastic search for .NET v8.10

I am currently using the Elasticsearch client for .NET, The version I am using is 8.10
the way I query the information with “MultiSearchAsync” is as follows:

    Query[] collectionQuerys = new Query[]
    {
        new MatchQuery(new Field("amount")) { Query = operationAmount },
        new MatchQuery(new Field("service.trackingId.keyword")) { Query = serviceTrackingId },
        new MatchQuery(new Field("dueDate")) { Query = dueDate },
    };

    BoolQuery boolQuery = new BoolQuery { Must = collectionQuerys };

    listSearchRequestItems.Add(
        new SearchRequestItem(
        new MultisearchHeader { Index = elasticIndex },
        new MultisearchBody { Query = boolQuery }));
}

MultiSearchRequestDescriptor<DirectDebitPaymentResponse> msrd = new MultiSearchRequestDescriptor<DirectDebitPaymentResponse>();

listSearchRequestItems.ForEach(item =>
{
    msrd.AddSearch(item);
});

Elastic.Clients.Elasticsearch.MultiSearchResponse<DirectDebitPaymentResponse> multiSearchResponse = await this.elasticsearchClient.MultiSearchAsync(msrd);

I have problems obtaining the responses since it appears to me that the members are not public.
Can you help me with an example of how to do it, please?

Hi @demet,

the ResponseItem is a union. You have to use the .Match() method to access the internal Item1/Item2 fields:

var resp = await client.MultiSearchAsync<Person>();

foreach (var responseItem in resp.Responses)
{
	// ResponseItem is a union of MultiSearchItem and ErrorResponseBase.
	responseItem.Match(x =>
	{
		// Match MultiSearchItem variant
		Console.WriteLine("Success response item:");
		Console.WriteLine(x.Documents.Count);
	}, x =>
	{
		// Match ErrorResponseBase variant
		Console.WriteLine("Error response item");
		Console.WriteLine(x.Error);
	});
}

The above example calls .Match() for each returned ResponseItem. Match will either invoke the first action (for success responses) or the second action (for error responses).

1 Like

Hi @flobernd.

Thank you for your response; it has worked great for me.

Best regards,