Skip to content

Commit 4eaf662

Browse files
ramikggithub-actions[bot]
authored andcommitted
Add _id to the result of helpers.search (#2432)
(cherry picked from commit 2455dac)
1 parent dd9b38b commit 4eaf662

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/helpers.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { errors, TransportResult, TransportRequestOptions, TransportRequestOptio
2828
import { Table, TypeMap, tableFromIPC, RecordBatchStreamReader } from 'apache-arrow/Arrow.node'
2929
import Client from './client'
3030
import * as T from './api/types'
31+
import { Id } from './api/types'
3132

3233
export interface HelpersOptions {
3334
client: Client
@@ -193,12 +194,18 @@ export default class Helpers {
193194
* @param {object} options - The client optional configuration for this request.
194195
* @return {array} The documents that matched the request.
195196
*/
196-
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<TDocument[]> {
197-
appendFilterPath('hits.hits._source', params, true)
197+
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<Array<TDocument & {_id: Id}>> {
198+
appendFilterPath('hits.hits._id,hits.hits._source', params, true)
198199
options.meta = true
199200
const { body: result } = await this[kClient].search<TDocument>(params, options as TransportRequestOptionsWithMeta)
200201
if (result.hits?.hits != null) {
201-
return result.hits.hits.map(d => d._source as TDocument)
202+
return result.hits.hits.map(d => ({
203+
// Starting with version 8.14.0, _id is optional, but in our case it's always present.
204+
// See @es_quirk documentation in elasticsearch-specification/specification/_global/search/_types/hits.ts
205+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
206+
_id: d._id!,
207+
...(d._source as TDocument)
208+
}))
202209
}
203210
return []
204211
}

test/unit/helpers/search.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ import { connection } from '../../utils'
2424
test('Search should have an additional documents property', async t => {
2525
const MockConnection = connection.buildMockConnection({
2626
onRequest (params) {
27-
t.equal(params.querystring, 'filter_path=hits.hits._source')
27+
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
2828
return {
2929
body: {
3030
hits: {
3131
hits: [
32-
{ _source: { one: 'one' } },
33-
{ _source: { two: 'two' } },
34-
{ _source: { three: 'three' } }
32+
{ _id: '1', _source: { one: 'one' } },
33+
{ _id: '2', _source: { two: 'two' } },
34+
{ _id: '3', _source: { three: 'three' } }
3535
]
3636
}
3737
}
@@ -49,16 +49,16 @@ test('Search should have an additional documents property', async t => {
4949
query: { match_all: {} }
5050
})
5151
t.same(result, [
52-
{ one: 'one' },
53-
{ two: 'two' },
54-
{ three: 'three' }
52+
{ _id: '1', one: 'one' },
53+
{ _id: '2', two: 'two' },
54+
{ _id: '3', three: 'three' }
5555
])
5656
})
5757

5858
test('kGetHits fallback', async t => {
5959
const MockConnection = connection.buildMockConnection({
6060
onRequest (params) {
61-
t.equal(params.querystring, 'filter_path=hits.hits._source')
61+
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
6262
return { body: {} }
6363
}
6464
})
@@ -78,14 +78,14 @@ test('kGetHits fallback', async t => {
7878
test('Merge filter paths (snake_case)', async t => {
7979
const MockConnection = connection.buildMockConnection({
8080
onRequest (params) {
81-
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._source')
81+
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._id%2Chits.hits._source')
8282
return {
8383
body: {
8484
hits: {
8585
hits: [
86-
{ _source: { one: 'one' } },
87-
{ _source: { two: 'two' } },
88-
{ _source: { three: 'three' } }
86+
{ _id: '1', _source: { one: 'one' } },
87+
{ _id: '2', _source: { two: 'two' } },
88+
{ _id: '3', _source: { three: 'three' } }
8989
]
9090
}
9191
}
@@ -104,9 +104,9 @@ test('Merge filter paths (snake_case)', async t => {
104104
query: { match_all: {} }
105105
})
106106
t.same(result, [
107-
{ one: 'one' },
108-
{ two: 'two' },
109-
{ three: 'three' }
107+
{ _id: '1', one: 'one' },
108+
{ _id: '2', two: 'two' },
109+
{ _id: '3', three: 'three' }
110110
])
111111
})
112112

0 commit comments

Comments
 (0)