Improved sort method

This commit is contained in:
delvedor
2020-09-10 17:34:20 +02:00
parent 459e0b0cf9
commit cf2c0c3297
2 changed files with 26 additions and 10 deletions

View File

@ -274,8 +274,10 @@ class FluentQ {
return this
}
sort (key: string | any[], opts: Record<string, any>): this
sort (key: string | any[], opts: any): this {
sort (key: string | string[]): this
sort (key: string | string[], order: T.SortOrder): this
sort (key: string | string[], opts: T.Sort): this
sort (key: string | string[], opts?: any): this {
this[kState].push(Q.sort(key, opts))
return this
}
@ -289,6 +291,11 @@ class FluentQ {
this[kState].push(A(...aggregations))
return this
}
raw (obj: Record<string, any>): this {
this[kState].push(obj)
return this
}
}
export default function build () {

View File

@ -85,8 +85,14 @@ function Q (...blocks: (SearchRequest | T.QueryContainer | T.QueryContainer[] |
for (const block of blocks) {
const key = Object.keys(block)[0]
if (topLevelKeys.includes(key)) {
// @ts-expect-error
body[key] = block[key]
if (key === 'sort') {
body.sort = body.sort || []
// @ts-expect-error
body.sort.push.apply(body.sort, block[key])
} else {
// @ts-expect-error
body[key] = block[key]
}
}
}
@ -577,14 +583,17 @@ namespace Q {
return { boosting: boostOpts }
}
export function sort (key: string | any[], opts: Record<string, any> | string): Record<string, any> | Record<string, any>[] {
if (Array.isArray(key) === true) {
return { sort: key }
export function sort (key: string | string[]): { sort: string[] }
export function sort (key: string | string[], order: T.SortOrder): { sort: Record<string, T.SortOrder>[] }
export function sort (key: string | string[], opts: T.Sort): { sort: Record<string, T.Sort>[] }
export function sort (key: string | string[], opts?: any): any {
if (opts == null) {
return { sort: Array.isArray(key) ? key : [key] }
}
return {
// @ts-ignore
sort: [{ [key]: opts }]
if (Array.isArray(key)) {
return { sort: key.map(k => ({ [k]: opts })) }
}
return { sort: [{ [key]: opts }] }
}
export function size (s: number | Symbol): { size: number } {