File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed
Sources/ElasticsearchQueryBuilder
Tests/ElasticsearchQueryBuilderTests Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -199,6 +199,22 @@ extension esb {
199199 }
200200 }
201201
202+ /// Adds `sort` block to the query syntax.
203+ public struct Sort < Component: ArrayComponent > : DictComponent {
204+ var component : Component
205+ public init ( @QueryArrayBuilder component: ( ) -> Component ) {
206+ self . component = component ( )
207+ }
208+ public func makeDict( ) -> QueryDict {
209+ let values : [ QueryDict ] = self . component. makeCompactArray ( )
210+ if values. isEmpty {
211+ return [ : ]
212+ } else {
213+ return [ " sort " : . array( values. map ( QueryValue . dict) ) ]
214+ }
215+ }
216+ }
217+
202218 /// Adds `term` block to the query syntax.
203219 ///
204220 /// Excludes the component if value is nil.
Original file line number Diff line number Diff line change @@ -256,6 +256,43 @@ final class BoolTests: XCTestCase {
256256 }
257257}
258258
259+ final class SortTests : XCTestCase {
260+ func testBuild( ) throws {
261+ @ElasticsearchQueryBuilder func build( ) -> some esb . QueryDSL {
262+ esb. Sort {
263+ esb. Key ( " _score " ) {
264+ [ " order " : " desc " ]
265+ }
266+ esb. Key ( " name " ) {
267+ [ " order " : " asc " ]
268+ }
269+ }
270+ }
271+ XCTAssertNoDifference ( build ( ) . makeQuery ( ) , [
272+ " sort " : [
273+ [
274+ " _score " : [ " order " : " desc " ]
275+ ] ,
276+ [
277+ " name " : [ " order " : " asc " ]
278+ ]
279+ ]
280+ ] )
281+ }
282+ func testBuildEmpty( ) throws {
283+ @ElasticsearchQueryBuilder func build( ) -> some esb . QueryDSL {
284+ esb. Sort {
285+ if false {
286+ esb. Key ( " _score " ) {
287+ [ " order " : " desc " ]
288+ }
289+ }
290+ }
291+ }
292+ XCTAssertNoDifference ( build ( ) . makeQuery ( ) , [ : ] )
293+ }
294+ }
295+
259296final class TermTests : XCTestCase {
260297 func testBuild( ) throws {
261298 @ElasticsearchQueryBuilder func build( ) -> some esb . QueryDSL {
You can’t perform that action at this time.
0 commit comments