-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add strongly typed FacetResult helpers for Search (#12620)
* Add strongly typed FacetResult helpers for Search Fixes #10613. We kept this open for a long time hoping we'd be able to do something nifty using type info, but there's just not a great answer so we're stealing the Track 1 experience.
- Loading branch information
Showing
7 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
sdk/search/Azure.Search.Documents/src/Models/FacetResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Azure.Search.Documents.Models | ||
{ | ||
public partial class FacetResult | ||
{ | ||
/// <summary> | ||
/// Gets the type of this facet. Value facets count documents with a | ||
/// particular field value and Range facets count documents with a | ||
/// field value in a particular range. | ||
/// </summary> | ||
public FacetType FacetType => (Value != null) ? FacetType.Value : FacetType.Range; | ||
|
||
/// <summary> | ||
/// Gets the value of the facet, or the inclusive lower bound if it's | ||
/// an interval facet. | ||
/// </summary> | ||
public object Value => GetValue(Constants.ValueKey); | ||
|
||
/// <summary> | ||
/// Gets a value indicating the inclusive lower bound of the facet's | ||
/// range, or null to indicate that there is no lower bound (i.e. -- | ||
/// for the first bucket). | ||
/// </summary> | ||
public object From => GetValue(Constants.FromKey); | ||
|
||
/// <summary> | ||
/// Gets a value indicating the exclusive upper bound of the facet's | ||
/// range, or null to indicate that there is no upper bound (i.e. -- | ||
/// for the last bucket). | ||
/// </summary> | ||
public object To => GetValue(Constants.ToKey); | ||
|
||
/// <summary> | ||
/// Get the value of a key like "value" or return null if not found. | ||
/// </summary> | ||
/// <param name="key">The name of the key to lookup.</param> | ||
/// <returns>The value of the key or null.</returns> | ||
private object GetValue(string key) => | ||
AdditionalProperties.TryGetValue(key, out object value) ? value : null; | ||
|
||
/// <summary> | ||
/// Attempts to convert the facet to a range facet of the given type. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// A type that matches the type of the field to which the facet was | ||
/// applied. Valid types include <see cref="DateTimeOffset"/>, | ||
/// <see cref="Double"/>, and <see cref="Int64"/>. | ||
/// </typeparam> | ||
/// <returns>A new strongly-typed range facet instance.</returns> | ||
/// <exception cref="InvalidCastException"> | ||
/// This instance is not a range facet of the given type. | ||
/// </exception> | ||
public RangeFacetResult<T> AsRangeFacetResult<T>() where T : struct | ||
{ | ||
if (FacetType != FacetType.Range) { throw new InvalidCastException(); } | ||
return new RangeFacetResult<T>(Count.GetValueOrDefault(), (T?)From, (T?)To); | ||
} | ||
|
||
/// <summary> | ||
/// Attempts to convert the facet to a value facet of the given type. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// A type that matches the type of the field to which the facet was | ||
/// applied. | ||
/// </typeparam> | ||
/// <returns>A new strongly-typed value facet instance.</returns> | ||
/// <exception cref="InvalidCastException"> | ||
/// This instance is not a value facet of the given type. | ||
/// </exception> | ||
public ValueFacetResult<T> AsValueFacetResult<T>() | ||
{ | ||
if (FacetType != FacetType.Value) { throw new InvalidCastException(); } | ||
return new ValueFacetResult<T>(Count.GetValueOrDefault(), (T)Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
namespace Azure.Search.Documents.Models | ||
{ | ||
/// <summary> | ||
/// Specifies the type of a facet query result. | ||
/// </summary> | ||
public enum FacetType | ||
{ | ||
/// <summary> | ||
/// The facet counts documents with a particular field value. | ||
/// </summary> | ||
Value = 0, | ||
|
||
/// <summary> | ||
/// The facet counts documents with a field value in a particular range. | ||
/// </summary> | ||
Range | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
sdk/search/Azure.Search.Documents/src/Models/RangeFacetResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Azure.Search.Documents.Models | ||
{ | ||
/// <summary> | ||
/// A single bucket of a range facet query result that reports the number | ||
/// of documents with a field value falling within a particular range. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// A type that matches the type of the field to which the facet was | ||
/// applied. Valid types include <see cref="DateTimeOffset"/>, | ||
/// <see cref="Double"/>, and <see cref="Int64"/>. | ||
/// </typeparam> | ||
public class RangeFacetResult<T> where T : struct | ||
{ | ||
/// <summary> | ||
/// Gets the approximate count of documents falling within the bucket | ||
/// described by this facet. | ||
/// </summary> | ||
public long Count { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating the inclusive lower bound of the facet's | ||
/// range, or <c>null</c> to indicate that there is no lower bound | ||
/// (for the first bucket). | ||
/// </summary> | ||
public T? From { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating the exclusive upper bound of the facet's | ||
/// range, or <c>null</c> to indicate that there is no upper bound | ||
/// (for the last bucket). | ||
/// </summary> | ||
public T? To { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of the RangeFacetResult class. | ||
/// </summary> | ||
/// <param name="count"> | ||
/// The approximate count of documents falling within the bucket | ||
/// described by this facet. | ||
/// </param> | ||
/// <param name="from"> | ||
/// A value indicating the inclusive lower bound of the facet's range, | ||
/// or <c>null</c> to indicate that there is no lower bound (for the | ||
/// first bucket). | ||
/// </param> | ||
/// <param name="to"> | ||
/// A value indicating the exclusive upper bound of the facet's range, | ||
/// or <c>null</c> to indicate that there is no upper bound (for the | ||
/// last bucket). | ||
/// </param> | ||
public RangeFacetResult(long count, T? from, T? to) | ||
{ | ||
From = from; | ||
To = to; | ||
Count = count; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
sdk/search/Azure.Search.Documents/src/Models/ValueFacetResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
namespace Azure.Search.Documents.Models | ||
{ | ||
/// <summary> | ||
/// A single bucket of a simple or interval facet query result that reports | ||
/// the number of documents with a field falling within a particular | ||
/// interval or having a specific value. | ||
/// </summary> | ||
/// <typeparam name="T"> | ||
/// A type that matches the type of the field to which the facet was | ||
/// applied. | ||
/// </typeparam> | ||
public class ValueFacetResult<T> | ||
{ | ||
/// <summary> | ||
/// Gets the approximate count of documents falling within the bucket | ||
/// described by this facet. | ||
/// </summary> | ||
public long Count { get; } | ||
|
||
/// <summary> | ||
/// Gets the value of the facet, or the inclusive lower bound if it's | ||
/// an interval facet. | ||
/// </summary> | ||
public T Value { get; } | ||
|
||
/// <summary> | ||
/// Creates a new instance of the ValueFacetResult class. | ||
/// </summary> | ||
/// <param name="count"> | ||
/// The approximate count of documents falling within the bucket | ||
/// described by this facet. | ||
/// </param> | ||
/// <param name="value"> | ||
/// The value of the facet, or the inclusive lower bound if it's an | ||
/// interval facet. | ||
/// </param> | ||
public ValueFacetResult(long count, T value) | ||
{ | ||
Value = value; | ||
Count = count; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters