You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description of the problem including expected versus actual behavior:
This might seem like an edge case but since two different teams ran in to this little gotcha individually. I would just make sure you where aware of it.
Accessing SearchResponse<T>.Hits may cause System.NullReferenceException
Steps to reproduce:
Simplified project to show behavior:
usingElastic.Clients.Elasticsearch;usingElastic.Clients.Elasticsearch.QueryDsl;usingElastic.Transport;usingSystem.Text.Json;varsettings=newElasticsearchClientSettings(newUri("https://localhost:9200")).DisableDirectStreaming().Authentication(newBasicAuthentication("elastic","YOUR_ELASTIC_PASSWORD"));varclient=newElasticsearchClient(settings);awaitclient.Indices.CreateAsync("my_index");varresponse1=awaitclient.SearchAsync<MyDoc>(s =>s.Index("my_index").Query(q =>q.MatchAll(newMatchAllQuery())));Console.WriteLine(JsonSerializer.Serialize(response1.Hits));varresponse2=awaitclient.SearchAsync<MyDoc>(s =>s.Index("my_index").Query(q =>q.MatchAll(newMatchAllQuery())).FilterPath("hits.hits._source"));try{Console.WriteLine(JsonSerializer.Serialize(response2.Hits));}catch(Exceptione){Console.WriteLine("Error:"+e.Message);}awaitclient.IndexAsync(newMyDoc{Id=42,Name="name"}, i =>i.Index("my_index").Refresh(Refresh.True));varresponse3=awaitclient.SearchAsync<MyDoc>(s =>s.Index("my_index").Query(q =>q.MatchAll(newMatchAllQuery())).FilterPath("hits.hits._source"));Console.WriteLine(JsonSerializer.Serialize(response3.Hits));classMyDoc{publicintId{get;set;}publicrequiredstringName{get;set;}}
Expected behavior
When the result set is empty in query 2 the expected output is [] but instead the exception is thrown. This example is of cause simplified but there exist valid reasons for filtering aggressively in returned data.
Just FYI: Using FilterPath always involves a certain risk since you actively remove properties from the response. This might even fundamentally break the client in some cases.
Elastic.Clients.Elasticsearch version:
8.17.3
Elasticsearch version:
8.17.2
.NET runtime version:
net9.0
Operating system version:
Docker/Windows
Description of the problem including expected versus actual behavior:
This might seem like an edge case but since two different teams ran in to this little gotcha individually. I would just make sure you where aware of it.
Accessing
SearchResponse<T>.Hits
may causeSystem.NullReferenceException
Steps to reproduce:
Simplified project to show behavior:
NullableElasticClient.csproj
Program.cs
Expected behavior
When the result set is empty in query 2 the expected output is
[]
but instead the exception is thrown. This example is of cause simplified but there exist valid reasons for filtering aggressively in returned data.Looking at
It would be expected that Hits would guard against null similar to Total.
Thanks for a great product.
The text was updated successfully, but these errors were encountered: