|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Globalization; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Text.Json; |
8 | 9 | using System.Threading.Tasks; |
|
11 | 12 | using Azure.Core.GeoJson; |
12 | 13 | #endif |
13 | 14 | using Azure.Core.TestFramework; |
| 15 | +using Azure.Search.Documents.Indexes; |
14 | 16 | using Azure.Search.Documents.Models; |
15 | 17 | using NUnit.Framework; |
16 | 18 |
|
@@ -663,6 +665,133 @@ await AssertKeysContains( |
663 | 665 | Assert.AreEqual(4, second.Count); |
664 | 666 | } |
665 | 667 |
|
| 668 | + public class FacetKeyValuePair |
| 669 | + { |
| 670 | + public FacetKeyValuePair() { } |
| 671 | + public FacetKeyValuePair(string key, string value) { Key = key; Value = value; } |
| 672 | + |
| 673 | + [SimpleField(IsKey = true)] |
| 674 | + public string Key { get; set; } |
| 675 | + |
| 676 | + [SimpleField(IsFacetable = true)] |
| 677 | + public string Value { get; set; } |
| 678 | + } |
| 679 | + |
| 680 | + [Test] |
| 681 | + public async Task FacetsArentAutomaticallyParsed() |
| 682 | + { |
| 683 | + await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync<FacetKeyValuePair>(this); |
| 684 | + SearchClient client = resources.GetSearchClient(); |
| 685 | + await client.UploadDocumentsAsync( |
| 686 | + new[] |
| 687 | + { |
| 688 | + new FacetKeyValuePair("1", "9-6"), |
| 689 | + new FacetKeyValuePair("2", "9.6"), |
| 690 | + new FacetKeyValuePair("3", "9'6\""), |
| 691 | + }); |
| 692 | + await resources.WaitForIndexingAsync(); |
| 693 | + |
| 694 | + Response<SearchResults<FacetKeyValuePair>> response = |
| 695 | + await resources.GetQueryClient().SearchAsync<FacetKeyValuePair>( |
| 696 | + null, |
| 697 | + new SearchOptions { Facets = new[] { "Value" } }); |
| 698 | + |
| 699 | + Assert.IsNotNull(response.Value.Facets); |
| 700 | + AssertFacetsEqual( |
| 701 | + GetFacetsForField(response.Value.Facets, "Value", 3), |
| 702 | + MakeValueFacet(1, "9'6\""), |
| 703 | + MakeValueFacet(1, "9-6"), |
| 704 | + MakeValueFacet(1, "9.6")); |
| 705 | + |
| 706 | + // Check strongly typed value facets |
| 707 | + ICollection<FacetResult> facets = GetFacetsForField(response.Value.Facets, "Value", 3); |
| 708 | + ValueFacetResult<string> first = facets.ElementAt(0).AsValueFacetResult<string>(); |
| 709 | + Assert.AreEqual("9'6\"", first.Value); |
| 710 | + Assert.AreEqual(1, first.Count); |
| 711 | + ValueFacetResult<string> second = facets.ElementAt(1).AsValueFacetResult<string>(); |
| 712 | + Assert.AreEqual("9-6", second.Value); |
| 713 | + Assert.AreEqual(1, second.Count); |
| 714 | + ValueFacetResult<string> third = facets.ElementAt(2).AsValueFacetResult<string>(); |
| 715 | + Assert.AreEqual("9.6", third.Value); |
| 716 | + Assert.AreEqual(1, third.Count); |
| 717 | + } |
| 718 | + |
| 719 | + [Test] |
| 720 | + public async Task FacetDateTimesRoundtrip() |
| 721 | + { |
| 722 | + await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync<FacetKeyValuePair>(this); |
| 723 | + SearchClient client = resources.GetSearchClient(); |
| 724 | + DateTimeOffset now = Recording.Now; |
| 725 | + |
| 726 | + // Use valid dates |
| 727 | + string prefix = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"; |
| 728 | + FacetKeyValuePair[] data = |
| 729 | + new[] |
| 730 | + { |
| 731 | + new FacetKeyValuePair("1", now.ToString(prefix + "zzz", CultureInfo.InvariantCulture)), |
| 732 | + new FacetKeyValuePair("2", now.ToString(prefix + "K", CultureInfo.InvariantCulture)), |
| 733 | + new FacetKeyValuePair("3", now.ToString(prefix + "'.'fzzz", CultureInfo.InvariantCulture)), |
| 734 | + new FacetKeyValuePair("4", now.ToString(prefix + "'.'fK", CultureInfo.InvariantCulture)), |
| 735 | + new FacetKeyValuePair("5", now.ToString(prefix + "'.'ffzzz", CultureInfo.InvariantCulture)), |
| 736 | + new FacetKeyValuePair("6", now.ToString(prefix + "'.'ffK", CultureInfo.InvariantCulture)), |
| 737 | + new FacetKeyValuePair("7", now.ToString(prefix + "'.'fffzzz", CultureInfo.InvariantCulture)), |
| 738 | + new FacetKeyValuePair("8", now.ToString(prefix + "'.'fffK", CultureInfo.InvariantCulture)), |
| 739 | + new FacetKeyValuePair("9", now.ToString(prefix + "'.'ffffzzz", CultureInfo.InvariantCulture)), |
| 740 | + new FacetKeyValuePair("10", now.ToString(prefix + "'.'ffffK", CultureInfo.InvariantCulture)), |
| 741 | + new FacetKeyValuePair("11", now.ToString(prefix + "'.'fffffzzz", CultureInfo.InvariantCulture)), |
| 742 | + new FacetKeyValuePair("12", now.ToString(prefix + "'.'fffffK", CultureInfo.InvariantCulture)), |
| 743 | + new FacetKeyValuePair("13", now.ToString(prefix + "'.'ffffffzzz", CultureInfo.InvariantCulture)), |
| 744 | + new FacetKeyValuePair("14", now.ToString(prefix + "'.'ffffffK", CultureInfo.InvariantCulture)), |
| 745 | + new FacetKeyValuePair("15", now.ToString(prefix + "'.'fffffffzzz", CultureInfo.InvariantCulture)), |
| 746 | + new FacetKeyValuePair("16", now.ToString(prefix + "'.'fffffffK", CultureInfo.InvariantCulture)) |
| 747 | + }; |
| 748 | + await client.UploadDocumentsAsync(data); |
| 749 | + await resources.WaitForIndexingAsync(); |
| 750 | + |
| 751 | + Response<SearchResults<SearchDocument>> response = |
| 752 | + await resources.GetQueryClient().SearchAsync<SearchDocument>( |
| 753 | + null, |
| 754 | + new SearchOptions { Facets = new[] { "Value,count:" + data.Length } }); |
| 755 | + foreach (FacetResult facet in response.Value.Facets["Value"]) |
| 756 | + { |
| 757 | + Assert.IsInstanceOf( |
| 758 | + typeof(DateTimeOffset), |
| 759 | + facet.Value, |
| 760 | + $"Expected a DateTimeOffset, not {facet.Value} of type {facet.Value?.GetType().FullName}"); |
| 761 | + } |
| 762 | + } |
| 763 | + |
| 764 | + [Test] |
| 765 | + public async Task FacetDateTimesInvalid() |
| 766 | + { |
| 767 | + await using SearchResources resources = await SearchResources.CreateWithEmptyIndexAsync<FacetKeyValuePair>(this); |
| 768 | + SearchClient client = resources.GetSearchClient(); |
| 769 | + DateTimeOffset now = Recording.Now; |
| 770 | + |
| 771 | + // Use invalid dates |
| 772 | + FacetKeyValuePair[] data = |
| 773 | + new[] |
| 774 | + { |
| 775 | + new FacetKeyValuePair("1", now.ToString("yyyy'-'MM'-'dd'T'HH':'mm", CultureInfo.InvariantCulture)), |
| 776 | + new FacetKeyValuePair("2", now.ToString("yyyy'-'MM'-'dd", CultureInfo.InvariantCulture)), |
| 777 | + new FacetKeyValuePair("3", now.ToString("HH':'mm", CultureInfo.InvariantCulture)) |
| 778 | + }; |
| 779 | + await client.UploadDocumentsAsync(data); |
| 780 | + await resources.WaitForIndexingAsync(); |
| 781 | + |
| 782 | + Response<SearchResults<SearchDocument>> response = |
| 783 | + await resources.GetQueryClient().SearchAsync<SearchDocument>( |
| 784 | + null, |
| 785 | + new SearchOptions { Facets = new[] { "Value,count:" + data.Length } }); |
| 786 | + foreach (FacetResult facet in response.Value.Facets["Value"]) |
| 787 | + { |
| 788 | + Assert.IsInstanceOf( |
| 789 | + typeof(string), |
| 790 | + facet.Value, |
| 791 | + $"Expected a string, not {facet.Value} of type {facet.Value?.GetType().FullName}"); |
| 792 | + } |
| 793 | + } |
| 794 | + |
666 | 795 | [Test] |
667 | 796 | public async Task CanContinueStatic() |
668 | 797 | { |
|
0 commit comments