Skip to content

Commit 4f7eab9

Browse files
committed
implemented clear_scroll API
Conflicts: src/Nest/IElasticClient.cs src/Tests/Nest.Tests.Integration/Core/Suggest/SuggestTests.cs
1 parent 56d9bfc commit 4f7eab9

File tree

7 files changed

+178
-39
lines changed

7 files changed

+178
-39
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using Elasticsearch.Net;
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Converters;
9+
using Nest.Resolvers.Converters;
10+
using System.Linq.Expressions;
11+
using Nest.Resolvers;
12+
13+
namespace Nest
14+
{
15+
[DescriptorFor("ClearScroll")]
16+
public partial class ClearScrollDescriptor : BasePathDescriptor<ClearScrollDescriptor>
17+
, IPathInfo<ClearScrollRequestParameters>
18+
{
19+
internal string _ScrollId { get; set; }
20+
21+
/// <summary>
22+
/// Specify the {name} part of the operation
23+
/// </summary>
24+
public ClearScrollDescriptor ScrollId(string scrollId)
25+
{
26+
this._ScrollId = scrollId;
27+
return this;
28+
}
29+
30+
ElasticsearchPathInfo<ClearScrollRequestParameters> IPathInfo<ClearScrollRequestParameters>.ToPathInfo(IConnectionSettingsValues settings)
31+
{
32+
if (this._ScrollId.IsNullOrEmpty())
33+
throw new DslException("missing ScrollId()");
34+
35+
var pathInfo = new ElasticsearchPathInfo<ClearScrollRequestParameters>();
36+
pathInfo.RequestParameters = this._QueryString;
37+
pathInfo.ScrollId = this._ScrollId;
38+
pathInfo.HttpMethod = PathInfoHttpMethod.DELETE;
39+
40+
return pathInfo;
41+
}
42+
}
43+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Newtonsoft.Json;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Nest
6+
{
7+
public interface IEmptyResponse : IResponse
8+
{
9+
}
10+
11+
[JsonObject]
12+
public class EmptyResponse : BaseResponse, IEmptyResponse
13+
{
14+
public EmptyResponse()
15+
{
16+
this.IsValid = true;
17+
}
18+
}
19+
}

src/Nest/ElasticClient-Scroll.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ namespace Nest
77
public partial class ElasticClient
88
{
99
/// <inheritdoc />
10-
public ISearchResponse<T> Scroll<T>(
11-
Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector)
12-
where T : class
10+
public ISearchResponse<T> Scroll<T>(Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector) where T : class
1311
{
1412
return this.Dispatch<ScrollDescriptor<T>, ScrollRequestParameters, SearchResponse<T>>(
1513
scrollSelector,
@@ -23,9 +21,7 @@ public ISearchResponse<T> Scroll<T>(
2321
}
2422

2523
/// <inheritdoc />
26-
public Task<ISearchResponse<T>> ScrollAsync<T>(
27-
Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector)
28-
where T : class
24+
public Task<ISearchResponse<T>> ScrollAsync<T>(Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector) where T : class
2925
{
3026
return this.DispatchAsync<ScrollDescriptor<T>, ScrollRequestParameters, SearchResponse<T>, ISearchResponse<T>>(
3127
scrollSelector,
@@ -37,5 +33,24 @@ public Task<ISearchResponse<T>> ScrollAsync<T>(
3733
}
3834
);
3935
}
36+
37+
/// <inheritdoc />
38+
public IEmptyResponse ClearScroll(Func<ClearScrollDescriptor, ClearScrollDescriptor> clearScrollSelector)
39+
{
40+
return this.Dispatch<ClearScrollDescriptor, ClearScrollRequestParameters, EmptyResponse>(
41+
clearScrollSelector,
42+
(p, d) => this.RawDispatch.ClearScrollDispatch<EmptyResponse>(p)
43+
);
44+
}
45+
46+
/// <inheritdoc />
47+
public Task<IEmptyResponse> ClearScrollAsync(Func<ClearScrollDescriptor, ClearScrollDescriptor> clearScrollSelector)
48+
{
49+
return this.DispatchAsync<ClearScrollDescriptor, ClearScrollRequestParameters, EmptyResponse, IEmptyResponse>(
50+
clearScrollSelector,
51+
(p, d) => this.RawDispatch.ClearScrollDispatchAsync<EmptyResponse>(p)
52+
);
53+
}
54+
4055
}
4156
}

src/Nest/ElasticClient.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,24 @@ D descriptor
8383
where R : BaseResponse
8484
{
8585
var pathInfo = descriptor.ToPathInfo(this._connectionSettings);
86-
Func<ElasticsearchResponse<R>, D, R> resultSelector =
87-
((c, d) => c.Success || allow404 && c.HttpStatusCode == 404 ? c.Response : CreateInvalidInstance<R>(c));
8886
var response = dispatch(pathInfo, descriptor);
89-
return resultSelector(response, descriptor);
87+
return ResultsSelector<D, Q, R>(response, descriptor, allow404);
9088
}
9189

90+
private static R ResultsSelector<D, Q, R>(ElasticsearchResponse<R> c, D descriptor, bool allow404)
91+
where Q : FluentRequestParameters<Q>, new()
92+
where D : IPathInfo<Q>
93+
where R : BaseResponse
94+
{
95+
if (c.Success || allow404 && c.HttpStatusCode == 404)
96+
{
97+
c.Response.IsValid = true;
98+
return c.Response;
99+
}
100+
var badResponse = CreateInvalidInstance<R>(c);
101+
return badResponse;
102+
103+
}
92104

93105

94106
private static R CreateInvalidInstance<R>(IElasticsearchResponse response) where R : BaseResponse
@@ -125,10 +137,8 @@ D descriptor
125137
where I : IResponse
126138
{
127139
var pathInfo = descriptor.ToPathInfo(this._connectionSettings);
128-
Func<ElasticsearchResponse<R>, D, R> resultSelector =
129-
((c, d) => c.Success || allow404 && c.HttpStatusCode == 404 ? c.Response : CreateInvalidInstance<R>(c));
130140
return dispatch(pathInfo, descriptor)
131-
.ContinueWith<I>(r => resultSelector(r.Result, descriptor));
141+
.ContinueWith<I>(r => ResultsSelector<D, Q, R>(r.Result, descriptor, allow404));
132142
}
133143

134144

src/Nest/IElasticClient.cs

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ public interface IElasticClient
2020
/// <returns>An IObservable you can subscribe to to listen to the progress of the reindexation process</returns>
2121
IObservable<IReindexResponse<T>> Reindex<T>(Func<ReindexDescriptor<T>, ReindexDescriptor<T>> reindexSelector)
2222
where T : class;
23-
24-
/// <summary>
25-
/// A search request can be scrolled by specifying the scroll parameter.
26-
/// <para>The scroll parameter is a time value parameter (for example: scroll=5m),
27-
/// indicating for how long the nodes that participate in the search will maintain relevant resources in
28-
/// order to continue and support it.</para><para>
29-
/// This is very similar in its idea to opening a cursor against a database.</para>
30-
/// <para> </para><para>http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-scroll.html</para>
31-
/// </summary>
32-
/// <typeparam name="T">The type that represents the result hits</typeparam>
33-
/// <param name="scrollSelector">A descriptor that describes the scroll operation</param>
34-
/// <returns>A query response holding T hits as well as the ScrollId for the next scroll operation</returns>
35-
ISearchResponse<T> Scroll<T>(Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector)
36-
where T : class;
37-
38-
/// <summary>
39-
/// A search request can be scrolled by specifying the scroll parameter.
40-
/// <para>The scroll parameter is a time value parameter (for example: scroll=5m),
41-
/// indicating for how long the nodes that participate in the search will maintain relevant resources in
42-
/// order to continue and support it.</para><para>
43-
/// This is very similar in its idea to opening a cursor against a database.</para>
44-
/// </summary>
45-
/// <typeparam name="T">The type that represents the result hits</typeparam>
46-
/// <param name="scrollSelector">A descriptor that describes the scroll operation</param>
47-
/// <returns>A query response holding T hits as well as the ScrollId for the next scroll operation</returns>
48-
Task<ISearchResponse<T>> ScrollAsync<T>(Func<ScrollDescriptor<T>, ScrollDescriptor<T>> scrollSelector)
49-
where T : class;
5023

5124
/// <summary>
5225
/// The update API allows to update a document based on a script provided.
@@ -990,5 +963,11 @@ Task<IBulkResponse> IndexManyAsync<T>(IEnumerable<T> objects, string index = nul
990963
/// </summary>
991964
/// <param name="selector">An optional descriptor that further describes the status operation, i.e limiting it to certain indices</param>
992965
Task<IStatusResponse> StatusAsync(Func<IndicesStatusDescriptor, IndicesStatusDescriptor> selector = null);
966+
967+
/// <inheritdoc />
968+
IEmptyResponse ClearScroll(Func<ClearScrollDescriptor, ClearScrollDescriptor> clearScrollSelector);
969+
970+
/// <inheritdoc />
971+
Task<IEmptyResponse> ClearScrollAsync(Func<ClearScrollDescriptor, ClearScrollDescriptor> clearScrollSelector);
993972
}
994973
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Nest.Tests.MockData.Domain;
5+
using Nest.Resolvers;
6+
using Elasticsearch.Net;
7+
8+
namespace Nest.Tests.Integration.Core.ClearScroll
9+
{
10+
[TestFixture]
11+
public class ClearScrollTests : IntegrationTests
12+
{
13+
[Test]
14+
public void ClearScroll()
15+
{
16+
var searchResults = this._client.Search<ElasticsearchProject>(s => s.Scroll("1m").SearchType(SearchTypeOptions.Scan));
17+
var validScrollId = searchResults.ScrollId;
18+
validScrollId.Should().NotBeNullOrWhiteSpace();
19+
20+
var clearResponse = this._client.ClearScroll(cs => cs.ScrollId(validScrollId));
21+
clearResponse.IsValid.Should().BeTrue();
22+
23+
var bogusClearResponse = this._client.ClearScroll(cs => cs.ScrollId("asdasdadasdasd"));
24+
bogusClearResponse.IsValid.Should().BeFalse();
25+
}
26+
27+
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Linq;
2+
using FluentAssertions;
3+
using NUnit.Framework;
4+
using Nest.Tests.MockData.Domain;
5+
using Nest.Resolvers;
6+
using Elasticsearch.Net;
7+
8+
namespace Nest.Tests.Integration.Core.Suggest
9+
{
10+
[TestFixture]
11+
public class SuggestTests : IntegrationTests
12+
{
13+
[Test]
14+
public void TestSuggest()
15+
{
16+
var country = this._client.Search<ElasticsearchProject>(s => s.Size(1)).Documents.First().Country;
17+
var wrongCountry = country + "x";
18+
19+
var suggestResults = _client.Suggest<ElasticsearchProject>(s => s
20+
.Term("mySuggest", m => m
21+
.SuggestMode(SuggestMode.Always)
22+
.Text(wrongCountry)
23+
.Size(1)
24+
.OnField("country")
25+
)
26+
);
27+
28+
suggestResults.IsValid.Should().BeTrue();
29+
30+
suggestResults.Shards.Should().NotBeNull();
31+
suggestResults.Suggestions.Should().NotBeNull().And.HaveCount(1);
32+
var suggestions = suggestResults.Suggestions["mySuggest"];
33+
suggestions.Should().NotBeNull().And.NotBeEmpty();
34+
35+
var suggestion = suggestions.First();
36+
suggestion.Text.Should().Be(wrongCountry);
37+
var option = suggestion.Options.First();
38+
option.Text.Should().Be(country);
39+
40+
}
41+
42+
43+
}
44+
}

0 commit comments

Comments
 (0)