Skip to content

Commit f8e1637

Browse files
authored
Add support for _shard_doc sort with pit (#5374)
* Add support for _shard_doc sort with pit * Update test
1 parent 8f1c5bd commit f8e1637

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

src/Nest/Search/Search/Sort/FieldSort.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ public interface IFieldSort : ISort
2323

2424
public class FieldSort : SortBase, IFieldSort
2525
{
26+
private const string ShardDoc = "_shard_doc";
27+
2628
public static readonly IList<ISort> ByDocumentOrder = new ReadOnlyCollection<ISort>(new List<ISort> { new FieldSort { Field = "_doc" } });
29+
public static readonly IList<ISort> ByShardDocumentOrder = new ReadOnlyCollection<ISort>(new List<ISort> { new FieldSort { Field = ShardDoc } });
30+
31+
public static readonly FieldSort ShardDocumentOrderAscending = new() { Field = ShardDoc, Order = SortOrder.Ascending };
32+
public static readonly FieldSort ShardDocumentOrderDescending = new() { Field = ShardDoc, Order = SortOrder.Descending };
33+
2734
public Field Field { get; set; }
2835
public bool? IgnoreUnmappedFields { get; set; }
2936
public FieldType? UnmappedType { get; set; }

src/Nest/Search/Search/Sort/SortSpecialField.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public enum SortSpecialField
1515
Score,
1616

1717
[EnumMember(Value = "_doc")]
18-
DocumentIndexOrder
18+
DocumentIndexOrder,
19+
20+
[EnumMember(Value = "_shard_doc")]
21+
ShardDocumentOrder
1922
}
2023
}

tests/Tests/Search/PointInTime/PointInTimeApiTests.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5-
using System;
5+
using System.Collections.Generic;
66
using System.Linq;
77
using System.Threading.Tasks;
88
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
@@ -21,6 +21,7 @@ public class PointInTimeApiTests : CoordinatedIntegrationTestBase<ReadOnlyCluste
2121
{
2222
private const string OpenPointInTimeStep = nameof(OpenPointInTimeStep);
2323
private const string SearchPointInTimeStep = nameof(SearchPointInTimeStep);
24+
private const string SearchPointInTimeWithSortStep = nameof(SearchPointInTimeWithSortStep);
2425
private const string ClosePointInTimeStep = nameof(ClosePointInTimeStep);
2526

2627
public PointInTimeApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(new CoordinatedUsage(cluster, usage)
@@ -60,6 +61,31 @@ public PointInTimeApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(
6061
uniqueValueSelector: values => values.ExtendedValue<string>("pitId")
6162
)
6263
},
64+
{
65+
SearchPointInTimeWithSortStep, ">=7.12.0", u =>
66+
u.Calls<SearchDescriptor<Project>, SearchRequest<Project>, ISearchRequest<Project>, ISearchResponse<Project>>(
67+
v => new SearchRequest<Project>
68+
{
69+
Size = 1,
70+
Query = new QueryContainer(new MatchAllQuery()),
71+
PointInTime = new Nest.PointInTime(v, "1m"),
72+
Sort =new List<ISort>
73+
{
74+
FieldSort.ShardDocumentOrderDescending
75+
}
76+
},
77+
(v, d) => d
78+
.Size(1)
79+
.Query(q => q.MatchAll())
80+
.PointInTime(v, p => p.KeepAlive("1m"))
81+
.Sort(s => s.Descending(SortSpecialField.ShardDocumentOrder)),
82+
(v, c, f) => c.Search(f),
83+
(v, c, f) => c.SearchAsync(f),
84+
(v, c, r) => c.Search<Project>(r),
85+
(v, c, r) => c.SearchAsync<Project>(r),
86+
uniqueValueSelector: values => values.ExtendedValue<string>("pitId")
87+
)
88+
},
6389
{
6490
ClosePointInTimeStep, u =>
6591
u.Calls<ClosePointInTimeDescriptor, ClosePointInTimeRequest, IClosePointInTimeRequest, ClosePointInTimeResponse>(
@@ -74,13 +100,13 @@ public PointInTimeApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(
74100
}
75101
}) { }
76102

77-
[I] public async Task OpenPointInTimeResponse() => await Assert<OpenPointInTimeResponse>(OpenPointInTimeStep, (v, r) =>
103+
[I] public async Task OpenPointInTimeResponse() => await Assert<OpenPointInTimeResponse>(OpenPointInTimeStep, r =>
78104
{
79105
r.ShouldBeValid();
80106
r.Id.Should().NotBeNullOrEmpty();
81107
});
82108

83-
[I] public async Task SearchPointInTimeResponse() => await Assert<SearchResponse<Project>>(SearchPointInTimeStep, (v, r) =>
109+
[I] public async Task SearchPointInTimeResponse() => await Assert<SearchResponse<Project>>(SearchPointInTimeStep, r =>
84110
{
85111
r.ShouldBeValid();
86112
r.PointInTimeId.Should().NotBeNullOrEmpty();
@@ -93,7 +119,15 @@ [I] public async Task SearchPointInTimeResponse() => await Assert<SearchResponse
93119
r.Took.Should().BeGreaterOrEqualTo(0);
94120
});
95121

96-
[I] public async Task ClosePointInTimeResponse() => await Assert<ClosePointInTimeResponse>(ClosePointInTimeStep, (v, r) =>
122+
[I] public async Task SearchPointInTimeWithSortResponse() => await Assert<SearchResponse<Project>>(SearchPointInTimeWithSortStep, r =>
123+
{
124+
r.ShouldBeValid();
125+
r.PointInTimeId.Should().NotBeNullOrEmpty();
126+
r.Total.Should().BeGreaterThan(0);
127+
r.Hits.Count.Should().BeGreaterThan(0);
128+
});
129+
130+
[I] public async Task ClosePointInTimeResponse() => await Assert<ClosePointInTimeResponse>(ClosePointInTimeStep, r =>
97131
{
98132
r.ShouldBeValid();
99133
r.Succeeded.Should().BeTrue();

tests/Tests/Search/Request/SortUsageTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using Elasticsearch.Net;
910
using Nest;
1011
using Tests.Core.ManagedElasticsearch.Clusters;
1112
using Tests.Domain;
13+
using Tests.Framework.EndpointTests;
1214
using Tests.Framework.EndpointTests.TestState;
1315
using static Nest.Infer;
1416

@@ -400,4 +402,56 @@ public NumericTypeUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : bas
400402
}
401403
};
402404
}
405+
406+
//hide
407+
[SkipVersion("<7.12.0", "_shard_doc added in 7.12.0")]
408+
public class ShardDocUsageTests : ApiIntegrationTestBase<ReadOnlyCluster, ISearchResponse<Project>, ISearchRequest, SearchDescriptor<Project>, SearchRequest<Project>>
409+
{
410+
public ShardDocUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
411+
412+
private string _pit = string.Empty;
413+
414+
protected override void IntegrationSetup(IElasticClient client, CallUniqueValues values)
415+
{
416+
var response = client.OpenPointInTime(Nest.Indices.Index<Project>(), f => f.KeepAlive("1m"));
417+
_pit = response.Id;
418+
}
419+
420+
protected override object ExpectJson =>
421+
new
422+
{
423+
pit = new
424+
{
425+
id = ""
426+
},
427+
sort = new object[]
428+
{
429+
new { _shard_doc = new { order = "asc" } }
430+
}
431+
};
432+
433+
protected override HttpMethod HttpMethod => HttpMethod.POST;
434+
protected override string UrlPath => "/project/_search";
435+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
436+
.PointInTime(_pit)
437+
.Sort(ss => ss.Ascending(SortSpecialField.ShardDocumentOrder));
438+
439+
protected override bool ExpectIsValid => true;
440+
protected override int ExpectStatusCode => 200;
441+
442+
protected override SearchRequest<Project> Initializer => new()
443+
{
444+
PointInTime = new Nest.PointInTime(_pit),
445+
Sort = new List<ISort>
446+
{
447+
FieldSort.ShardDocumentOrderAscending
448+
}
449+
};
450+
451+
protected override LazyResponses ClientUsage() => Calls(
452+
(client, f) => client.Search(f),
453+
(client, f) => client.SearchAsync(f),
454+
(client, r) => client.Search<Project>(r),
455+
(client, r) => client.SearchAsync<Project>(r));
456+
}
403457
}

0 commit comments

Comments
 (0)