Skip to content

Commit 1ea3db3

Browse files
[Backport master] Support point in time searches (#5159)
* Support point in time searches (#5151) * Support point in time searches * Support override of ResolveUrl This allows us to special case pit searches to remove the index parameter. Includes new usage tests for doc generation. * manual fixup Co-authored-by: Steve Gordon <[email protected]>
1 parent b410dbf commit 1ea3db3

File tree

13 files changed

+335
-150
lines changed

13 files changed

+335
-150
lines changed

src/Nest/CommonAbstractions/Request/ApiUrls.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Nest
1414
/// </summary>
1515
internal class ApiUrls
1616
{
17+
private static readonly RouteValues EmptyRouteValues = new();
18+
private readonly string _errorMessageSuffix;
19+
1720
/// <summary>
1821
/// If the spec only defines a single non parameterizable route this allows us to shortcircuit and avoid hitting
1922
/// the cached string builders.
@@ -25,9 +28,7 @@ internal class ApiUrls
2528
/// <see cref="UrlLookup.Matches"/> allows us to quickly find the right url to use in the list.
2629
/// </summary>
2730
public Dictionary<int, List<UrlLookup>> Routes { get; }
28-
29-
private readonly string _errorMessageSuffix;
30-
31+
3132
/// <summary> Only intended to be created once per request and stored in a static </summary>
3233
internal ApiUrls(string[] routes)
3334
{

src/Nest/CommonAbstractions/Request/RequestBase.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ protected RequestBase(Func<RouteValues, RouteValues> pathSelector)
8080

8181
internal abstract ApiUrls ApiUrls { get; }
8282

83-
string IRequest.GetUrl(IConnectionSettingsValues settings) => ApiUrls.Resolve(RequestState.RouteValues, settings);
83+
string IRequest.GetUrl(IConnectionSettingsValues settings) => ResolveUrl(RequestState.RouteValues, settings);
8484

85+
protected virtual string ResolveUrl(RouteValues routeValues, IConnectionSettingsValues settings) => ApiUrls.Resolve(routeValues, settings);
8586

8687
/// <summary>
8788
/// Allows a request implementation to set certain request parameter defaults, use sparingly!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Runtime.Serialization;
6+
using Nest.Utf8Json;
7+
8+
namespace Nest
9+
{
10+
[InterfaceDataContract]
11+
[ReadAs(typeof(PointInTime))]
12+
public interface IPointInTime
13+
{
14+
/// <summary>
15+
/// The ID of the point in time.
16+
/// </summary>
17+
[DataMember(Name = "id")]
18+
string Id { get; set; }
19+
20+
/// <summary>
21+
/// How long to extend the TTL of the point in time.
22+
/// </summary>
23+
[DataMember(Name = "keep_alive")]
24+
Time KeepAlive { get; set; }
25+
}
26+
27+
public class PointInTime : IPointInTime
28+
{
29+
/// <param name="id">The ID of the point in time.</param>
30+
public PointInTime(string id) => Id = id;
31+
32+
/// <param name="id">The ID of the point in time.</param>
33+
/// <param name="keepAlive">How long to extend the TTL of the point in time.</param>
34+
public PointInTime(string id, Time keepAlive)
35+
{
36+
Id = id;
37+
KeepAlive = keepAlive;
38+
}
39+
40+
/// <inheritdoc />
41+
public string Id { get; set; }
42+
43+
/// <inheritdoc />
44+
public Time KeepAlive { get; set; }
45+
}
46+
47+
public class PointInTimeDescriptor : DescriptorBase<PointInTimeDescriptor, IPointInTime>, IPointInTime
48+
{
49+
public PointInTimeDescriptor(string id) => Self.Id = id;
50+
51+
string IPointInTime.Id { get; set; }
52+
Time IPointInTime.KeepAlive { get; set; }
53+
54+
/// <inheritdoc cref="IPointInTime.KeepAlive" />
55+
public PointInTimeDescriptor KeepAlive(Time id) => Assign(id, (a, v) => a.KeepAlive = v);
56+
}
57+
}

src/Nest/Search/Search/SearchRequest.cs

+37
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ public partial interface ISearchRequest : ITypedSearchRequest
167167
/// </summary>
168168
[DataMember(Name = "version")]
169169
bool? Version { get; set; }
170+
171+
[DataMember(Name = "pit")]
172+
IPointInTime PointInTime { get; set; }
170173
}
171174

172175
[ReadAs(typeof(SearchRequest<>))]
@@ -228,6 +231,8 @@ public partial class SearchRequest
228231
public bool? TrackTotalHits { get; set; }
229232
/// <inheritdoc />
230233
public bool? Version { get; set; }
234+
/// <inheritdoc />
235+
public IPointInTime PointInTime { get; set; }
231236

232237
protected override HttpMethod HttpMethod =>
233238
RequestState.RequestParameters?.ContainsQueryString("source") == true
@@ -237,6 +242,16 @@ public partial class SearchRequest
237242
Type ITypedSearchRequest.ClrType => null;
238243

239244
protected sealed override void RequestDefaults(SearchRequestParameters parameters) => TypedKeys = true;
245+
246+
protected override string ResolveUrl(RouteValues routeValues, IConnectionSettingsValues settings)
247+
{
248+
if (Self.PointInTime is object && !string.IsNullOrEmpty(Self.PointInTime.Id) && routeValues.ContainsKey("index"))
249+
{
250+
routeValues.Remove("index");
251+
}
252+
253+
return base.ResolveUrl(routeValues, settings);
254+
}
240255
}
241256

242257
[DataContract]
@@ -282,6 +297,7 @@ public partial class SearchDescriptor<TInferDocument> where TInferDocument : cla
282297
bool? ISearchRequest.TrackScores { get; set; }
283298
bool? ISearchRequest.TrackTotalHits { get; set; }
284299
bool? ISearchRequest.Version { get; set; }
300+
IPointInTime ISearchRequest.PointInTime { get; set; }
285301

286302
protected sealed override void RequestDefaults(SearchRequestParameters parameters) => TypedKeys();
287303

@@ -439,5 +455,26 @@ public SearchDescriptor<TInferDocument> Rescore(Func<RescoringDescriptor<TInferD
439455

440456
/// <inheritdoc cref="ISearchRequest.TrackTotalHits" />
441457
public SearchDescriptor<TInferDocument> TrackTotalHits(bool? trackTotalHits = true) => Assign(trackTotalHits, (a, v) => a.TrackTotalHits = v);
458+
459+
/// <inheritdoc cref="ISearchRequest.PointInTime" />
460+
public SearchDescriptor<TInferDocument> PointInTime(string pitId)
461+
{
462+
Self.PointInTime = new PointInTime(pitId);
463+
return this;
464+
}
465+
466+
/// <inheritdoc cref="ISearchRequest.PointInTime" />
467+
public SearchDescriptor<TInferDocument> PointInTime(string pitId, Func<PointInTimeDescriptor, IPointInTime> pit) =>
468+
Assign(pit, (a, v) => a.PointInTime = v?.Invoke(new PointInTimeDescriptor(pitId)));
469+
470+
protected override string ResolveUrl(RouteValues routeValues, IConnectionSettingsValues settings)
471+
{
472+
if (Self.PointInTime is object && !string.IsNullOrEmpty(Self.PointInTime.Id) && routeValues.ContainsKey("index"))
473+
{
474+
routeValues.Remove("index");
475+
}
476+
477+
return base.ResolveUrl(routeValues, settings);
478+
}
442479
}
443480
}

src/Nest/Search/Search/SearchResponse.cs

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ public interface ISearchResponse<out TDocument> : IResponse where TDocument : cl
110110
/// Gets the total number of documents matching the search query criteria
111111
/// </summary>
112112
long Total { get; }
113+
114+
/// <summary>
115+
/// When a search is made over a point in time, this will be the ID of the point in time.
116+
/// </summary>
117+
string PointInTimeId { get; }
113118
}
114119

115120
public class SearchResponse<TDocument> : ResponseBase, ISearchResponse<TDocument> where TDocument : class
@@ -190,5 +195,9 @@ public class SearchResponse<TDocument> : ResponseBase, ISearchResponse<TDocument
190195
/// <inheritdoc />
191196
[IgnoreDataMember]
192197
public long Total => HitsMetadata?.Total.Value ?? -1;
198+
199+
/// <inheritdoc />
200+
[DataMember(Name = "pit_id")]
201+
public string PointInTimeId { get; internal set; }
193202
}
194203
}

tests/Tests/Search/PointInTime/Close/ClosePointInTimeApiTests.cs

-61
This file was deleted.

tests/Tests/Search/PointInTime/Close/ClosePointInTimeUrlTests.cs

-22
This file was deleted.

tests/Tests/Search/PointInTime/Open/OpenPointInTimeApiTests.cs

-49
This file was deleted.

0 commit comments

Comments
 (0)