Skip to content

Commit

Permalink
Fix null-ref exception when track total hits is disabled (#341)
Browse files Browse the repository at this point in the history
* Fix null-ref exception when track total hits is disabled

Signed-off-by: Osman Tunçelli <[email protected]>

* update changelog

Signed-off-by: Osman Tunçelli <[email protected]>

---------

Signed-off-by: Osman Tunçelli <[email protected]>
Co-authored-by: Osman Tunçelli <[email protected]>
(cherry picked from commit fcfcae7)
  • Loading branch information
otuncelli authored and github-actions[bot] committed Sep 4, 2023
1 parent c95a977 commit c0c4a49
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [Unreleased]
### 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))

### Dependencies
- Bumps `Microsoft.TestPlatform.ObjectModel` from 17.5.0 to 17.7.1
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;
}
}

0 comments on commit c0c4a49

Please sign in to comment.