| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | namespace Microsoft.Azure.Search.Models |
| | 6 | | { |
| | 7 | | using System; |
| | 8 | | using System.Collections.Generic; |
| | 9 | |
|
| | 10 | | public partial class FacetResult |
| | 11 | | { |
| | 12 | | private const string FromKey = "from"; |
| | 13 | | private const string ToKey = "to"; |
| | 14 | | private const string ValueKey = "value"; |
| | 15 | |
|
| | 16 | | /// <summary> |
| | 17 | | /// Initializes a new instance of the FacetResult class. This constructor is intended to be used for test purpos |
| | 18 | | /// properties of this class are immutable. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="from">A value indicating the inclusive lower bound of the facet's range, or null to indicate th |
| | 21 | | /// lower bound (i.e. -- for the first bucket).</param> |
| | 22 | | /// <param name="to">A value indicating the exclusive upper bound of the facet's range, or null to indicate that |
| | 23 | | /// upper bound (i.e. -- for the last bucket).</param> |
| | 24 | | /// <param name="value">The value of the facet, or the inclusive lower bound if it's an interval facet.</param> |
| | 25 | | /// <param name="count">The approximate count of documents falling within the bucket described by this facet.</p |
| 132 | 26 | | public FacetResult(object from, object to, object value, long? count) : this(new Dictionary<string, object>(), c |
| | 27 | | { |
| 132 | 28 | | AdditionalProperties[FromKey] = from; |
| 132 | 29 | | AdditionalProperties[ToKey] = to; |
| 132 | 30 | | AdditionalProperties[ValueKey] = value; |
| 132 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets a value indicating the type of this facet. |
| | 35 | | /// </summary> |
| 132 | 36 | | public FacetType Type => (Value != null) ? FacetType.Value : FacetType.Range; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets a value indicating the inclusive lower bound of the facet's range, or null to indicate that there is |
| | 40 | | /// no lower bound (i.e. -- for the first bucket). |
| | 41 | | /// </summary> |
| 24 | 42 | | public object From => GetValueOrNull(FromKey); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Gets a value indicating the exclusive upper bound of the facet's range, or null to indicate that there is |
| | 46 | | /// no upper bound (i.e. -- for the last bucket). |
| | 47 | | /// </summary> |
| 24 | 48 | | public object To => GetValueOrNull(ToKey); |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets the value of the facet, or the inclusive lower bound if it's an interval facet. |
| | 52 | | /// </summary> |
| 240 | 53 | | public object Value => GetValueOrNull(ValueKey); |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Attempts to convert the facet to a range facet of the given type. |
| | 57 | | /// </summary> |
| | 58 | | /// <typeparam name="T"> |
| | 59 | | /// A type that matches the type of the field to which the facet was applied. Valid types include |
| | 60 | | /// <c cref="System.DateTimeOffset">DateTimeOffset</c>, <c cref="System.Double">Double</c>, and |
| | 61 | | /// <c cref="System.Int64">Int64</c> (long in C#, int64 in F#). |
| | 62 | | /// </typeparam> |
| | 63 | | /// <returns>A new strongly-typed range facet instance.</returns> |
| | 64 | | /// <exception cref="InvalidCastException">This instance is not a range facet of the given type.</exception> |
| | 65 | | public RangeFacetResult<T> AsRangeFacetResult<T>() where T : struct |
| | 66 | | { |
| 24 | 67 | | if (Type != FacetType.Range) |
| | 68 | | { |
| 0 | 69 | | throw new InvalidCastException(); |
| | 70 | | } |
| | 71 | |
|
| 24 | 72 | | return new RangeFacetResult<T>(Count.GetValueOrDefault(), (T?)From, (T?)To); |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Attempts to convert the facet to a value facet of the given type. |
| | 77 | | /// </summary> |
| | 78 | | /// <typeparam name="T"> |
| | 79 | | /// A type that matches the type of the field to which the facet was applied. |
| | 80 | | /// </typeparam> |
| | 81 | | /// <returns>A new strongly-typed value facet instance.</returns> |
| | 82 | | /// <exception cref="InvalidCastException">This instance is not a value facet of the given type.</exception> |
| | 83 | | public ValueFacetResult<T> AsValueFacetResult<T>() |
| | 84 | | { |
| 108 | 85 | | if (Type != FacetType.Value) |
| | 86 | | { |
| 0 | 87 | | throw new InvalidCastException(); |
| | 88 | | } |
| | 89 | |
|
| 108 | 90 | | return new ValueFacetResult<T>(Count.GetValueOrDefault(), (T)Value); |
| | 91 | | } |
| | 92 | |
|
| 288 | 93 | | private object GetValueOrNull(string key) => AdditionalProperties.TryGetValue(key, out object value) ? value : n |
| | 94 | | } |
| | 95 | | } |