|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Elasticsearch.Net; |
| 5 | +using FluentAssertions; |
| 6 | +using Nest; |
| 7 | +using Tests.Framework; |
| 8 | +using Tests.Framework.Integration; |
| 9 | +using Tests.Framework.MockData; |
| 10 | + |
| 11 | +namespace Tests.Search.Search |
| 12 | +{ |
| 13 | + public class SearchProfileApiTests : ApiIntegrationTestBase<ReadOnlyCluster, ISearchResponse<Project>, ISearchRequest, |
| 14 | + SearchDescriptor<Project>, SearchRequest<Project>> |
| 15 | + { |
| 16 | + public SearchProfileApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + protected override LazyResponses ClientUsage() => Calls( |
| 21 | + fluent: (c, f) => c.Search(f), |
| 22 | + fluentAsync: (c, f) => c.SearchAsync(f), |
| 23 | + request: (c, r) => c.Search<Project>(r), |
| 24 | + requestAsync: (c, r) => c.SearchAsync<Project>(r) |
| 25 | + ); |
| 26 | + |
| 27 | + protected override int ExpectStatusCode => 200; |
| 28 | + protected override bool ExpectIsValid => true; |
| 29 | + protected override HttpMethod HttpMethod => HttpMethod.POST; |
| 30 | + protected override string UrlPath => $"/project/project/_search"; |
| 31 | + |
| 32 | + protected override object ExpectJson => new |
| 33 | + { |
| 34 | + profile = true, |
| 35 | + query = new |
| 36 | + { |
| 37 | + match_all = new { } |
| 38 | + }, |
| 39 | + aggs = new |
| 40 | + { |
| 41 | + startDates = new |
| 42 | + { |
| 43 | + terms = new |
| 44 | + { |
| 45 | + field = "startedOn" |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + protected override void ExpectResponse(ISearchResponse<Project> response) |
| 52 | + { |
| 53 | + response.Hits.Count().Should().BeGreaterThan(0); |
| 54 | + var profile = response.Profile; |
| 55 | + profile.Should().NotBeNull(); |
| 56 | + var shardProfiles = profile.Shards; |
| 57 | + shardProfiles.Should().NotBeNullOrEmpty(); |
| 58 | + foreach (var shardProfile in shardProfiles) |
| 59 | + { |
| 60 | + shardProfile.Id.Should().NotBeNullOrEmpty(); |
| 61 | + var searches = shardProfile.Searches; |
| 62 | + searches.Should().NotBeNullOrEmpty(); |
| 63 | + foreach (var search in searches) |
| 64 | + { |
| 65 | + var queries = search.Query; |
| 66 | + queries.Should().NotBeNullOrEmpty(); |
| 67 | + foreach (var query in queries) |
| 68 | + { |
| 69 | + query.Should().NotBeNull(); |
| 70 | + query.Type.Should().NotBeNullOrEmpty(); |
| 71 | + query.Description.Should().NotBeNullOrEmpty(); |
| 72 | + query.Time.Should().NotBeNull(); |
| 73 | + query.TimeInNanoseconds.Should().BeGreaterThan(0); |
| 74 | + query.Breakdown.Should().NotBeNull(); |
| 75 | + } |
| 76 | + search.RewriteTime.Should().BeGreaterThan(0); |
| 77 | + var collectors = search.Collector; |
| 78 | + foreach (var collector in collectors) |
| 79 | + { |
| 80 | + collector.Name.Should().NotBeNullOrEmpty(); |
| 81 | + collector.Reason.Should().NotBeNullOrEmpty(); |
| 82 | + collector.Time.Should().NotBeNull(); |
| 83 | + collector.TimeInNanoseconds.Should().BeGreaterThan(0); |
| 84 | + var children = collector.Children; |
| 85 | + children.Should().NotBeNull(); |
| 86 | + foreach (var child in children) |
| 87 | + { |
| 88 | + child.Should().NotBeNull(); |
| 89 | + child.Name.Should().NotBeNullOrEmpty(); |
| 90 | + child.Reason.Should().NotBeNullOrEmpty(); |
| 91 | + child.Time.Should().NotBeNull(); |
| 92 | + child.TimeInNanoseconds.Should().BeGreaterThan(0); |
| 93 | + var grandchildren = child.Children; |
| 94 | + grandchildren.Should().NotBeNull(); |
| 95 | + foreach (var grandchild in grandchildren) |
| 96 | + { |
| 97 | + grandchild.Name.Should().NotBeNullOrEmpty(); |
| 98 | + grandchild.Reason.Should().NotBeNullOrEmpty(); |
| 99 | + grandchild.Time.Should().NotBeNull(); |
| 100 | + grandchild.TimeInNanoseconds.Should().BeGreaterThan(0); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + var aggregations = shardProfile.Aggregations; |
| 106 | + aggregations.Should().NotBeNull(); |
| 107 | + foreach (var aggregation in aggregations) |
| 108 | + { |
| 109 | + aggregation.Should().NotBeNull(); |
| 110 | + aggregation.Type.Should().NotBeNullOrEmpty(); |
| 111 | + aggregation.Description.Should().NotBeNullOrEmpty(); |
| 112 | + aggregation.Time.Should().NotBeNull(); |
| 113 | + aggregation.TimeInNanoseconds.Should().BeGreaterThan(0); |
| 114 | + aggregation.Breakdown.Should().NotBeNull(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s |
| 120 | + .Profile() |
| 121 | + .Query(q => q |
| 122 | + .MatchAll() |
| 123 | + ) |
| 124 | + .Aggregations(aggs => aggs |
| 125 | + .Terms("startDates", t => t |
| 126 | + .Field(p => p.StartedOn) |
| 127 | + ) |
| 128 | + ); |
| 129 | + |
| 130 | + protected override SearchRequest<Project> Initializer => new SearchRequest<Project>() |
| 131 | + { |
| 132 | + Profile = true, |
| 133 | + Query = new QueryContainer(new MatchAllQuery()), |
| 134 | + Aggregations = new TermsAggregation("startDates") |
| 135 | + { |
| 136 | + Field = "startedOn" |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | +} |
0 commit comments