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

Allow setting TrackTotalHits to both boolean and integer values #121

Merged
merged 3 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Removed

### Fixed
- Allow passing both boolean and integer values to `TrackTotalHits`.

### Security

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using System;
using System.Diagnostics;
using System.Globalization;
using OpenSearch.Net;
using OpenSearch.Net.Utf8Json;

namespace OpenSearch.Client
{
[JsonFormatter(typeof(TrackTotalHitsFormatter))]
[DebuggerDisplay("{DebugDisplay,nq}")]
public class TrackTotalHits : IEquatable<TrackTotalHits>, IUrlParameter
{
public TrackTotalHits(bool trackTotalHits)
{
Tag = 0;
BoolValue = trackTotalHits;
}

public TrackTotalHits(long trackTotalHitsUpTo)
{
Tag = 1;
LongValue = trackTotalHitsUpTo;
}

public bool Equals(TrackTotalHits other)
{
if (Tag != other?.Tag) return false;

return Tag switch
{
0 => BoolValue == other.BoolValue,
1 => LongValue == other.LongValue,
_ => false
};
}

private byte Tag { get; }

internal bool? BoolValue { get; }

internal long? LongValue { get; }

private string BoolOrLongValue => BoolValue?.ToString(CultureInfo.InvariantCulture) ?? LongValue?.ToString(CultureInfo.InvariantCulture);

private string DebugDisplay => BoolOrLongValue;

public override string ToString() => BoolOrLongValue;

public string GetString(IConnectionConfigurationValues settings) => BoolOrLongValue;

public static implicit operator TrackTotalHits(bool trackTotalHits) => new(trackTotalHits);
public static implicit operator TrackTotalHits(bool? trackTotalHits) => trackTotalHits is {} b ? new TrackTotalHits(b) : null;

public static implicit operator TrackTotalHits(long trackTotalHitsUpTo) => new(trackTotalHitsUpTo);
public static implicit operator TrackTotalHits(long? trackTotalHitsUpTo) => trackTotalHitsUpTo is {} l ? new TrackTotalHits(l) : null;

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;

return obj switch
{
TrackTotalHits t => Equals(t),
bool b => Equals(b),
long l => Equals(l),
_ => false
};
}

private static int TypeHashCode { get; } = typeof(TrackTotalHits).GetHashCode();

public override int GetHashCode()
{
unchecked
{
var result = TypeHashCode;
result = (result * 397) ^ (BoolValue?.GetHashCode() ?? 0);
result = (result * 397) ^ (LongValue?.GetHashCode() ?? 0);
return result;
}
}

public static bool operator ==(TrackTotalHits left, TrackTotalHits right) => Equals(left, right);

public static bool operator !=(TrackTotalHits left, TrackTotalHits right) => !Equals(left, right);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

using OpenSearch.Net.Utf8Json;

namespace OpenSearch.Client
{
internal class TrackTotalHitsFormatter : IJsonFormatter<TrackTotalHits>
{
public void Serialize(ref JsonWriter writer, TrackTotalHits value, IJsonFormatterResolver formatterResolver)
{
if (value?.LongValue is { } l)
writer.WriteInt64(l);
else if (value?.BoolValue is { } b)
writer.WriteBoolean(b);
else
writer.WriteNull();
}

public TrackTotalHits Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) =>
reader.GetCurrentJsonToken() == JsonToken.Number
? new TrackTotalHits(reader.ReadInt64())
: new TrackTotalHits(reader.ReadBoolean());
}
}
2 changes: 1 addition & 1 deletion src/OpenSearch.Client/Requests.NoNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,7 @@ Fields StoredFields
}

[DataMember(Name = "track_total_hits")]
bool? TrackTotalHits
TrackTotalHits TrackTotalHits
{
get;
set;
Expand Down
9 changes: 6 additions & 3 deletions src/OpenSearch.Client/Search/Search/SearchRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public partial class SearchRequest
/// <inheritdoc />
public bool? TrackScores { get; set; }
/// <inheritdoc />
public bool? TrackTotalHits { get; set; }
public TrackTotalHits TrackTotalHits { get; set; }
/// <inheritdoc />
public bool? Version { get; set; }
/// <inheritdoc />
Expand Down Expand Up @@ -333,7 +333,7 @@ public partial class SearchDescriptor<TInferDocument> where TInferDocument : cla
long? ISearchRequest.TerminateAfter { get; set; }
string ISearchRequest.Timeout { get; set; }
bool? ISearchRequest.TrackScores { get; set; }
bool? ISearchRequest.TrackTotalHits { get; set; }
TrackTotalHits ISearchRequest.TrackTotalHits { get; set; }
bool? ISearchRequest.Version { get; set; }
IRuntimeFields ISearchRequest.RuntimeFields { get; set; }

Expand Down Expand Up @@ -506,7 +506,10 @@ public SearchDescriptor<TInferDocument> Rescore(Func<RescoringDescriptor<TInferD
Assign(rescoreSelector, (a, v) => a.Rescore = v?.Invoke(new RescoringDescriptor<TInferDocument>()).Value);

/// <inheritdoc cref="ISearchRequest.TrackTotalHits" />
public SearchDescriptor<TInferDocument> TrackTotalHits(bool? trackTotalHits = true) => Assign(trackTotalHits, (a, v) => a.TrackTotalHits = v);
public SearchDescriptor<TInferDocument> TrackTotalHits() => Assign(true, (a, v) => a.TrackTotalHits = v);

/// <inheritdoc cref="ISearchRequest.TrackTotalHits" />
public SearchDescriptor<TInferDocument> TrackTotalHits(TrackTotalHits trackTotalHits) => Assign(trackTotalHits, (a, v) => a.TrackTotalHits = v);

/// <inheritdoc cref="ISearchRequest.RuntimeFields" />
public SearchDescriptor<TInferDocument> RuntimeFields(Func<RuntimeFieldsDescriptor<TInferDocument>, IPromise<IRuntimeFields>> runtimeFieldsSelector) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1878,9 +1878,9 @@ public bool? TotalHitsAsInteger
}

///<summary>Indicate if the number of documents that match the query should be tracked</summary>
public bool? TrackTotalHits
public string TrackTotalHits
{
get => Q<bool? >("track_total_hits");
get => Q<string>("track_total_hits");
set => Q("track_total_hits", value);
}

Expand Down