Skip to content

Fix #1868 add filter option to _termvectors and _mtermvectors #1897

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
Mar 14, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface IMultiTermVectorOperation
bool? TermStatistics { get; set; }
[JsonProperty("field_statistics")]
bool? FieldStatistics { get; set; }
[JsonProperty("filter")]
ITermVectorFilter Filter { get; set; }
}

public class MultiTermVectorOperation<T> : IMultiTermVectorOperation
Expand All @@ -47,6 +49,7 @@ public MultiTermVectorOperation(Id id)
public bool? Positions { get; set; }
public bool? TermStatistics { get; set; }
public bool? FieldStatistics { get; set; }
public ITermVectorFilter Filter { get; set; }
}

public class MultiTermVectorOperationDescriptor<T> : DescriptorBase<MultiTermVectorOperationDescriptor<T>, IMultiTermVectorOperation>, IMultiTermVectorOperation
Expand All @@ -62,6 +65,7 @@ public class MultiTermVectorOperationDescriptor<T> : DescriptorBase<MultiTermVec
bool? IMultiTermVectorOperation.Positions { get; set; }
bool? IMultiTermVectorOperation.TermStatistics { get; set; }
bool? IMultiTermVectorOperation.FieldStatistics { get; set; }
ITermVectorFilter IMultiTermVectorOperation.Filter { get; set; }

public MultiTermVectorOperationDescriptor<T> Fields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
Assign(a => a.Fields = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
Expand All @@ -77,5 +81,8 @@ public MultiTermVectorOperationDescriptor<T> Fields(Func<FieldsDescriptor<T>, IP
public MultiTermVectorOperationDescriptor<T> TermStatistics(bool termStatistics = true) => Assign(a => a.TermStatistics = termStatistics);

public MultiTermVectorOperationDescriptor<T> FieldStatistics(bool fieldStatistics = true) => Assign(a => a.FieldStatistics = fieldStatistics);

public MultiTermVectorOperationDescriptor<T> Filter(Func<TermVectorFilterDescriptor, ITermVectorFilter> filterSelector) =>
Assign(a => a.Filter = filterSelector?.Invoke(new TermVectorFilterDescriptor()));
}
}
44 changes: 44 additions & 0 deletions src/Nest/Document/Single/TermVectors/TermVectorFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Nest
{
[JsonObject(MemberSerialization.OptIn)]
public interface ITermVectorFilter
{
[JsonProperty("max_num_terms")]
int? MaximumNumberOfTerms { get; set; }

[JsonProperty("min_term_freq")]
int? MinimumTermFrequency { get; set; }

[JsonProperty("min_doc_freq")]
int? MinimumDocumentFrequency { get; set; }
}

public class TermVectorFilter : ITermVectorFilter
{
public int? MaximumNumberOfTerms { get; set; }

public int? MinimumTermFrequency { get; set; }

public int? MinimumDocumentFrequency { get; set; }
}

public class TermVectorFilterDescriptor
: DescriptorBase<TermVectorFilterDescriptor, ITermVectorFilter>, ITermVectorFilter
{
int? ITermVectorFilter.MaximumNumberOfTerms { get; set; }

int? ITermVectorFilter.MinimumDocumentFrequency { get; set; }

int? ITermVectorFilter.MinimumTermFrequency { get; set; }

public TermVectorFilterDescriptor MaximimumNumberOfTerms(int maxNumTerms) => Assign(a => a.MaximumNumberOfTerms = maxNumTerms);

public TermVectorFilterDescriptor MinimumDocumentFrequency(int minDocFreq) => Assign(a => a.MinimumDocumentFrequency = minDocFreq);

public TermVectorFilterDescriptor MinimumTermFrequency(int minTermFreq) => Assign(a => a.MinimumTermFrequency = minTermFreq);
}
}
14 changes: 12 additions & 2 deletions src/Nest/Document/Single/TermVectors/TermVectorsRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ public partial interface ITermVectorsRequest<TDocument>

[JsonProperty("per_field_analyzer")]
IPerFieldAnalyzer PerFieldAnalyzer { get; set; }

[JsonProperty("filter")]
ITermVectorFilter Filter { get; set; }
}

public partial class TermVectorsRequest<TDocument>
where TDocument : class
{
HttpMethod IRequest.HttpMethod => this.Document == null ? HttpMethod.GET : HttpMethod.POST;
HttpMethod IRequest.HttpMethod => (this.Document != null || this.Filter != null) ? HttpMethod.POST : HttpMethod.GET;

public TDocument Document { get; set; }

public IPerFieldAnalyzer PerFieldAnalyzer { get; set; }

public ITermVectorFilter Filter { get; set; }

partial void DocumentFromPath(TDocument document)
{
Self.Document = document;
Expand All @@ -38,15 +43,20 @@ partial void DocumentFromPath(TDocument document)
[DescriptorFor("Termvectors")]
public partial class TermVectorsDescriptor<TDocument> where TDocument : class
{
HttpMethod IRequest.HttpMethod => Self.Document == null ? HttpMethod.GET : HttpMethod.POST;
HttpMethod IRequest.HttpMethod => (Self.Document != null || Self.Filter != null) ? HttpMethod.POST : HttpMethod.GET;

TDocument ITermVectorsRequest<TDocument>.Document { get; set; }

IPerFieldAnalyzer ITermVectorsRequest<TDocument>.PerFieldAnalyzer { get; set; }

ITermVectorFilter ITermVectorsRequest<TDocument>.Filter { get; set; }

public TermVectorsDescriptor<TDocument> Document(TDocument document) => Assign(a => a.Document = document);

public TermVectorsDescriptor<TDocument> PerFieldAnalyzer(Func<PerFieldAnalyzerDescriptor<TDocument>, IPromise<IPerFieldAnalyzer>> analyzerSelector) =>
Assign(a => a.PerFieldAnalyzer = analyzerSelector?.Invoke(new PerFieldAnalyzerDescriptor<TDocument>())?.Value);

public TermVectorsDescriptor<TDocument> Filter(Func<TermVectorFilterDescriptor, ITermVectorFilter> filterSelector) =>
Assign(a => a.Filter = filterSelector?.Invoke(new TermVectorFilterDescriptor()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ protected override LazyResponses ClientUsage() => Calls(
field_statistics = true,
term_statistics = true,
positions = true,
offsets = true
offsets = true,
filter = new
{
max_num_terms = 3,
min_term_freq = 1,
min_doc_freq = 1
}
}).Take(2)
};

Expand Down Expand Up @@ -77,7 +83,18 @@ protected override void ExpectResponse(IMultiTermVectorsResponse response)

protected override Func<MultiTermVectorsDescriptor, IMultiTermVectorsRequest> Fluent => d => d
.Index<Developer>()
.GetMany<Developer>(Developer.Developers.Select(p => p.Id).Take(2), (p, i) => p.FieldStatistics().Payloads().TermStatistics().Positions().Offsets())
.GetMany<Developer>(Developer.Developers.Select(p => p.Id).Take(2), (p, i) => p
.FieldStatistics()
.Payloads()
.TermStatistics()
.Positions()
.Offsets()
.Filter(f => f
.MaximimumNumberOfTerms(3)
.MinimumTermFrequency(1)
.MinimumDocumentFrequency(1)
)
)
;

protected override MultiTermVectorsRequest Initializer => new MultiTermVectorsRequest(Index<Developer>())
Expand All @@ -89,7 +106,13 @@ protected override void ExpectResponse(IMultiTermVectorsResponse response)
Payloads = true,
TermStatistics = true,
Positions = true,
Offsets = true
Offsets = true,
Filter = new TermVectorFilter
{
MaximumNumberOfTerms = 3,
MinimumTermFrequency = 1,
MinimumDocumentFrequency = 1
}
})
};
}
Expand Down
28 changes: 25 additions & 3 deletions src/Tests/Document/Single/TermVectors/TermVectorsApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

namespace Tests.Document.Single.TermVectors
{
[Collection(IntegrationContext.Indexing)]
[Collection(IntegrationContext.ReadOnly)]
public class TermVectorsApiTests : ApiIntegrationTestBase<ITermVectorsResponse, ITermVectorsRequest<Project>, TermVectorsDescriptor<Project>, TermVectorsRequest<Project>>
{
public TermVectorsApiTests(IndexingCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
public TermVectorsApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
protected override LazyResponses ClientUsage() => Calls(
fluent: (client, f) => client.TermVectors(f),
fluentAsync: (client, f) => client.TermVectorsAsync(f),
Expand All @@ -22,20 +22,42 @@ protected override LazyResponses ClientUsage() => Calls(

protected override bool ExpectIsValid => true;
protected override int ExpectStatusCode => 200;
protected override HttpMethod HttpMethod => HttpMethod.GET;
protected override HttpMethod HttpMethod => HttpMethod.POST;
protected override string UrlPath => $"/project/project/{Uri.EscapeDataString(Project.Instance.Name)}/_termvectors?offsets=true";

protected override bool SupportsDeserialization => false;

protected override object ExpectJson => new
{
filter = new
{
max_num_terms = 3,
min_term_freq = 1,
min_doc_freq = 1
}
};

protected override TermVectorsDescriptor<Project> NewDescriptor() => new TermVectorsDescriptor<Project>(typeof (Project), typeof (Project));

protected override Func<TermVectorsDescriptor<Project>, ITermVectorsRequest<Project>> Fluent => d=>d
.Id(Id(Project.Instance))
.Offsets()
.Filter(f => f
.MaximimumNumberOfTerms(3)
.MinimumTermFrequency(1)
.MinimumDocumentFrequency(1)
)
;

protected override TermVectorsRequest<Project> Initializer => new TermVectorsRequest<Project>(Project.Instance.Name)
{
Offsets = true,
Filter = new TermVectorFilter
{
MaximumNumberOfTerms = 3,
MinimumTermFrequency = 1,
MinimumDocumentFrequency = 1
}
};
}
}
4 changes: 2 additions & 2 deletions src/Tests/tests.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# mode either u (unit test), i (integration test) or m (mixed mode)
mode: u
mode: m
# the elasticsearch version that should be started
elasticsearch_version: 2.0.1
elasticsearch_version: 2.2.0
# whether we want to forcefully reseed on the node, if you are starting the tests with a node already running
force_reseed: true
# do not spawn nodes as part of the test setup but rely on a manually started es node being up
Expand Down