Skip to content

Commit 0e7e4cc

Browse files
authored
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.
1 parent de02c9d commit 0e7e4cc

File tree

13 files changed

+335
-150
lines changed

13 files changed

+335
-150
lines changed

src/Nest/CommonAbstractions/Request/ApiUrls.cs

Lines changed: 4 additions & 3 deletions
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

Lines changed: 2 additions & 1 deletion
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!
Lines changed: 57 additions & 0 deletions
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 Elasticsearch.Net.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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ public partial interface ISearchRequest : ITypedSearchRequest
166166
/// </summary>
167167
[DataMember(Name = "version")]
168168
bool? Version { get; set; }
169+
170+
[DataMember(Name = "pit")]
171+
IPointInTime PointInTime { get; set; }
169172
}
170173

171174
[ReadAs(typeof(SearchRequest<>))]
@@ -227,6 +230,8 @@ public partial class SearchRequest
227230
public bool? TrackTotalHits { get; set; }
228231
/// <inheritdoc />
229232
public bool? Version { get; set; }
233+
/// <inheritdoc />
234+
public IPointInTime PointInTime { get; set; }
230235

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

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

241256
[DataContract]
@@ -281,6 +296,7 @@ public partial class SearchDescriptor<TInferDocument> where TInferDocument : cla
281296
bool? ISearchRequest.TrackScores { get; set; }
282297
bool? ISearchRequest.TrackTotalHits { get; set; }
283298
bool? ISearchRequest.Version { get; set; }
299+
IPointInTime ISearchRequest.PointInTime { get; set; }
284300

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

@@ -438,5 +454,26 @@ public SearchDescriptor<TInferDocument> Rescore(Func<RescoringDescriptor<TInferD
438454

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

src/Nest/Search/Search/SearchResponse.cs

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

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

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

Lines changed: 0 additions & 61 deletions
This file was deleted.

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

Lines changed: 0 additions & 22 deletions
This file was deleted.

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)