Skip to content

[Backport 7.9] Add index filter to field_caps API #4897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/Nest/Search/FieldCapabilities/FieldCapabilitiesRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elasticsearch.Net;
using System;
using System.Runtime.Serialization;
using Elasticsearch.Net;

namespace Nest
{
[MapsApi("field_caps.json")]
public partial interface IFieldCapabilitiesRequest { }
public partial interface IFieldCapabilitiesRequest
{
[DataMember(Name = "index_filter")]
QueryContainer IndexFilter { get; set; }
}

public partial class FieldCapabilitiesRequest
{
protected override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod HttpMethod => IndexFilter != null? HttpMethod.POST : HttpMethod.GET;

public QueryContainer IndexFilter { get; set; }
}

public partial class FieldCapabilitiesDescriptor
{
protected override HttpMethod HttpMethod => HttpMethod.GET;
QueryContainer IFieldCapabilitiesRequest.IndexFilter { get; set; }

protected override HttpMethod HttpMethod => Self.IndexFilter != null? HttpMethod.POST : HttpMethod.GET;

public FieldCapabilitiesDescriptor IndexFilter<T>(Func<QueryContainerDescriptor<T>, QueryContainer> query) where T : class =>
Assign(query, (a, v) => a.IndexFilter = v?.Invoke(new QueryContainerDescriptor<T>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System;
using System.Linq;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;
Expand Down Expand Up @@ -69,4 +70,64 @@ protected override void ExpectResponse(FieldCapabilitiesResponse response)
jobTitleCapabilities.Searchable.Should().BeTrue();
}
}

[SkipVersion("<7.9.0", "index filter introduced in 7.9.0")]
public class FieldCapabilitiesIndexFilterApiTests
: ApiIntegrationTestBase<ReadOnlyCluster, FieldCapabilitiesResponse, IFieldCapabilitiesRequest, FieldCapabilitiesDescriptor,
FieldCapabilitiesRequest>
{
public FieldCapabilitiesIndexFilterApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

protected override object ExpectJson => new
{
index_filter = new
{
term = new
{
versionControl = new
{
value = "git"
}
}
}
};

protected override bool SupportsDeserialization { get; } = false;

protected override bool ExpectIsValid => true;

protected override int ExpectStatusCode => 200;

protected override Func<FieldCapabilitiesDescriptor, IFieldCapabilitiesRequest> Fluent => d => d
.Fields("*")
.IndexFilter<Project>(q => q
.Term(t => t
.Field(f => f.VersionControl)
.Value(Project.VersionControlConstant)
)
);

protected override HttpMethod HttpMethod => HttpMethod.POST;

protected override FieldCapabilitiesRequest Initializer => new FieldCapabilitiesRequest(Index<Project>().And<Developer>())
{
Fields = "*",
IndexFilter = new TermQuery
{
Field = Field<Project>(f => f.VersionControl),
Value = Project.VersionControlConstant
}
};

protected override string UrlPath => "/project%2Cdevs/_field_caps?fields=%2A";

protected override LazyResponses ClientUsage() => Calls(
(c, f) => c.FieldCapabilities(Index<Project>().And<Developer>(), f),
(c, f) => c.FieldCapabilitiesAsync(Index<Project>().And<Developer>(), f),
(c, r) => c.FieldCapabilities(r),
(c, r) => c.FieldCapabilitiesAsync(r)
);

protected override void ExpectResponse(FieldCapabilitiesResponse response) => response.ShouldBeValid();
}
}