This may be a bug, I'm not sure.
I'm looking at upgrading from the elasticsearch 15.4.1 npm package to @elastic/elasticsearch 7.0.0-rc.2. I have existing typescript code written against the 15.4.1 (using @types/elasticsearch) using client.count that compiles and works, but the 7.0 package will not compile. It looks like either the typescript definition is wrong, or the response from count has been changed and the API documentation hasn't been updated?
per the documentation, this should work (and works against the v15 typescript):
```import { Client } from '@elastic/elasticsearch'
const count = async () => {
const client = new Client({ node: 'http://localhost:9200' })
let result = await client.count({ index: "index", body: "doesn't-matter"});
//this should be valid per https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-count.html
console.log(result.count);
}
```
That won't compile against the v7 typescript definition because ApiResponse<any> doesn't have a count property. Is this intentional? Should I be using body instead (are the docs incorrect)?
Thanks.
Hello!
I recommend you to read the Breaking changes coming from the old client doc :)
Every API now returns an object structured as follows:
{
body: object,
statusCode: number,
headers: object,
warnings: array,
meta: object
}
So you need to update your code:
const count = async () => {
const client = new Client({ node: 'http://localhost:9200' })
- let result = await client.count({ index: "index", body: "doesn't-matter"});
+ let { body: result } = await client.count({ index: "index", body: "doesn't-matter"});
console.log(result.count);
}
or
const count = async () => {
const client = new Client({ node: 'http://localhost:9200' })
let result = await client.count({ index: "index", body: "doesn't-matter"});
- console.log(result.count);
+ console.log(result.body.count);
}
Regarding TypeScript, take a look at our documentation :)
Ah, thank you, that's helpful.
Looking at the typescript example it looks as though it's the developer's responsibility to create the interface that defines what the response body from each API method looks like. That seems backwards - I'd expect the library I'm using to consume an API to internalize all knowledge of the API's response types. Is that the intention?
Looking at the actual typescript, every method definition I've seen seems to have the same problem of being defined as ApiMethod<T, any>. So the entire API essentially returns any unless I go out of my way to write a type for each body inside my code.
Honestly, the typescript definitions -- which I want to make clear, I applaud you guys for including and wish more people would make that effort -- are way less useful than they could be as things stand today. In fact they actually force a developer who wants to use typescript to do _more_ work to use your library.
I hope all this makes sense and I further hope I'm not coming off as nasty here, I'm sincerely trying to give feedback to make your product better :)
Thanks @lamontadams, we're well aware that having TypeScript definitions for the body of Elasticsearch responses would be great, but unfortunately they're nearly impossible to write in a way that can be shared with every user for every use case. We're working on improvements on the ES side of things, and within the broader ES clients team, to make this possible but for now this is the best we have to offer.
We understand that this might be important for you, but this issue has been automatically marked as stale because it has not had recent activity either from our end or yours.
It will be closed if no further activity occurs, please write a comment if you would like to keep this going.
Note: in the past months we have built a new client, that has just landed in master. If you want to open an issue or a pr for the legacy client, you should do that in https://github.com/elastic/elasticsearch-js-legacy
Most helpful comment
Hello!
I recommend you to read the Breaking changes coming from the old client doc :)
Every API now returns an object structured as follows:
So you need to update your code:
or
Regarding TypeScript, take a look at our documentation :)