Skip to content

Add max_children to Nested Sort #4596

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
Apr 17, 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
7 changes: 5 additions & 2 deletions docs/search/request/sort-usage.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ s => s
.Filter(ff => ff
.MatchAll()
)
.MaxChildren(50)
)
)
)
Expand All @@ -268,7 +269,8 @@ new SearchRequest<Project>
Nested = new NestedSort
{
Path = Field<Project>(p => p.Tags),
Filter = new MatchAllQuery()
Filter = new MatchAllQuery(),
MaxChildren = 50
}
}
}
Expand All @@ -289,7 +291,8 @@ new SearchRequest<Project>
"path": "tags",
"filter": {
"match_all": {}
}
},
"max_children": 50
},
"unmapped_type": "date"
}
Expand Down
36 changes: 36 additions & 0 deletions src/Nest/Search/Search/Sort/NestedSort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,78 @@

namespace Nest
{
/// <summary>
/// Sort on a field inside one or more nested objects.
/// </summary>
[InterfaceDataContract]
[ReadAs(typeof(NestedSort))]
public interface INestedSort
{
/// <summary>
/// A filter that the inner objects inside the nested path should match with in order for its field values to be taken into account
/// by sorting. A common pattern is to repeat the query/filter inside the nested filter or query.
/// By default no nested filter is active.
/// </summary>
[DataMember(Name = "filter")]
QueryContainer Filter { get; set; }

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

/// <summary>
/// Defines on which nested object to sort. The actual sort field must be a direct field inside this nested object.
/// When sorting by nested field, this field is mandatory.
/// </summary>
[DataMember(Name = "path")]
Field Path { get; set; }

/// <summary>
/// The maximum number of children to consider per root document when picking the sort value. Defaults to unlimited.
/// </summary>
[DataMember(Name = "max_children")]
int? MaxChildren { get; set; }
}

/// <inheritdoc />
public class NestedSort : INestedSort
{
/// <inheritdoc />
public QueryContainer Filter { get; set; }
/// <inheritdoc />
public INestedSort Nested { get; set; }
/// <inheritdoc />
public Field Path { get; set; }
/// <inheritdoc />
public int? MaxChildren { get; set; }
}

/// <inheritdoc cref="INestedSort"/>
public class NestedSortDescriptor<T>
: DescriptorBase<NestedSortDescriptor<T>, INestedSort>, INestedSort where T : class
{
QueryContainer INestedSort.Filter { get; set; }
INestedSort INestedSort.Nested { get; set; }
Field INestedSort.Path { get; set; }
int? INestedSort.MaxChildren { get; set; }

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

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

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

/// <inheritdoc cref="INestedSort.Nested"/>
public NestedSortDescriptor<T> Nested(Func<NestedSortDescriptor<T>, INestedSort> filterSelector) =>
Assign(filterSelector, (a, v) => a.Nested = v?.Invoke(new NestedSortDescriptor<T>()));

/// <inheritdoc cref="INestedSort.MaxChildren"/>
public NestedSortDescriptor<T> MaxChildren(int? maxChildren) => Assign(maxChildren, (a, v) => a.MaxChildren = v);
}
}
7 changes: 5 additions & 2 deletions tests/Tests/Search/Request/SortUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
filter = new
{
match_all = new { }
}
},
max_children = 50
},
unmapped_type = "date"
}
Expand All @@ -272,6 +273,7 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
.Filter(ff => ff
.MatchAll()
)
.MaxChildren(50)
)
)
);
Expand All @@ -291,7 +293,8 @@ public NestedSortUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base
Nested = new NestedSort
{
Path = Field<Project>(p => p.Tags),
Filter = new MatchAllQuery()
Filter = new MatchAllQuery(),
MaxChildren = 50
}
}
}
Expand Down