| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | |
|
| | 6 | | namespace Azure.Search.Documents.Models |
| | 7 | | { |
| | 8 | | public partial class FacetResult |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Gets the type of this facet. Value facets count documents with a |
| | 12 | | /// particular field value and Range facets count documents with a |
| | 13 | | /// field value in a particular range. |
| | 14 | | /// </summary> |
| 10 | 15 | | public FacetType FacetType => (Value != null) ? FacetType.Value : FacetType.Range; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Gets the value of the facet, or the inclusive lower bound if it's |
| | 19 | | /// an interval facet. |
| | 20 | | /// </summary> |
| 14 | 21 | | public object Value => GetValue(Constants.ValueKey); |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Gets a value indicating the inclusive lower bound of the facet's |
| | 25 | | /// range, or null to indicate that there is no lower bound (i.e. -- |
| | 26 | | /// for the first bucket). |
| | 27 | | /// </summary> |
| 6 | 28 | | public object From => GetValue(Constants.FromKey); |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets a value indicating the exclusive upper bound of the facet's |
| | 32 | | /// range, or null to indicate that there is no upper bound (i.e. -- |
| | 33 | | /// for the last bucket). |
| | 34 | | /// </summary> |
| 6 | 35 | | public object To => GetValue(Constants.ToKey); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Get the value of a key like "value" or return null if not found. |
| | 39 | | /// </summary> |
| | 40 | | /// <param name="key">The name of the key to lookup.</param> |
| | 41 | | /// <returns>The value of the key or null.</returns> |
| | 42 | | private object GetValue(string key) => |
| 26 | 43 | | AdditionalProperties.TryGetValue(key, out object value) ? value : null; |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Attempts to convert the facet to a range facet of the given type. |
| | 47 | | /// </summary> |
| | 48 | | /// <typeparam name="T"> |
| | 49 | | /// A type that matches the type of the field to which the facet was |
| | 50 | | /// applied. Valid types include <see cref="DateTimeOffset"/>, |
| | 51 | | /// <see cref="Double"/>, and <see cref="Int64"/>. |
| | 52 | | /// </typeparam> |
| | 53 | | /// <returns>A new strongly-typed range facet instance.</returns> |
| | 54 | | /// <exception cref="InvalidCastException"> |
| | 55 | | /// This instance is not a range facet of the given type. |
| | 56 | | /// </exception> |
| | 57 | | public RangeFacetResult<T> AsRangeFacetResult<T>() where T : struct |
| | 58 | | { |
| 0 | 59 | | if (FacetType != FacetType.Range) { throw new InvalidCastException(); } |
| 6 | 60 | | return new RangeFacetResult<T>(Count.GetValueOrDefault(), (T?)From, (T?)To); |
| | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Attempts to convert the facet to a value facet of the given type. |
| | 65 | | /// </summary> |
| | 66 | | /// <typeparam name="T"> |
| | 67 | | /// A type that matches the type of the field to which the facet was |
| | 68 | | /// applied. |
| | 69 | | /// </typeparam> |
| | 70 | | /// <returns>A new strongly-typed value facet instance.</returns> |
| | 71 | | /// <exception cref="InvalidCastException"> |
| | 72 | | /// This instance is not a value facet of the given type. |
| | 73 | | /// </exception> |
| | 74 | | public ValueFacetResult<T> AsValueFacetResult<T>() |
| | 75 | | { |
| 0 | 76 | | if (FacetType != FacetType.Value) { throw new InvalidCastException(); } |
| 4 | 77 | | return new ValueFacetResult<T>(Count.GetValueOrDefault(), (T)Value); |
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |