elasticsearch-js
elasticsearch-js copied to clipboard
Issue with search Sort
Hey,
You can't use Sort properly when using Typescript
In order for this:
sort: [ { createdDate: { order: "Desc", } } ]
To work - you must mark the entire query with "as any".
This is due to the fact that the "sort" types on the file lib/api/types.d.ts is marked as string | string[]
. And though it's working for "ASC" sorting, if you need "desc" you have to make some sort of override like this.
I'm using version: 8.2.1
I am using JSON.stringify for now to fix that problem and it is working for me
Hey As I've said there ways to go around that, but I think it shoule be solved since for version 7 it used to work ok with TS (as you can see if you try to use the deprecated "body" version)
I too have this same issue sorting does not work at all. I am on version 6.6.1 and using Typescript. Any help would very much useful and appreciated :-) . Thanks
`
await this.client.indices.refresh({ index })
const searchParams: RequestParams.Search = {
index,
body: {
query
},
_source: ['@timestamp'],
sort: JSON.stringify([{ "@timestamp": { "order": "desc" } }]), // This needs to be a string
size
}
result = await this.client.search(searchParams)
return result`
And my results
[
{ '@timestamp': '2022-07-01T13:35:38.545Z' },
{ '@timestamp': '2022-07-01T13:35:38.531Z' },
{ '@timestamp': '2022-07-04T13:26:47.754Z' },
{ '@timestamp': '2022-07-01T12:44:11.054Z' },
{ '@timestamp': '2022-07-01T15:09:19.114Z' },
{ '@timestamp': '2022-07-02T22:11:40.658Z' },
{ '@timestamp': '2022-07-02T22:12:55.148Z' },
{ '@timestamp': '2022-07-02T22:15:00.430Z' },
{ '@timestamp': '2022-07-03T13:52:53.238Z' },
{ '@timestamp': '2022-07-01T10:12:44.849Z' },
{ '@timestamp': '2022-07-05T10:06:24.919Z' },
{ '@timestamp': '2022-07-06T02:32:59.841Z' },
{ '@timestamp': '2022-07-06T10:08:42.775Z' }
]
Hi, If the issue is open, I want to work on it.