Skip to content

Commit abe6365

Browse files
authored
Add index filter to field_caps API (#4888)
Relates: elastic/elasticsearch#57276
1 parent 8bd1908 commit abe6365

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

src/Nest/Search/FieldCapabilities/FieldCapabilitiesRequest.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@
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 Elasticsearch.Net;
5+
using System;
6+
using System.Runtime.Serialization;
7+
using Elasticsearch.Net;
68

79
namespace Nest
810
{
911
[MapsApi("field_caps.json")]
10-
public partial interface IFieldCapabilitiesRequest { }
12+
public partial interface IFieldCapabilitiesRequest
13+
{
14+
[DataMember(Name = "index_filter")]
15+
QueryContainer IndexFilter { get; set; }
16+
}
1117

1218
public partial class FieldCapabilitiesRequest
1319
{
14-
protected override HttpMethod HttpMethod => HttpMethod.GET;
20+
protected override HttpMethod HttpMethod => IndexFilter != null? HttpMethod.POST : HttpMethod.GET;
21+
22+
public QueryContainer IndexFilter { get; set; }
1523
}
1624

1725
public partial class FieldCapabilitiesDescriptor
1826
{
19-
protected override HttpMethod HttpMethod => HttpMethod.GET;
27+
QueryContainer IFieldCapabilitiesRequest.IndexFilter { get; set; }
28+
29+
protected override HttpMethod HttpMethod => Self.IndexFilter != null? HttpMethod.POST : HttpMethod.GET;
30+
31+
public FieldCapabilitiesDescriptor IndexFilter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> query) where T : class =>
32+
Assign(query, (a, v) => a.IndexFilter = v?.Invoke(new QueryContainerDescriptor<T>()));
2033
}
2134
}

tests/Tests/Search/FieldCapabilities/FieldCapabilitiesApiTests.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
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;
66
using System.Linq;
7+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
78
using Elasticsearch.Net;
89
using FluentAssertions;
910
using Nest;
@@ -69,4 +70,64 @@ protected override void ExpectResponse(FieldCapabilitiesResponse response)
6970
jobTitleCapabilities.Searchable.Should().BeTrue();
7071
}
7172
}
73+
74+
[SkipVersion("<7.9.0", "index filter introduced in 7.9.0")]
75+
public class FieldCapabilitiesIndexFilterApiTests
76+
: ApiIntegrationTestBase<ReadOnlyCluster, FieldCapabilitiesResponse, IFieldCapabilitiesRequest, FieldCapabilitiesDescriptor,
77+
FieldCapabilitiesRequest>
78+
{
79+
public FieldCapabilitiesIndexFilterApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
80+
81+
protected override object ExpectJson => new
82+
{
83+
index_filter = new
84+
{
85+
term = new
86+
{
87+
versionControl = new
88+
{
89+
value = "git"
90+
}
91+
}
92+
}
93+
};
94+
95+
protected override bool SupportsDeserialization { get; } = false;
96+
97+
protected override bool ExpectIsValid => true;
98+
99+
protected override int ExpectStatusCode => 200;
100+
101+
protected override Func<FieldCapabilitiesDescriptor, IFieldCapabilitiesRequest> Fluent => d => d
102+
.Fields("*")
103+
.IndexFilter<Project>(q => q
104+
.Term(t => t
105+
.Field(f => f.VersionControl)
106+
.Value(Project.VersionControlConstant)
107+
)
108+
);
109+
110+
protected override HttpMethod HttpMethod => HttpMethod.POST;
111+
112+
protected override FieldCapabilitiesRequest Initializer => new FieldCapabilitiesRequest(Index<Project>().And<Developer>())
113+
{
114+
Fields = "*",
115+
IndexFilter = new TermQuery
116+
{
117+
Field = Field<Project>(f => f.VersionControl),
118+
Value = Project.VersionControlConstant
119+
}
120+
};
121+
122+
protected override string UrlPath => "/project%2Cdevs/_field_caps?fields=%2A";
123+
124+
protected override LazyResponses ClientUsage() => Calls(
125+
(c, f) => c.FieldCapabilities(Index<Project>().And<Developer>(), f),
126+
(c, f) => c.FieldCapabilitiesAsync(Index<Project>().And<Developer>(), f),
127+
(c, r) => c.FieldCapabilities(r),
128+
(c, r) => c.FieldCapabilitiesAsync(r)
129+
);
130+
131+
protected override void ExpectResponse(FieldCapabilitiesResponse response) => response.ShouldBeValid();
132+
}
72133
}

0 commit comments

Comments
 (0)