Skip to content

Commit 8f65007

Browse files
committed
Forward port of Percolate request sort and response score
Based on PR #1655 for 1.x
1 parent 89255ba commit 8f65007

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

Diff for: src/Nest/Search/Percolator/MultiPercolate/IPercolateOperation.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface IPercolateOperation
1616
[JsonProperty(PropertyName = "track_scores")]
1717
bool? TrackScores { get; set; }
1818

19-
[JsonProperty(PropertyName = "score")]
19+
[JsonProperty(PropertyName = "sort")]
2020
IList<ISort> Sort { get; set; }
2121

2222
[JsonProperty(PropertyName = "highlight")]

Diff for: src/Nest/Search/Percolator/Percolate/PercolateRequest.cs

+29-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public partial interface IPercolateRequest<TDocument> : IPercolateOperation
1010
{
1111
[JsonProperty(PropertyName = "doc")]
1212
TDocument Document { get; set; }
13-
1413
}
1514

1615
public partial class PercolateRequest<TDocument>
@@ -26,8 +25,26 @@ public PercolateRequest(Id id) : this(typeof(TDocument), typeof(TDocument), id)
2625
public AggregationDictionary Aggregations { get; set; }
2726

2827
public int? Size { get; set; }
28+
29+
/// <summary>
30+
/// Whether the _score is included for each match. The _score is based on the query and represents
31+
/// how the query matched the percolate query’s metadata, not how the document (that is being percolated)
32+
/// matched the query. The <see cref="Query"/> option is required for this option.
33+
/// </summary>
2934
public bool? TrackScores { get; set; }
35+
36+
/// <summary>
37+
/// The object to percolate
38+
/// </summary>
3039
public TDocument Document { get; set; }
40+
41+
/// <summary>
42+
/// Define a sort specification like in the search API. Currently only sorting _score reverse
43+
/// (default relevancy) is supported. Other sort fields will throw an exception.
44+
/// The <see cref="Size"/> and <see cref="Query"/> option are required for this setting. Like <see cref="TrackScores"/>
45+
/// the score is based on the query and represents how the query matched to the percolate query’s metadata
46+
/// and not how the document being percolated matched to the query.
47+
/// </summary>
3148
public IList<ISort> Sort { get; set; }
3249

3350
IRequestParameters IPercolateOperation.GetRequestParameters() => this.RequestState.RequestParameters;
@@ -63,7 +80,10 @@ public partial class PercolateDescriptor<TDocument> : IPercolateRequest<TDocumen
6380
/// </summary>
6481
public PercolateDescriptor<TDocument> Document(TDocument @object) => Assign(a => a.Document = @object);
6582

66-
/// Make sure we keep calculating score even if we are sorting on a field.
83+
/// <summary>
84+
/// Whether the _score is included for each match. The _score is based on the query and represents
85+
/// how the query matched the percolate query’s metadata, not how the document (that is being percolated)
86+
/// matched the query. The <see cref="Query"/> option is required for this option.
6787
/// </summary>
6888
public PercolateDescriptor<TDocument> TrackScores(bool? trackScores = true) => Assign(a => a.TrackScores = trackScores);
6989

@@ -78,6 +98,13 @@ public PercolateDescriptor<TDocument> Highlight(Func<HighlightDescriptor<TDocume
7898

7999
public PercolateDescriptor<TDocument> Size(int? size) => Assign(a => a.Size = size);
80100

101+
/// <summary>
102+
/// Define a sort specification like in the search API. Currently only sorting _score reverse
103+
/// (default relevancy) is supported. Other sort fields will throw an exception.
104+
/// The <see cref="Size"/> and <see cref="Query"/> option are required for this setting. Like <see cref="TrackScores"/>
105+
/// the score is based on the query and represents how the query matched to the percolate query’s metadata
106+
/// and not how the document being percolated matched to the query.
107+
/// </summary>
81108
public PercolateDescriptor<TDocument> Sort(Func<SortDescriptor<TDocument>, IPromise<IList<ISort>>> selector) => Assign(a => a.Sort = selector?.Invoke(new SortDescriptor<TDocument>())?.Value);
82109

83110
/// <summary>

Diff for: src/Nest/Search/Percolator/Percolate/PercolateResponse.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public interface IPercolateResponse : IPercolateCountResponse
1818
[JsonObject]
1919
public class PercolateCountResponse : BaseResponse, IPercolateCountResponse
2020
{
21-
22-
2321
[JsonProperty(PropertyName = "took")]
2422
public int Took { get; internal set; }
2523

@@ -48,13 +46,17 @@ public class PercolateResponse : PercolateCountResponse, IPercolateResponse
4846

4947
public class PercolatorMatch
5048
{
51-
[JsonProperty(PropertyName = "_index")]
52-
public string Index { get; set; }
53-
[JsonProperty(PropertyName = "_id")]
54-
public string Id { get; set; }
5549
[JsonProperty(PropertyName = "highlight")]
5650
public Dictionary<string, IList<string>> Highlight { get; set; }
57-
51+
52+
[JsonProperty(PropertyName = "_id")]
53+
public string Id { get; set; }
54+
55+
[JsonProperty(PropertyName = "_index")]
56+
public string Index { get; set; }
57+
58+
[JsonProperty(PropertyName = "_score")]
59+
public double Score { get; set; }
5860
}
5961

6062
}

Diff for: src/Tests/Search/Percolator/Percolate/PercolateApiTests.cs

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using Elasticsearch.Net;
45
using FluentAssertions;
6+
using FluentAssertions.Common;
57
using Nest;
68
using Tests.Framework;
79
using Tests.Framework.Integration;
@@ -46,6 +48,7 @@ protected override void ExpectResponse(IPercolateResponse response)
4648
response.Matches.Count().Should().BeGreaterThan(0);
4749
var match = response.Matches.First();
4850
match.Id.Should().Be(PercolatorId);
51+
match.Score.Should().Be(1);
4952
}
5053

5154
private static readonly string PercolatorId = RandomString();
@@ -54,17 +57,28 @@ protected override void OnBeforeCall(IElasticClient client)
5457
{
5558
var register = this.Client.RegisterPercolator<Project>(PercolatorId, r => r
5659
.Index(this.Index)
57-
.Query(q => q .MatchAll())
60+
.Query(q => q.MatchAll())
5861
);
62+
63+
this.Client.Refresh(this.Index);
5964
}
6065

6166
protected override Func<PercolateDescriptor<Project>, IPercolateRequest<Project>> Fluent => c => c
6267
.Index(this.Index)
63-
.Document(Project.Instance);
68+
.Document(Project.Instance)
69+
.Query(q => q.MatchAll())
70+
.Size(10)
71+
.Sort(s => s.Descending(SortSpecialField.Score))
72+
.TrackScores()
73+
;
6474

6575
protected override PercolateRequest<Project> Initializer => new PercolateRequest<Project>(Index, Type<Project>())
6676
{
67-
Document = Project.Instance
77+
Document = Project.Instance,
78+
Query = new QueryContainer(new MatchAllQuery()),
79+
Size = 10,
80+
Sort = new List<ISort> { new SortField { Field = "_score", Order = SortOrder.Descending } },
81+
TrackScores = true
6882
};
6983
}
7084

@@ -74,21 +88,21 @@ public class PercolateExistingDocApiTests : ApiTestBase<IPercolateResponse, IPer
7488
public PercolateExistingDocApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
7589

7690
protected override LazyResponses ClientUsage() => Calls(
77-
fluent: (c, f) => c.Percolate<Project>(p => p.Id(_percId)),
78-
fluentAsync: (c, f) => c.PercolateAsync<Project>(p => p.Id(_percId)),
91+
fluent: (c, f) => c.Percolate<Project>(p => p.Id(_percolateId)),
92+
fluentAsync: (c, f) => c.PercolateAsync<Project>(p => p.Id(_percolateId)),
7993
request: (c, r) => c.Percolate(r),
8094
requestAsync: (c, r) => c.PercolateAsync(r)
8195
);
8296

83-
private string _percId = Project.Instance.Name;
97+
private readonly string _percolateId = Project.Instance.Name;
8498

8599
protected override HttpMethod HttpMethod => HttpMethod.POST;
86-
protected override string UrlPath => $"project/project/{_percId}/_percolate";
100+
protected override string UrlPath => $"project/project/{_percolateId}/_percolate";
87101

88102
protected override bool SupportsDeserialization => false;
89103

90104
protected override Func<PercolateDescriptor<Project>, IPercolateRequest<Project>> Fluent => null;
91105

92-
protected override PercolateRequest<Project> Initializer => new PercolateRequest<Project>(_percId);
106+
protected override PercolateRequest<Project> Initializer => new PercolateRequest<Project>(_percolateId);
93107
}
94108
}

0 commit comments

Comments
 (0)