Skip to content

Commit 1f01f26

Browse files
committed
Add max_children to Nested Sort (#4596)
This commit adds max_children to nested sort. Closes #4595 (cherry picked from commit 3110d3e)
1 parent 93b5fe3 commit 1f01f26

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

docs/search/request/sort-usage.asciidoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ s => s
244244
.Filter(ff => ff
245245
.MatchAll()
246246
)
247+
.MaxChildren(50)
247248
)
248249
)
249250
)
@@ -268,7 +269,8 @@ new SearchRequest<Project>
268269
Nested = new NestedSort
269270
{
270271
Path = Field<Project>(p => p.Tags),
271-
Filter = new MatchAllQuery()
272+
Filter = new MatchAllQuery(),
273+
MaxChildren = 50
272274
}
273275
}
274276
}
@@ -289,7 +291,8 @@ new SearchRequest<Project>
289291
"path": "tags",
290292
"filter": {
291293
"match_all": {}
292-
}
294+
},
295+
"max_children": 50
293296
},
294297
"unmapped_type": "date"
295298
}

src/Nest/Search/Search/Sort/NestedSort.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,78 @@
55

66
namespace Nest
77
{
8+
/// <summary>
9+
/// Sort on a field inside one or more nested objects.
10+
/// </summary>
811
[InterfaceDataContract]
912
[ReadAs(typeof(NestedSort))]
1013
public interface INestedSort
1114
{
15+
/// <summary>
16+
/// A filter that the inner objects inside the nested path should match with in order for its field values to be taken into account
17+
/// by sorting. A common pattern is to repeat the query/filter inside the nested filter or query.
18+
/// By default no nested filter is active.
19+
/// </summary>
1220
[DataMember(Name = "filter")]
1321
QueryContainer Filter { get; set; }
1422

23+
/// <summary>
24+
/// Same as top-level nested, but applies to another nested path within the current nested object.
25+
/// </summary>
1526
[DataMember(Name = "nested")]
1627
INestedSort Nested { get; set; }
1728

29+
/// <summary>
30+
/// Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object.
31+
/// When sorting by nested field, this field is mandatory.
32+
/// </summary>
1833
[DataMember(Name = "path")]
1934
Field Path { get; set; }
35+
36+
/// <summary>
37+
/// The maximum number of children to consider per root document when picking the sort value. Defaults to unlimited.
38+
/// </summary>
39+
[DataMember(Name = "max_children")]
40+
int? MaxChildren { get; set; }
2041
}
2142

43+
/// <inheritdoc />
2244
public class NestedSort : INestedSort
2345
{
46+
/// <inheritdoc />
2447
public QueryContainer Filter { get; set; }
48+
/// <inheritdoc />
2549
public INestedSort Nested { get; set; }
50+
/// <inheritdoc />
2651
public Field Path { get; set; }
52+
/// <inheritdoc />
53+
public int? MaxChildren { get; set; }
2754
}
2855

56+
/// <inheritdoc cref="INestedSort"/>
2957
public class NestedSortDescriptor<T>
3058
: DescriptorBase<NestedSortDescriptor<T>, INestedSort>, INestedSort where T : class
3159
{
3260
QueryContainer INestedSort.Filter { get; set; }
3361
INestedSort INestedSort.Nested { get; set; }
3462
Field INestedSort.Path { get; set; }
63+
int? INestedSort.MaxChildren { get; set; }
3564

65+
/// <inheritdoc cref="INestedSort.Path"/>
3666
public NestedSortDescriptor<T> Path(Field path) => Assign(path, (a, v) => a.Path = v);
3767

68+
/// <inheritdoc cref="INestedSort.Path"/>
3869
public NestedSortDescriptor<T> Path<TValue>(Expression<Func<T, TValue>> objectPath) => Assign(objectPath, (a, v) => a.Path = v);
3970

71+
/// <inheritdoc cref="INestedSort.Filter"/>
4072
public NestedSortDescriptor<T> Filter(Func<QueryContainerDescriptor<T>, QueryContainer> filterSelector) =>
4173
Assign(filterSelector, (a, v) => a.Filter = v?.Invoke(new QueryContainerDescriptor<T>()));
4274

75+
/// <inheritdoc cref="INestedSort.Nested"/>
4376
public NestedSortDescriptor<T> Nested(Func<NestedSortDescriptor<T>, INestedSort> filterSelector) =>
4477
Assign(filterSelector, (a, v) => a.Nested = v?.Invoke(new NestedSortDescriptor<T>()));
78+
79+
/// <inheritdoc cref="INestedSort.MaxChildren"/>
80+
public NestedSortDescriptor<T> MaxChildren(int? maxChildren) => Assign(maxChildren, (a, v) => a.MaxChildren = v);
4581
}
4682
}

tests/Tests/Search/Request/SortUsageTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
250250
filter = new
251251
{
252252
match_all = new { }
253-
}
253+
},
254+
max_children = 50
254255
},
255256
unmapped_type = "date"
256257
}
@@ -272,6 +273,7 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
272273
.Filter(ff => ff
273274
.MatchAll()
274275
)
276+
.MaxChildren(50)
275277
)
276278
)
277279
);
@@ -291,7 +293,8 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
291293
Nested = new NestedSort
292294
{
293295
Path = Field<Project>(p => p.Tags),
294-
Filter = new MatchAllQuery()
296+
Filter = new MatchAllQuery(),
297+
MaxChildren = 50
295298
}
296299
}
297300
}

0 commit comments

Comments
 (0)