Skip to content

Commit a64fbe3

Browse files
committed
Replace IList<Field> with Fields on SearchRequest, kill FluentFieldList
1 parent 6453999 commit a64fbe3

File tree

7 files changed

+80
-65
lines changed

7 files changed

+80
-65
lines changed

src/Nest/CommonAbstractions/Fields/FluentFieldList.cs

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

src/Nest/Nest.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,6 @@
677677
<Compile Include="CommonAbstractions\ConnectionSettings\ConnectionSettings.cs" />
678678
<Compile Include="CommonAbstractions\ConnectionSettings\IConnectionSettingsValues.cs" />
679679
<Compile Include="CommonAbstractions\ForAttribute.cs" />
680-
<Compile Include="CommonAbstractions\Fields\FluentFieldList.cs" />
681680
<Compile Include="Modules\SnapshotAndRestore\Snapshot\Snapshot.cs" />
682681
<Compile Include="Modules\SnapshotAndRestore\Restore\SnapshotRestore.cs" />
683682
<Compile Include="Cluster\ClusterSettings\ClusterPutSettings\ClusterPutSettingsResponse.cs" />

src/Nest/Search/Search/SearchRequest.cs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ public partial interface ISearchRequest : ICovariantSearchRequest
5252
IRescore Rescore { get; set; }
5353

5454
[JsonProperty(PropertyName = "fields")]
55-
IList<Field> Fields { get; set; }
55+
Fields Fields { get; set; }
5656

5757
[JsonProperty(PropertyName = "fielddata_fields")]
58-
IList<Field> FielddataFields { get; set; }
58+
Fields FielddataFields { get; set; }
5959

6060
[JsonProperty(PropertyName = "script_fields")]
6161
IScriptFields ScriptFields { get; set; }
@@ -102,8 +102,8 @@ public partial class SearchRequest
102102
public bool? TrackScores { get; set; }
103103
public double? MinScore { get; set; }
104104
public long? TerminateAfter { get; set; }
105-
public IList<Field> Fields { get; set; }
106-
public IList<Field> FielddataFields { get; set; }
105+
public Fields Fields { get; set; }
106+
public Fields FielddataFields { get; set; }
107107
public IScriptFields ScriptFields { get; set; }
108108
public ISourceFilter Source { get; set; }
109109
public IList<ISort> Sort { get; set; }
@@ -144,8 +144,8 @@ public partial class SearchRequest<T>
144144
public bool? TrackScores { get; set; }
145145
public double? MinScore { get; set; }
146146
public long? TerminateAfter { get; set; }
147-
public IList<Field> Fields { get; set; }
148-
public IList<Field> FielddataFields { get; set; }
147+
public Fields Fields { get; set; }
148+
public Fields FielddataFields { get; set; }
149149
public IScriptFields ScriptFields { get; set; }
150150
public ISourceFilter Source { get; set; }
151151
public IList<ISort> Sort { get; set; }
@@ -207,8 +207,8 @@ public partial class SearchDescriptor<T> where T : class
207207
IRescore ISearchRequest.Rescore { get; set; }
208208
QueryContainer ISearchRequest.Query { get; set; }
209209
QueryContainer ISearchRequest.PostFilter { get; set; }
210-
IList<Field> ISearchRequest.Fields { get; set; }
211-
IList<Field> ISearchRequest.FielddataFields { get; set; }
210+
Fields ISearchRequest.Fields { get; set; }
211+
Fields ISearchRequest.FielddataFields { get; set; }
212212
IScriptFields ISearchRequest.ScriptFields { get; set; }
213213
ISourceFilter ISearchRequest.Source { get; set; }
214214
AggregationDictionary ISearchRequest.Aggregations { get; set; }
@@ -345,34 +345,14 @@ public SearchDescriptor<T> IndicesBoost(Func<FluentDictionary<IndexName, double>
345345
/// Allows to selectively load specific fields for each document
346346
/// represented by a search hit. Defaults to load the internal _source field.
347347
/// </summary>
348-
public SearchDescriptor<T> Fields(params Expression<Func<T, object>>[] expressions) =>
349-
Assign(a => a.Fields = expressions?.Select(e => (Field)e).ToListOrNullIfEmpty());
350-
351-
/// <summary>
352-
/// Allows to selectively load specific fields for each document
353-
/// represented by a search hit. Defaults to load the internal _source field.
354-
/// </summary>
355-
public SearchDescriptor<T> Fields(Func<FluentFieldList<T>, FluentFieldList<T>> properties) =>
356-
Assign(a => a.Fields = properties?.Invoke(new FluentFieldList<T>()).ToListOrNullIfEmpty());
357-
358-
/// <summary>
359-
/// Allows to selectively load specific fields for each document
360-
/// represented by a search hit. Defaults to load the internal _source field.
361-
/// </summary>
362-
public SearchDescriptor<T> Fields(params string[] fields) =>
363-
Assign(a => a.Fields = fields?.Select(f => (Field)f).ToListOrNullIfEmpty());
364-
365-
///<summary>
366-
///A comma-separated list of fields to return as the field data representation of a field for each hit
367-
///</summary>
368-
public SearchDescriptor<T> FielddataFields(params string[] fielddataFields) =>
369-
Assign(a => a.FielddataFields = fielddataFields?.Select(f => (Field)f).ToListOrNullIfEmpty());
348+
public SearchDescriptor<T> Fields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
349+
Assign(a => a.Fields = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
370350

371351
///<summary>
372352
///A comma-separated list of fields to return as the field data representation of a field for each hit
373353
///</summary>
374-
public SearchDescriptor<T> FielddataFields(params Expression<Func<T, object>>[] fielddataFields) =>
375-
Assign(a => a.FielddataFields = fielddataFields?.Select(f => (Field)f).ToListOrNullIfEmpty());
354+
public SearchDescriptor<T> FielddataFields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
355+
Assign(a => a.FielddataFields = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
376356

377357
public SearchDescriptor<T> ScriptFields(Func<ScriptFieldsDescriptor, IPromise<IScriptFields>> selector) =>
378358
Assign(a => a.ScriptFields = selector?.Invoke(new ScriptFieldsDescriptor())?.Value);

src/Tests/Search/Request/FielddataFieldsUsageTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ public FielddataFieldsUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) :
1515
};
1616

1717
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
18-
.FielddataFields(
19-
f => f.Name,
20-
f => f.LeadDeveloper,
21-
f => f.StartedOn
18+
.FielddataFields(fs => fs
19+
.Field(p => p.Name)
20+
.Field(p => p.LeadDeveloper)
21+
.Field(p => p.StartedOn)
2222
);
2323

2424
protected override SearchRequest<Project> Initializer =>
2525
new SearchRequest<Project>
2626
{
27-
FielddataFields = new Field[] { "name", "leadDeveloper", "startedOn" }
27+
FielddataFields = new string [] { "name", "leadDeveloper", "startedOn" }
2828
};
2929
}
3030
}

src/Tests/Search/Request/FieldsUsageTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Nest;
33
using Tests.Framework.Integration;
44
using Tests.Framework.MockData;
5+
using static Nest.Infer;
56

67
namespace Tests.Search.Request
78
{
@@ -15,15 +16,15 @@ public FieldsUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(clu
1516
};
1617

1718
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
18-
.Fields(
19-
f => f.Name,
20-
f => f.StartedOn
19+
.Fields(fs => fs
20+
.Field(p => p.Name)
21+
.Field(p => p.StartedOn)
2122
);
2223

2324
protected override SearchRequest<Project> Initializer =>
2425
new SearchRequest<Project>
2526
{
26-
Fields = new Field[] { "name", "startedOn" }
27+
Fields = Fields<Project>(p => p.Name, p => p.StartedOn)
2728
};
2829
}
2930
}

src/Tests/Search/Search/SearchApiTests.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override LazyResponses ClientUsage() => Calls(
2121
request: (c, r) => c.Search<Project>(r),
2222
requestAsync: (c, r) => c.SearchAsync<Project>(r)
2323
);
24-
24+
2525
protected override int ExpectStatusCode => 200;
2626
protected override bool ExpectIsValid => true;
2727
protected override HttpMethod HttpMethod => HttpMethod.POST;
@@ -61,6 +61,8 @@ protected override LazyResponses ClientUsage() => Calls(
6161
protected override void ExpectResponse(ISearchResponse<Project> response)
6262
{
6363
response.Hits.Count().Should().BeGreaterThan(0);
64+
response.Hits.First().Should().NotBeNull();
65+
response.Hits.First().Source.Should().NotBeNull();
6466
response.Aggregations.Count.Should().BeGreaterThan(0);
6567
var startDates = response.Aggs.Terms("startDates");
6668
startDates.Should().NotBeNull();
@@ -84,7 +86,7 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
8486
protected override SearchRequest<Project> Initializer => new SearchRequest<Project>()
8587
{
8688
From = 10,
87-
Size =20,
89+
Size = 20,
8890
Query = new QueryContainer(new MatchAllQuery()),
8991
Aggregations = new TermsAggregation("startDates")
9092
{
@@ -97,4 +99,57 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
9799
})
98100
};
99101
}
102+
103+
[Collection(IntegrationContext.ReadOnly)]
104+
public class SearchApiFieldsTests : SearchApiTests
105+
{
106+
public SearchApiFieldsTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
107+
108+
protected override Func<SearchDescriptor<Project>, ISearchRequest> Fluent => s => s
109+
.From(10)
110+
.Size(20)
111+
.Query(q => q
112+
.MatchAll()
113+
)
114+
.Aggregations(a => a
115+
.Terms("startDates", t => t
116+
.Field(p => p.StartedOn)
117+
)
118+
)
119+
.PostFilter(f => f
120+
.Term(p => p.State, StateOfBeing.Stable)
121+
)
122+
.Fields(fs => fs
123+
.Field(p => p.Name)
124+
.Field(p => p.NumberOfCommits)
125+
);
126+
127+
protected override SearchRequest<Project> Initializer => new SearchRequest<Project>()
128+
{
129+
From = 10,
130+
Size = 20,
131+
Query = new QueryContainer(new MatchAllQuery()),
132+
Aggregations = new TermsAggregation("startDates")
133+
{
134+
Field = "startedOn"
135+
},
136+
PostFilter = new QueryContainer(new TermQuery
137+
{
138+
Field = "state",
139+
Value = "Stable"
140+
}),
141+
Fields = Infer.Fields<Project>(p => p.Name, p => p.NumberOfCommits)
142+
};
143+
144+
protected override void ExpectResponse(ISearchResponse<Project> response)
145+
{
146+
response.Hits.Count().Should().BeGreaterThan(0);
147+
response.Hits.First().Should().NotBeNull();
148+
response.Hits.First().Fields.ValueOf<Project, string>(p => p.Name).Should().NotBeNullOrEmpty();
149+
response.Hits.First().Fields.ValueOf<Project, int>(p => p.NumberOfCommits).Should().BeGreaterThan(0);
150+
response.Aggregations.Count.Should().BeGreaterThan(0);
151+
var startDates = response.Aggs.Terms("startDates");
152+
startDates.Should().NotBeNull();
153+
}
154+
}
100155
}

src/Tests/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# mode either u (unit test), i (integration test) or m (mixed mode)
2-
mode: u
2+
mode: i
33
# the elasticsearch version that should be started
44
elasticsearch_version: 2.0.1
55
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running

0 commit comments

Comments
 (0)