Skip to content

Commit

Permalink
Add _id to the result of helpers.search (#2432)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramikg authored Nov 6, 2024
1 parent edb5563 commit 2455dac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
13 changes: 10 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { errors, TransportResult, TransportRequestOptions, TransportRequestOptio
import { Table, TypeMap, tableFromIPC, RecordBatchStreamReader } from 'apache-arrow/Arrow.node'
import Client from './client'
import * as T from './api/types'
import { Id } from './api/types'

export interface HelpersOptions {
client: Client
Expand Down Expand Up @@ -193,12 +194,18 @@ export default class Helpers {
* @param {object} options - The client optional configuration for this request.
* @return {array} The documents that matched the request.
*/
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<TDocument[]> {
appendFilterPath('hits.hits._source', params, true)
async search<TDocument = unknown> (params: T.SearchRequest, options: TransportRequestOptions = {}): Promise<Array<TDocument & {_id: Id}>> {
appendFilterPath('hits.hits._id,hits.hits._source', params, true)
options.meta = true
const { body: result } = await this[kClient].search<TDocument>(params, options as TransportRequestOptionsWithMeta)
if (result.hits?.hits != null) {
return result.hits.hits.map(d => d._source as TDocument)
return result.hits.hits.map(d => ({
// Starting with version 8.14.0, _id is optional, but in our case it's always present.
// See @es_quirk documentation in elasticsearch-specification/specification/_global/search/_types/hits.ts
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
_id: d._id!,
...(d._source as TDocument)
}))
}
return []
}
Expand Down
30 changes: 15 additions & 15 deletions test/unit/helpers/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import { connection } from '../../utils'
test('Search should have an additional documents property', async t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.equal(params.querystring, 'filter_path=hits.hits._source')
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
return {
body: {
hits: {
hits: [
{ _source: { one: 'one' } },
{ _source: { two: 'two' } },
{ _source: { three: 'three' } }
{ _id: '1', _source: { one: 'one' } },
{ _id: '2', _source: { two: 'two' } },
{ _id: '3', _source: { three: 'three' } }
]
}
}
Expand All @@ -49,16 +49,16 @@ test('Search should have an additional documents property', async t => {
query: { match_all: {} }
})
t.same(result, [
{ one: 'one' },
{ two: 'two' },
{ three: 'three' }
{ _id: '1', one: 'one' },
{ _id: '2', two: 'two' },
{ _id: '3', three: 'three' }
])
})

test('kGetHits fallback', async t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.equal(params.querystring, 'filter_path=hits.hits._source')
t.equal(params.querystring, 'filter_path=hits.hits._id%2Chits.hits._source')
return { body: {} }
}
})
Expand All @@ -78,14 +78,14 @@ test('kGetHits fallback', async t => {
test('Merge filter paths (snake_case)', async t => {
const MockConnection = connection.buildMockConnection({
onRequest (params) {
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._source')
t.equal(params.querystring, 'filter_path=foo%2Chits.hits._id%2Chits.hits._source')
return {
body: {
hits: {
hits: [
{ _source: { one: 'one' } },
{ _source: { two: 'two' } },
{ _source: { three: 'three' } }
{ _id: '1', _source: { one: 'one' } },
{ _id: '2', _source: { two: 'two' } },
{ _id: '3', _source: { three: 'three' } }
]
}
}
Expand All @@ -104,9 +104,9 @@ test('Merge filter paths (snake_case)', async t => {
query: { match_all: {} }
})
t.same(result, [
{ one: 'one' },
{ two: 'two' },
{ three: 'three' }
{ _id: '1', one: 'one' },
{ _id: '2', two: 'two' },
{ _id: '3', three: 'three' }
])
})

0 comments on commit 2455dac

Please sign in to comment.