Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null-ref exception when track total hits is disabled #341

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Fix highlight max_analyzer_offset field name to match with the one introduced in OpenSearch 2.2.0 ([#322](https://github.com/opensearch-project/opensearch-net/pull/322))
- Fix null-ref exception when track total hits is disabled ([#341](https://github.com/opensearch-project/opensearch-net/pull/341))

### Removed
- Removed the `Features` API which is not supported by OpenSearch from the low-level client ([#331](https://github.com/opensearch-project/opensearch-net/pull/331))
Expand Down
23 changes: 12 additions & 11 deletions src/OpenSearch.Client/Search/Search/SearchResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public interface ISearchResponse<out TDocument> : IResponse where TDocument : cl
ClusterStatistics Clusters { get; }

/// <summary>
/// Gets the documents inside the hits, by deserializing <see cref="IHitMetadata{T}.Source" /> into <typeparamref name="TDocument" />
/// Gets the documents inside the hits, by deserializing <see cref="IHitMetadata{T}.Source" />
/// into <typeparamref name="TDocument" />
/// <para>
/// NOTE: if you use <see cref="ISearchRequest.StoredFields" /> on the search request,
/// <see cref="Documents" /> will be empty and you should use <see cref="Fields" />
Expand Down Expand Up @@ -143,7 +144,7 @@ public class SearchResponse<TDocument> : ResponseBase, ISearchResponse<TDocument
private IReadOnlyCollection<IHit<TDocument>> _hits;

/// <inheritdoc />
[DataMember(Name ="aggregations")]
[DataMember(Name = "aggregations")]
public AggregateDictionary Aggregations { get; internal set; } = AggregateDictionary.Default;

/// <inheritdoc />
Expand All @@ -170,47 +171,47 @@ public class SearchResponse<TDocument> : ResponseBase, ISearchResponse<TDocument
_hits ?? (_hits = HitsMetadata?.Hits ?? EmptyReadOnly<IHit<TDocument>>.Collection);

/// <inheritdoc />
[DataMember(Name ="hits")]
[DataMember(Name = "hits")]
public IHitsMetadata<TDocument> HitsMetadata { get; internal set; }

/// <inheritdoc />
[IgnoreDataMember]
public double MaxScore => HitsMetadata?.MaxScore ?? 0;

/// <inheritdoc />
[DataMember(Name ="num_reduce_phases")]
[DataMember(Name = "num_reduce_phases")]
public long NumberOfReducePhases { get; internal set; }

/// <inheritdoc />
[DataMember(Name ="profile")]
[DataMember(Name = "profile")]
public Profile Profile { get; internal set; }

/// <inheritdoc />
[DataMember(Name = "_scroll_id")]
public string ScrollId { get; internal set; }

/// <inheritdoc />
[DataMember(Name ="_shards")]
[DataMember(Name = "_shards")]
public ShardStatistics Shards { get; internal set; }

/// <inheritdoc />
[DataMember(Name ="suggest")]
[DataMember(Name = "suggest")]
public ISuggestDictionary<TDocument> Suggest { get; internal set; } = SuggestDictionary<TDocument>.Default;

/// <inheritdoc />
[DataMember(Name ="terminated_early")]
[DataMember(Name = "terminated_early")]
public bool TerminatedEarly { get; internal set; }

/// <inheritdoc />
[DataMember(Name ="timed_out")]
[DataMember(Name = "timed_out")]
public bool TimedOut { get; internal set; }

/// <inheritdoc />
[DataMember(Name ="took")]
[DataMember(Name = "took")]
public long Took { get; internal set; }

/// <inheritdoc />
[IgnoreDataMember]
public long Total => HitsMetadata?.Total.Value ?? -1;
public long Total => HitsMetadata?.Total?.Value ?? -1;
}
}