|
| 1 | +using System; |
| 2 | +using System.Runtime.Serialization; |
| 3 | +using Elasticsearch.Net; |
| 4 | + |
| 5 | +namespace Nest |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Description of the total number of hits of a query. The total hit count |
| 9 | + /// can't generally be computed accurately without visiting all matches, which |
| 10 | + /// is costly for queries that match lots of documents. Given that it is often |
| 11 | + /// enough to have a lower bounds of the number of hits, such as |
| 12 | + /// "there are more than 1000 hits", Elasticsearch has options to stop counting as soon |
| 13 | + /// as a threshold has been reached in order to improve query times. |
| 14 | + /// </summary> |
| 15 | + [JsonFormatter(typeof(TotalHitsFormatter))] |
| 16 | + public class TotalHits |
| 17 | + { |
| 18 | + /// <summary>Whether <see cref="Value"/> is the exact hit count, in which case <see cref="Relation"/> is |
| 19 | + /// <see cref="TotalHitsRelation.EqualTo"/>, or a lower bound of the total hit count, in which case |
| 20 | + /// <see cref="Relation"/> is <see cref="TotalHitsRelation.GreaterThanOrEqualTo"/> |
| 21 | + /// </summary> |
| 22 | + [DataMember(Name = "relation")] |
| 23 | + public TotalHitsRelation? Relation { get; internal set; } |
| 24 | + |
| 25 | + /// <summary>The value of the total hit count. Must be interpreted in the context of <see cref="Relation"/></summary> |
| 26 | + [DataMember(Name = "value")] |
| 27 | + public long Value { get; internal set; } |
| 28 | + } |
| 29 | + |
| 30 | + /// <summary>How the <see cref="TotalHits.Value"/> should be interpreted</summary> |
| 31 | + [StringEnum] |
| 32 | + public enum TotalHitsRelation |
| 33 | + { |
| 34 | + /// <summary>The total hit count is equal to <see cref="TotalHits.Value"/></summary> |
| 35 | + [EnumMember(Value = "eq")] |
| 36 | + EqualTo, |
| 37 | + |
| 38 | + /// <summary>The total hit count is greater than or equal to <see cref="TotalHits.Value"/></summary> |
| 39 | + [EnumMember(Value = "gte")] |
| 40 | + GreaterThanOrEqualTo, |
| 41 | + } |
| 42 | + |
| 43 | + internal class TotalHitsFormatter : IJsonFormatter<TotalHits> |
| 44 | + { |
| 45 | + private static readonly byte[] ValueField = JsonWriter.GetEncodedPropertyNameWithoutQuotation("value"); |
| 46 | + private static readonly byte[] RelationField = JsonWriter.GetEncodedPropertyNameWithoutQuotation("relation"); |
| 47 | + private static readonly EnumFormatter<TotalHitsRelation> RelationFormatter = new EnumFormatter<TotalHitsRelation>(true); |
| 48 | + |
| 49 | + public TotalHits Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) |
| 50 | + { |
| 51 | + switch (reader.GetCurrentJsonToken()) |
| 52 | + { |
| 53 | + case JsonToken.BeginObject: |
| 54 | + var count = 0; |
| 55 | + long value = -1; |
| 56 | + TotalHitsRelation? relation = null; |
| 57 | + while (reader.ReadIsInObject(ref count)) |
| 58 | + { |
| 59 | + var propertyName = reader.ReadPropertyNameSegmentRaw(); |
| 60 | + if (propertyName.EqualsBytes(ValueField)) |
| 61 | + value = reader.ReadInt64(); |
| 62 | + else if (propertyName.EqualsBytes(RelationField)) |
| 63 | + relation = RelationFormatter.Deserialize(ref reader, formatterResolver); |
| 64 | + else |
| 65 | + reader.ReadNextBlock(); |
| 66 | + } |
| 67 | + |
| 68 | + return new TotalHits { Value = value, Relation = relation }; |
| 69 | + case JsonToken.Number: |
| 70 | + return new TotalHits { Value = reader.ReadInt64() }; |
| 71 | + default: |
| 72 | + reader.ReadNextBlock(); |
| 73 | + return null; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void Serialize(ref JsonWriter writer, TotalHits value, IJsonFormatterResolver formatterResolver) |
| 78 | + { |
| 79 | + if (value == null) |
| 80 | + { |
| 81 | + writer.WriteNull(); |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + if (value.Relation.HasValue) |
| 86 | + { |
| 87 | + writer.WriteBeginObject(); |
| 88 | + writer.WritePropertyName("value"); |
| 89 | + writer.WriteInt64(value.Value); |
| 90 | + writer.WriteValueSeparator(); |
| 91 | + writer.WritePropertyName("relation"); |
| 92 | + RelationFormatter.Serialize(ref writer, value.Relation.Value, formatterResolver); |
| 93 | + writer.WriteEndObject(); |
| 94 | + } |
| 95 | + else |
| 96 | + writer.WriteInt64(value.Value); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments