Skip to content

Commit 925128c

Browse files
[Backport master] Add support for _shard_doc sort with pit (#5393)
* Add support for _shard_doc sort with pit (#5374) * Add support for _shard_doc sort with pit * Update test * Fixup namespaces Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent b216b95 commit 925128c

File tree

4 files changed

+104
-5
lines changed

4 files changed

+104
-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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using Elastic.Transport;
10+
using Elasticsearch.Net;
911
using Nest;
1012
using Tests.Core.ManagedElasticsearch.Clusters;
1113
using Tests.Domain;
14+
using Tests.Framework.EndpointTests;
1215
using Tests.Framework.EndpointTests.TestState;
1316
using static Nest.Infer;
1417

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

0 commit comments

Comments
 (0)